zoukankan      html  css  js  c++  java
  • Light OJ 1028

    题目链接: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
    ", cases, ans-1);
    }
    
    int main()
    {
        int n;
        scanf("%d", &n);
        init();
        for(int i=1; i<=n; ++i)
        {
            solve(i);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    drf3
    字典的操作方法
    列表的操作方法
    字符串的操作方法
    while循环和基本运算符
    初识数据类型
    USDT相关
    带团队
    CentOS7更改时区及同步网络时间
    mac胡刷新dns
  • 原文地址:https://www.cnblogs.com/aiterator/p/6015449.html
Copyright © 2011-2022 走看看