当进制转换后所剩下的为数较少时(2位,3位),对应的base都比较大,可以用数学的方法计算出来。
预处理掉转换后位数为3位后,base就小于n的3次方了,可以暴力计算。。。。
2 10 19
Case #1: 0 Case #2: 1Hint10 shown in hexadecimal number system is another letter different from ‘0’-‘9’, we can represent it as ‘A’, and you can extend to other cases.
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <set> #include <cmath> using namespace std; typedef long long int LL; LL n; set<LL> ans; bool change(LL x,LL base) { bool flag=true; while(x) { LL temp=x%base; x/=base; if(temp>=3LL&&temp<=6LL) continue; else { flag=false; break; } } return flag; } int main() { int T_T,cas=1; scanf("%d",&T_T); while(T_T--) { ans.clear(); cin>>n; if(n>=3LL&&n<=6LL) { cout<<"Case #"<<cas++<<": -1"<<endl; continue; } for(LL a=3;a<=6;a++) { for(LL b=3;b<=6;b++) { LL limit=max(a,b); if( (n-b)%a == 0 ) { if( (n-b)/a > limit) { ans.insert((n-b)/a); } } } } for(LL a=3;a<=6;a++) { for(LL b=3;b<=6;b++) { for(LL c=3;c<=6;c++) { LL C=c-n; if(b*b >= 4LL*a*C ) { LL deta=sqrt(b*b-4LL*a*C); LL base1=(-b+deta)/(2*a); LL base2=(-b-deta)/(2*a); LL limit=max(a,max(b,c)); if(a*base1*base1+b*base1+c==n && base1>limit) { ans.insert(base1); } if(a*base2*base2+b*base2+c==n && base2>limit) { ans.insert(base2); } } } } } for(LL i=4;i*i*i<=n;i++) { if(change(n,i)) { ans.insert(i); } } cout<<"Case #"<<cas++<<": "<<ans.size()<<endl; } return 0; }