light oj1028 - Trailing Zeroes (I)

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

Output for Sample Input


3

9

5

2


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.

分析:题目大意是一个数转化成任意进制后末尾有0的种数 ,就是求一个数的因子数,转化成n进制后末尾有0的数一定是原十进制数能被n整除,也就是说如果一个数是n的倍数的时候,一定能转换成n进制数末尾时0.

素数分解的唯一性:p1^x1*p2^x2...pn^xn(一个整数可唯一地分解为一些不同质因子的若干次方的乘积)

再根据乘法原理 因子个数为(x1+1)*(x2+1) + ... + (xn + 1)

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>

using namespace std;
typedef long long ll;
const ll N=1e6+10;
ll vis[N], prime[N];
ll k;

void init()
{
k = 0;
memset(vis,0,sizeof(vis));

for(ll i = 2; i <= N; i++)
{
if(!vis[i])
{
prime[k++] = i;

for(ll j = i * i; j <= N; j += i)
vis[j] = 1;
}
}
}
int main()
{
int T, cas;
ll n, ans;

init();

scanf("%d", &T);
cas = 0;
while(T--)
{
cas++;
ans = 1;

scanf("%lld", &n);

for(ll i = 0; i < k && prime[i] * prime[i] <= n; i++)
{
if(prime[i] > n)
break;

if(n % prime[i] == 0)
{
long long sum = 1;

while(n % prime[i] == 0)
{
sum++;
n /= prime[i];

}
ans *= sum;
}
}

if(n > 1)
ans *= 2;

printf("Case %d: %lld\n", cas, ans-1);

}

return 0;
}

时间: 2024-10-10 13:50:39

light oj1028 - Trailing Zeroes (I)的相关文章

Light OJ 1028 Trailing Zeroes (I) 求n因子数

今天真机调试的时候莫名其妙遇到了这样的一个问题: This product type must be built using a provisioning profile, however no provisioning profile matching both the identity "iPhone Developer" and the bundle identifier..... 具体如下图所示: 十分蛋疼, 发现不管是从网上下的demo, 还是自己的过程.凡事真机测试的时候都

Light oj 1138 - Trailing Zeroes (III) 【二分查找 &amp;&amp; N!中末尾连续0的个数】

1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1*2*...*N. F

Light oj 1138 - Trailing Zeroes (III) 【二分查找好题】【 给出N!末尾有连续的Q个0,让你求最小的N】

1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1*2*...*N. F

172. Factorial Trailing Zeroes

1. 问题描述 Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.Tags: MathSimilar Problems: (H) Number of Digit One 2. 解题思路 分解质因子, 当且仅当 因子中出现 一对 (2,5)时, 最后结果会增加一个 trailing zero.1. 2的

[LeetCode] Factorial Trailing Zeroes 阶乘末尾0

Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. Credits:Special thanks to @ts for adding this problem and creating all test cases. Hide Tags Math 这题应该是2014年年底修改该过测试样本,之前的通过

Java-Factorial Trailing Zeroes

Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. Credits: Special thanks to @ts for adding this problem and creating all test cases. 看了半天没看懂题目意思 以为要求n结尾0的个数 搞了半天是要求n!即 n的阶乘结

LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number

数学题 172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. (Easy) 分析:求n的阶乘中末位0的个数,也就是求n!中因数5的个数(2比5多),简单思路是遍历一遍,对于每个数,以此除以5求其因数5的个数,但会超时. 考虑到一个数n比他小

LightOJ Trailing Zeroes (III) 1138【二分搜索+阶乘分解】

1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1*2*...*N. F

[LeetCode]172.Factorial Trailing Zeroes

题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 分析 朴素解法: 首先求出n!,然后计算末尾0的个数.(重复÷10,直到余数非0) 该解法在输入的数字稍大时就会导致阶乘得数溢出,不足取. O(logn)解法: 考虑n!的质数因子. 后缀0总是由质因子2和质因子5相乘得来的.如果我们可以计数