Trailing Zeroes (I)LightOJ 1028(因子个数公式)

Trailing Zeroes (I)

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 3; 9 is represented as 100, and in base 9; 9 is represented as 10. In both bases, 9 contains a trailing zero.

题意:

给定一个数字n( 1 n 10 12 ),把它转换成任意进制后,要求使得至少末尾是0,问这样的满足题意的进制有多少种。

分析:

这道题本质上是求n的因子数个数

为什么是求因子数个数呢,相信大家会有疑问,因为我一开始就有疑问

如果将N转化成x进制我们可以得到:

N = a n x n + a n 1 x n 1 + + a 1 x + a 0

而我们的目的就是让 a 0 变成0

假设x是N的一个因数

那么有

N = x r e s t = x ( a n x n 1 + a n 1 x n 2 + + a 1 + a 0 x )

因为N是整数,所以此时必须有 a 0 为0

所以我们就知道了,只有让N转变成N的因数进制的时候,其末尾有零,所以它有多少个因数,就能有多少种可能的进制

但是注意:我们知道1进制是完全没有意义的,但是算因子个数的时候必然包含了1,所以我们只需要最终结果减1即可。

code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e6+10;
const int N = 1e5+10;
bool isprime[maxn];
ll prime[N],cnt;
void init(){
    cnt = 0;
    memset(isprime,true,sizeof(isprime));
    isprime[0] = isprime[1] = false;
    for(int i = 2; i < maxn; i++){
        if(isprime[i]){
            prime[cnt++] = (ll)i;
            for(int j = i + i ; j < maxn; j += i){
                isprime[j] = false;
            }
        }
    }
}
ll resolve(ll n){
    ll ans = 1LL;
    for(int i = 0; i < cnt && prime[i] * prime[i] <= n; i++){
        if(n % prime[i] == 0){
            ll t = 1;//从1开始因为因子个数是(ai+1)的乘积
            while(n % prime[i] == 0){
                t++;
                n /= prime[i];
            }
            ans *= t;
        }
    }   
    if(n != 1) ans *= 2LL;
    return ans - 1;//减去1进制,不存在1进制
}
int main(){
    int t,cas = 0;
    init();
    scanf("%d",&t);
    while(t--){
        ll n;
        scanf("%lld",&n);
        printf("Case %d: %lld\n",++cas,resolve(n));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/codeswarrior/article/details/81407943
今日推荐