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
求每一个素因子出现的次数,然后每个次数+1相乘就是n的因子个数。不会出现重复,因为任意个素数相乘不会等于素数。
还运用了一个优化,具体意思是对于任意一数n,他的素因子大于sqrt(n)的不会超过一个。如果有两个素因子大于sqrt(n),明显不成立。
#include<stdio.h>
#include<string.h>
int prime[1000010],is[1000010],l=0;
void lprime()
{
int i,j;
is[1]=1;
for(i=2;i<=100000;i++)
{
if(!is[i])
{
prime[l++]=i;
for(j=i+i;j<=100000;j+=i)
is[j]=1;
}
}
}
int main()
{
lprime();
int t,k=1;
scanf("%d",&t);
while(t--)
{
long long n,i,ans,sum=1;
scanf("%lld",&n);
for(i=0;i<l&&prime[i]*prime[i]<=n;i++)
{
ans=0;
while(n%prime[i]==0)
{
ans++;
n/=prime[i];
}
sum*=(ans+1);
}
if(n>1)
sum*=2;
sum-=1;
printf("Case %d: %lld\n",k++,sum);
}
return 0;
}