LightOJ - 1236 Pairs Forming LCM 合数分解

来源:互联网 发布:淘宝上的订单险是什么 编辑:程序博客网 时间:2024/06/06 04:28

LightOJ - 1236 Pairs Forming LCM 合数分解

一、题目

LightOJ - 1236 Pairs Forming LCM

Description
Find the result of the following code:

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;}

A straight forward implementation of the code may time out. If you analyze the code, you will find that the code actually counts the number of pairs (i, j) for which lcm(i, j) = n and (i ≤ j).

Input
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).

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

Sample Input
15
2
3
4
6
8
10
12
15
18
20
21
24
25
27
29
Sample Output
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

二、题目大意

求lcm[i,j]=n,i 和 j 的对数,其中i,j在[1,n]之间且i<=j,n<=1e14
三、解题思路
将n进行合数分解,得到素数表达式n=p^1^e1* p2^e2* p3^e3* p4^e4…pr^er。则对i=p1^e1’* p2^e2’* p3^e3’* p4^e4’…pr^er’,j=p1^e1”* p2^e2”* p3^e3”p4^e4”…pr^er”,lcm(i,j)=p1^max(e1’,e1”) p2^max(e2’,e2”) * …pr^max(er’,er”) ,则只需满足max(e1’,e1”)=e1, max(e2’,e2”)=e2,…, max(er’,er”)=e2 即可。若不考虑i,j大小关系,则对每个位置有2(2 ei+1)-1种选法,ans=ni==0(2(2ei+1)1),考虑i,j大小关系,i==j时,i==j==n,只有一种情况,其他情况i < j, i > j会被统计两次,一次答案为(ans1)2+1即:(ans+1)2
四、代码实现

    #include<iostream>    #include<cstdio>    #include<cstring>    #include<map>    #include<list>    #include<algorithm>    #include<vector>    #include<string>    #include<fstream>    //#pragma comment(linker,"/STACK:1024000000,1024000000")    using namespace std;    const int N=1e5+5;    const int MAXN=1e7+5;    int prime[MAXN/5];    bool vis[MAXN];    int cnt=0;    void getPrime()    {        for(int i=2;i<MAXN;i++)        {            if(!vis[i]) prime[++cnt]=i;            for(int j=1;j<=cnt;j++)            {                if(i*prime[j]>=MAXN) break;                vis[i*prime[j]]=1;                if(i%prime[j]==0) break;            }        }        prime[0]=cnt;    }    long long factor[200][2];    int fatCnt;    int getFactors(long long x)    {    fatCnt=0;    long long tmp=x;    for(int i=1;i<=cnt&&prime[i]<=tmp/prime[i];i++)    {    factor[fatCnt][1]=0;    if(tmp%prime[i]==0)    {    factor[fatCnt][0]=prime[i];    while(tmp%prime[i]==0)    {    factor[fatCnt][1]++;    tmp/=prime[i];    }    fatCnt++;    }    }    if(tmp!=1)    {    factor[fatCnt][0]=tmp;    factor[fatCnt++][1]=1;    }    return fatCnt;    }    long long n;    int main()    {        getPrime();    //    cout<<prime[cnt]<<endl;        int t,tt=0;        scanf("%d",&t);        while(t--)        {            scanf("%lld",&n);            getFactors(n);            long long ans=1;            for(int i=0;i<fatCnt;i++)            {                ans*=2*(factor[i][1]+1)-1;            }            printf("Case %d: %lld\n",++tt,(ans+1)/2);        }       return 0;    }
0 0
原创粉丝点击