LightOj1028

题目链接:http://lightoj.com/volume_showproblem.php?problem=1028

题意:给你一个数 n (1<=n<=10^12), 然后我们可以把它转化为k(k>=2)进制的数,但是要满足转化之后的数的最后一位是0,求这样的k共有多少个

其实就是求n的大于1的因子有多少个;

一个数n可以写成 n = p1^a1 * p2^a2 * p3^a3 * ... pk^ak(其中pi是n的素因子)那么n的所有因子个数根据乘法原理就是(a1+1)*(a2+1)*...*(ak+1),所以我们可以打表求10^6以内的素数,然后依次计算即可;

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
typedef unsigned long long LL;
#define N 1000001
using namespace std;

int f[N], p[N], k = 0;

void Prime()
{
    for(int i=2; i<N; i++)
    {
        if(!f[i]) p[k++] = i;
        for(int j=i; j<N; j+=i)
            f[j] = 1;
    }
}

int main()
{
    Prime();

    int T, t = 1;
    LL n;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%lld", &n);
        LL ans = 1;

        for(int i=0; i<k && (LL)p[i]*p[i]<=n; i++)
        {
            int cnt = 1;
            while(n%p[i] == 0)
            {
                cnt ++;
                n = n/p[i];
            }
            ans *= cnt;
        }

        if(n>1) ans *= 2;

        printf("Case %d: %lld
", t++, ans-1);
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/zhengguiping--9876/p/5996092.html