zoukankan      html  css  js  c++  java
  • Lightoj1028【计算约数个数】

    思路:
    最终就是求一个数的约数(除了1)对吧.
    然后想要枚举sqrt(N)受阻,枚举素数数组受阻,加上prime[i]*prime[i]<=n就好了?那就好了吧。

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    LL prime[1000100];
    bool IsPrime[1000100];
    int num;
    void init_prime()
    {
        num=0;
        memset(IsPrime,false,sizeof(IsPrime));
        for(LL i=2;i<=1000000;i++)
        {
            if(IsPrime[i]) continue;
            prime[num++]=i;
            for(LL j=i+i;j<=1000000;j+=i)
                IsPrime[j]=true;
        }
    }
    LL solve(LL n)
    {
        LL ans=1,sum;
        for(int i=0;i<num&&prime[i]*prime[i]<=n;i++)
        {
            LL tmp=prime[i];
            sum=0;
            if(n%tmp==0)
            {
                while(n%tmp==0)
                {
                    n/=tmp;
                    sum++;
                }
                ans*=(sum+1LL);
            }
        }
        if(n>1) ans*=2;
        return ans-1;
    }
    
    int main()
    {
        int cas=1,T;
        LL n;
        init_prime();
        scanf("%d",&T);
        while(T--)
        {
            scanf("%lld",&n);
            printf("Case %d: %lld
    ",cas++,solve(n));
        }
        return 0;
    }
    



  • 相关阅读:
    leetcode-String to Integer (atoi)
    2014薪水
    Ubunt下的软件集
    ubuntu常用软件
    python模块安装
    ubuntu下玩三国杀
    递归函数
    匿名函数
    装饰器函数
    生成器
  • 原文地址:https://www.cnblogs.com/keyboarder-zsq/p/6777387.html
Copyright © 2011-2022 走看看