lightoj1028求约数个数

We know what a base of a number is and what the properties are. For example, we use decimal number system, where the base is 10 and we use the symbols - {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. But in different bases we use different symbols. For example in binary number system we use only 0 and 1. Now in this problem, you are given an integer. You can convert it to any base you want to. But the condition is that if you convert it to any base then the number in that base should have at least one trailing zero that means a zero at the end.

For example, in decimal number system 2 doesn't have any trailing zero. But if we convert it to binary then 2 becomes (10)2 and it contains a trailing zero. Now you are given this task. You have to find the number of bases where the given number contains at least one trailing zero. You can use any base from two to infinite.

Input

Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case contains an integer N (1 ≤ N ≤ 1012).

Output

For each case, print the case number and the number of possible bases where N contains at least one trailing zero.

Sample Input

3

9

5

2

Sample Output

Case 1: 2

Case 2: 1

Case 3: 1

Note 

For 9, the possible bases are: 3 and 9. Since in base 39 is represented as 100, and in base 99 is represented as 10. In both bases, 9 contains a trailing zero.

n=a1*x^0+a2*x^1+a3*x^2+....

最后为0,a1就是0,n=a2*x^1+ a3*x^2+....=x(a2+a3*x+....)

问题转化为求约数个数,注意最后需要再减去1,1不算

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
#define maxn 1000005
int p[maxn];
int vis[maxn];
int cnt;
int t;
void init()
{
    memset(vis,0,sizeof(vis));
    memset(p,0,sizeof(p));
    cnt=0;
    for(int i=2;i<maxn;i++)
    {
        if(!vis[i])
        {
            p[cnt++]=i;
        }
        for(int j=0;j<cnt&&i*p[j]<maxn;j++)
        {
            vis[i*p[j]]=1;
            if(i%p[j]==0)
                break;
        }
    }
}
ll solve(ll x)
{
    ll num=1;
    for(int i=0;i<cnt&&p[i]*p[i]<=x;i++)
    {
        int cc=0;
        while(x%p[i]==0)
        {
            cc++;
            x/=p[i];
        }
        num*=cc+1;
    }
    if(x>1)
        num*=2;
    return num-1;
}
int main()
{scanf("%d",&t);
int w=0;
init();
while(t--)
{
    w++;
    ll n;
    scanf("%lld",&n);
    printf("Case %d: %lld\n",w,solve(n));
}
return 0;
}


猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/89811091
今日推荐