LightOj 1236

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

题意:给你一个数n,求有多少对(i,  j)满足 LCM(i, j) = n, (i<=j),  n <= 1e14;

之前做的那道LightOj 1215 中有说过:LCM(x, y) = ∏(所有质因子幂高的项之积);

那么本题就先把n分解质因子幂的形式,即 n = p1a1*p2a2*...*pkak;(pi为质数)

现在先不管i和j的大小,当 i 中包含因子p1a1时,j中可以包含p10|1|2|...|a1共有(a1+1)种可能,同样当j也有这种可能,所以共有2*(a1+1)

要减去 i 和 j 相等等于a1的时候;所以共有2*a1+1种,对于每个因子,都有这样的,所以连乘起来即可,除了i=j的情况每种情况都有两次,所以要/2+1;

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
typedef long long LL;
const int oo = 0xfffffff;
const int N = 1e7+1;
const double eps = 1e-8;

bool f[N];///用int会MLE;
int p[N/10], 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] = true;
    }
}

int main()
{
    Prime();
    ///printf("%d
", k);

    int T, t = 1;

    scanf("%d", &T);

    while(T--)
    {
        LL n;

        scanf("%lld", &n);

        LL ans = 1;

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

        if(n > 1) ans *= 3;

        ans = ans/2 + 1;

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