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 (10)_2 (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 (T ≤ 10000), denoting the number of test cases.
Each case contains an integer N (1 ≤ N ≤
1
0
12
10^{12}
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.
题意
给出一个十进制数,求有多少种进制表示该数时有后置零,例如9可以用3进制和9进制表示所以是两种
题解
进制基数就是逢几进一,所以给出数的约数即为一个基数。所以题意转化为求给出数的约数个数,但是注意去掉约数1
约数个数公式:
对于
n
=
p
1
x
1
p
2
x
2
⋯
p
k
x
k
n=p_1^{x_1}p_2^{x_2}\cdots\ p_k^{x_k}
n=p1x1p2x2⋯ pkxk有
约数个数:
(
x
1
+
1
)
(
x
2
+
1
)
⋯
(
x
k
+
1
)
=
Π
i
=
1
k
(
x
i
+
1
)
(x_1+1)(x_2+1)\cdots(x_k+1)=\Pi_{i=1}^{k}{(x_i+1)}
(x1+1)(x2+1)⋯(xk+1)=Πi=1k(xi+1)
代码
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <queue>
#include <math.h>
#include <string>
#include <string.h>
#include <map>
#include <set>
#include <unordered_map>
//#include <tr1/unordered_map>
using namespace std;
#define me(x,y) memset(x,y,sizeof x)
#define MIN(x,y) x < y ? x : y
#define MAX(x,y) x > y ? x : y
typedef long long ll;
typedef unsigned long long ull;
const long double INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const double eps = 1e-06;
const long double PI = std::acos(-1);
const int M=32768;
const int maxn = 1e6;
bool isprime[maxn+10];
vector<int> prime;
void euler(){
isprime[1] = 1;
for(int i = 2; i <= maxn; ++i){
if(!isprime[i]) prime.push_back(i);
for(int j = 0; j < prime.size() && prime[j]*i <= maxn; ++j){
isprime[prime[j]*i]=1;
if(i%prime[j] == 0) break;
}
}
}
int main() {
int t,ca=1;
cin>>t;
euler();
while(t--){
ll n,sum=1;
cin>>n;
for(int i = 0; i < prime.size() && n >= (prime[i]*prime[i]); ++i){
if(n%prime[i]==0){
int ans=0;
while(n%prime[i]==0){
ans++;
n /= prime[i];
}
sum *= ans+1;
}
}
if(n > 1){
sum *= 2;
}
printf("Case %d: %lld\n",ca++,sum-1);
}
return 0;
}
/*
*/