Pairs Forming LCM LightOJ - 1236

Pairs Forming LCM LightOJ - 1236

题意

问共有多少组数的最大公约数是n

分析

组合数学 ,唯一分解定理

参考代码

int Prime[670000];
const int LEN  = 1e7+1;
bool vis[LEN];
//int Prime[666666];
int cnt = 1;
void init(void)
{
    int n = 1e7;
    for(int i = 2; i <= n; ++i)
    {
        if(!vis[i])
        {
            Prime[cnt++] = i;
            for(LL j = (LL)i +  i; j <= n; j += i)
                vis[j] = 1;
        }

    }
}
int main(void)
{
//    const int n = 1e7;
//    cout<<n/log(n)<<endl;
    std::ios::sync_with_stdio(false);
    init();
    int T;
    cin>>T;
    int kase = 0;
    while(T--)
    {
        LL n;
        cin>>n;
        int num ;
        LL ans = 1;
        for(int i = 1; i < cnt&& n != 1; ++i)
        {
            if(n%Prime[i]==0)
            {
                num = 0;
                while(n%Prime[i]==0)
                {
                    n /= Prime[i];
                    num++;
                }
                ans *= 2*num+1;//对于某一个质因子来说,共有2(num+1) - 1种选择的方法
            }
        }
        if(n!=1)
            ans *= 3;
        ans = ans/2 + 1;//将重复的除去,并把(n,n) 这种情况给加上
        printf("Case %d: %lld\n",++kase,ans);
    }
    return 0;
}

posted on 2017-09-20 21:54  zzuzxy  阅读(84)  评论(0)    收藏  举报

导航