首先注意到 不会超过 ,故暴力枚举然后判断。
其次注意到当 的时候 不会超过 ,也可以暴力枚举然后判断。
现在只剩下 的情况。如果此时在 进制下 的位数 ,则有 ,也可以暴力做。
最后的情况是 ,且 的情况。此时有 ,于是枚举 的因数即可。
总时间复杂度为 。
参考代码
提交记录:https://codeforces.com/contest/2215/submission/390917999。
int T; ll n;
inline bool check(ll n, int b, int p){
int lst = -1, cnt = 0;
while(n){
int w = n % b;
if(~lst && lst != w && cnt < p) return false;
if(cnt == p) lst = -1, cnt = 0;
lst = w, cnt++;
n /= b;
}
return cnt == p;
}
inline void Solve(){
rd(T); while(T--){
rd(n);
int limp = log(n) / log(2) + 1, cnt = 0;
fo(p, 2, limp){
int limb = pow(n, 1.0 / (p - 1)) + 1;
if(p == 2){
for(int a = 1; (ll)a * a <= n; a++){
if(n % a == 0){
ll b = n / a - 1;
if(a < b) cnt++;
}
}
int limb = pow(n, 1.0 / 3) + 1;
fu(b, 2, limb){
if(check(n, b, p)){
cnt++;
}
}
continue;
}
fo(b, 2, limb){
if(check(n, b, p)){
cnt++;
}
}
}
wr(cnt), pc('\n');
}
return;
}