LightOJ - 1236 - LCM(ユニーク分解定理)を形成するペア

リンク:

https://vjudge.net/problem/LightOJ-1236

質問の意味:

次のコードの結果を検索します。

長い長いpairsFormLCM(int型N){
長い長いRES = 0。
以下のために(INT I 1 =; I <= N; I ++)
のための(INT J = I; J <= N; J ++)
IF(LCM(i、j)は== n)の解像度++。// LCM手段最小公倍数
の戻り解像度。
}

コードの単純な実装がタイムアウトすることがあります。あなたは、コードを分析する場合は、コードが実際にどのLCM(i、j)のためのペアの数を(i、j)のカウント= n、および(I≤j)がいることがわかります。

アイデア:

LCM(A、B)を考える= N 、 存在する(= P_1 ^ {K1 \ } * P_2 ^ {K2} * P_3 ^ {K3}、B = P_1 ^ {T1} * P_2 ^ {T2}、N = P_1 } * {P_2 E1 ^ {E2} ^ \)
あなたがLCM(a、b)がしたい= nの場合は 、 それぞれのp、最大(KI、TI)のために、それを確認する必要があり = EI。 各素数がn個の指標を満たすことを確認してください。
それぞれがp同減算値(2 * EI + 1)種を有するようになっています。
障害(B)で得られる
同じ計算ので、一度だけ、2 + 1に加えて、順序付けられました。

コード:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<vector>
#include<map>

using namespace std;
typedef long long LL;
const int INF = 1e9;

const int MAXN = 1e7+10;
const int MOD = 1e9+7;

bool IsPrime[MAXN];
int Prime[1000010];
int tot;
LL n;

void Init()
{
    tot = 0;
    memset(IsPrime, 0, sizeof(IsPrime));
    IsPrime[1] = 1;
    for (int i = 2;i < MAXN;i++)
    {
        if (IsPrime[i] == 0)
            Prime[++tot] = i;
        for (int j = 1;j <= tot && i*Prime[j] < MAXN;j++)
        {
            IsPrime[i*Prime[j]] = 1;
            if (i%Prime[j] == 0)
                break;
        }
    }
}

int main()
{
    Init();
    int t, cnt = 0;
    scanf("%d", &t);
    while(t--)
    {
        printf("Case %d:", ++cnt);
        scanf("%lld", &n);
        LL x = n;
        LL sum = 1;
        for (int i = 1;i <= tot && Prime[i] <= x;i++)
        {
            if (x%Prime[i] == 0)
            {
                int cnt = 0;
                while(x%Prime[i] == 0)
                {
                    cnt++;
                    x /= Prime[i];
                }
                sum *= (2LL*cnt+1);
            }
        }
        if (x>1)
            sum *= 3LL;
        sum = (sum+1)/2;
        printf(" %lld\n", sum); 
    }
    
    return 0;
}

おすすめ

転載: www.cnblogs.com/YDDDD/p/11846361.html