LCM (LightOJ - 1236)을 형성 쌍 [S] 간단한 소수 인수 분해 수] [] 산술의 기본 정리 (미완성)

LCM (LightOJ - 1236)을 형성 쌍 [S] 간단한 소수 인수 분해 수] [] 산술의 기본 정리 (미완성)

태그 : 강의 주제에 대한 중지를 얻기


제목 설명

다음 코드의 결과를 찾기 :

long long pairsFormLCM( int n ) {
    long long res = 0;
    for( int i = 1; i <= n; i++ )
        for( int j = i; j <= n; j++ )
           if( lcm(i, j) == n ) res++; // lcm means least common multiple
    return res;
}

코드의 정직하고 구현 시간이 초과 될 수 있습니다. 코드를 분석 할 경우, 당신은 코드가 실제로 쌍 (i, j)의 수를 계산 찾을 것이다 LCM (I, J) = n 및 (I ≤ J)합니다.
입력

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case starts with a line containing an integer n (1 ≤ n ≤ 1014).

산출

For each case, print the case number and the value returned by the function 'pairsFormLCM(n)'.

샘플 입력

15

2

3

4

6

8

10

12

15

18

20

21

24

25

27

29

샘플 출력

Case 1: 2

Case 2: 2

Case 3: 3

Case 4: 5

Case 5: 4

Case 6: 5

Case 7: 8

Case 8: 5

Case 9: 8

Case 10: 8

Case 11: 5

Case 12: 11

Case 13: 3

Case 14: 4

Case 15: 2

문제의 의미

을 감안할 때 \ (\ N- 형) .
상대 \ (\ sum_ {I = 1 } ^ {N} \ {sum_ J = 1} ^ {N} LCM (I, J) = N] \) 값.


해결


코드

/*
Problem
    LightOJ - 1236
Status
    Accepted
Time
    410ms
Memory
    19664kB
Length
    1299
Lang
    C++
Submitted
    2019-11-25 15:30:08
Shared
    
RemoteRunId
    1640611
*/

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int MAXN = 1e7 + 50;

bool vis[MAXN];
int prime[MAXN / 10], p[MAXN / 10], m = 0, cnt;

void fill_0()
{
    for(int i = 1; i <= cnt; i ++)
        p[i] = 0;
    return;
}
void get_prime()                //线性筛,筛出1e7以内全部质数.
{
    vis[1] = 1;

    for(int i = 2; i <= int(1e7 + 5); i ++){
        if(!vis[i])
            prime[++ m] = i;

        for(int j = 1; j <= m && i * prime[j] <= int(1e7 + 5); j ++){
            vis[i * prime[j]] = 1;

            if(i % prime[j] == 0)
                break;
        }
    }

    return;
}
void get_fact(ll x)
{
    fill_0();           //将p数组归零.
    cnt = 0;
    for(int i = 1; i <= m && 1ll * prime[i] * prime[i] <= x; i ++){ //以下求得一个数的质因数分解每个质数的幂次.
        if(x % prime[i] == 0){

            cnt ++;
            while(x % prime[i] == 0){
            
                p[cnt] ++;
                x /= prime[i];
                
            }

        }
    }

    if(x != 1)
        p[++ cnt] = 1;

    return;
}

ll work()
{
    ll res = 1;

    for(int i = 1; i <= cnt; i ++)
        res *= 1ll * (2 * p[i] + 1);

    return (res + 1) >> 1;
}
int main()
{
    get_prime();
    
    int times, _case = 0;

    scanf("%d", &times);

    while(times --){

        ll x;
        scanf("%lld", &x);

        get_fact(x);

        printf("Case %d: %lld\n", ++ _case, work());
    }

    return 0;
}

추천

출처www.cnblogs.com/satchelpp/p/11941165.html