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)中排列, 因为其中一种排列肯定为所有素因子的幂指数为0, 那么这个因子就是1, 这个最后去掉就好。 最后只求n的每个素因子的幂指数,即可求解

代码如下:

#include<bits/stdc++.h>
using namespace std;
const double eps = 1e-10;

long long prime[100003], p = 0;
bool is_prime[1000003];

void init()
{
    memset(is_prime, true, sizeof(is_prime));
    for(int i=2; i<=1000000; ++ i)
    {
        if(is_prime[i])
        {
            prime[p++] = i;
            for(int j=i; j<=1000000; j+=i)
                is_prime[j] = false;
        }
    }
}

void solve(int cases)
{
    long long n;
    scanf("%lld", &n);
    long long ans = 1;
    for(int i=0; i<p&&prime[i]*prime[i]<=n; ++ i)
    {
        int res = 0;
        while(n % prime[i] == 0)
        {
            n /= prime[i];
            res ++;
        }
        ans *= (res + 1);
    }
    if(n > 1)
        ans *= 2;
    printf("Case %d: %lld\n", cases, ans-1);
}

int main()
{
    int n;
    scanf("%d", &n);
    init();
    for(int i=1; i<=n; ++i)
    {
        solve(i);
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/aiterator/p/6015449.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值