lightoj-1028 - Trailing Zeroes (I)(素数法求因子个数)

1028 - Trailing Zeroes (I)
PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB
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 3; 9 is represented as 100, and in base 9; 9 is represented as 10. In both bases, 9 contains a trailing zero.

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

typedef long long ll;

int prime[1000000];
int judge[1000100]={false};
int cnt;

void Prime(){

    prime[0] = 2;
     cnt = 1;
    for(ll i=3;i<1000010;i+=2){
        if(!judge[i]){//cout<<i<<endl;
            for(ll j=i*i;j<1000010;j+=i) judge[j] = true;
            prime[cnt++] = i;
        }
    }
}

int main(){
    Prime();
    int T,save;
    ll sum,n;
    scanf("%d",&T);
    for(int t=1;t<=T;t++){

        scanf("%lld",&n);

        sum = 1;
        for(int i=0;i<cnt&&prime[i]*prime[i]<=n;i++){ // prime[i]*prime[i]<=n 是个很重要的优化点
            save = 0;
            while(n%prime[i]==0){
                save++;
                n = n/prime[i];
            }
            sum *= save+1;
        }
        if(n!=1) sum*=2;
        sum--;
        printf("Case %d: %lld\n",t,sum);
    }

    return 0;
;}
时间: 2024-11-01 06:54:13

lightoj-1028 - Trailing Zeroes (I)(素数法求因子个数)的相关文章

LightOj 1138 - Trailing Zeroes (III) 阶乘末尾0的个数 &amp; 二分

题目链接:http://lightoj.com/volume_showproblem.php?problem=1138 题意:给你一个数n,然后找个一个最小的数x,使得x!的末尾有n个0:如果没有输出impossible 可以用二分求结果,重点是求一个数的阶乘中末尾含有0的个数,一定和因子5和2的个数有关,因子为2的明显比5多,所以我们只需要求一个数的阶乘的因子中一共有多少个5即可; LL Find(LL x) { LL ans = 0; while(x) { ans += x/5; x /=

LightOJ - 1028 Trailing Zeroes (I)

题意:T(<=1e4)组数据,求对于N(<=1e12),已知N在k进制表示下末位是0,求有多少个可能的k 相当与求解每个N的因数个数减1(代表除去1进制的情况) N=p1a1*p2a2*...*pnan,若N=m*n,那么m可表示为m=x*p1^(k), 0 <= k <= a1, 那么N的约数个数为(a1+1)(a2+1)...(an+1) 对于不大于1e12的数,若它为素数,答案恒为1:若它不为素数,那么它必然可以被不大于1e6的数整除,所以我们只需要预处理出不大于1e6的素数

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, 还是自己的过程.凡事真机测试的时候都

求因子个数和因子和

//求因子个数 int Facnt(int n) { int res = 1; for(int i=2;i*i<=n;i++) { if(n%i == 0) { int cnt = 0; do { n /= i; cnt++; }while(n%i==0); res *= (cnt+1); } } if(n > 1) res = 2*res; return res; } //求因子和 int Facsum(int n) { int res = 1; for(int i=2;i*i<=n;

Easy Number Challenge(暴力,求因子个数)

Easy Number Challenge Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 236B Appoint description:  System Crawler  (2016-04-26) Description Let's denote d(n) as the number of divisors of a

Almost All Divisors(求因子个数及思维)

---恢复内容开始--- We guessed some integer number xx. You are given a list of almost all its divisors. Almost all means that there are all divisors except 11and xx in the list. Your task is to find the minimum possible integer xx that can be the guessed nu

Lightoj 1090 - Trailing Zeroes (II)

题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1090 题目大意: 给出n,r,p,q四个数字1<=n,r,p,q<=1000000,求出的末尾有几个0? 解题思路: 是不是一下子懵了,数字好大,复杂度好高,精度怎么办···············,就问你怕不怕? 因为你是Acmer,这都不应该是问题.因为10的因子只有2和5,所以可以打表保存从1到当前数字相乘的积中分别含有2,5的个数.然后算出中分别含有2,5的个数,

Light OJ 1028 - Trailing Zeroes (I) (数学-因子个数)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1028 题目大意:n除了1有多少个因子(包括他本身) 解题思路:对于n的每个因子, 可以用n的所有素因子排列组合而来, n = (a1x1) * (a2 x2) * (a3x3)...*(anxn), 其中ai为n的素因子,那么n的因子的个数等同于(x1 + 1) * (x2 + 1) * (x3 + 1) ... * (xn + 1)中排列, 因为其中一种排列肯定为所有素因子

数学题 求因子个数

小C的倍数问题 Accepts: 1990 Submissions: 4931 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description 根据小学数学的知识,我们知道一个正整数x是3的倍数的条件是x每一位加起来的和是3的倍数.反之,如果一个数每一位加起来是3的倍数,则这个数肯定是3的倍数. 现在给定进制P,求有多少个B满足P进制下,一个正整数是B的倍数的