zoukankan      html  css  js  c++  java
  • Pairs Forming LCM

    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,问你有多少种组合

    根据 LCM=p1^r1 * p2^r2 * p3^r3 .....

    其中r1=max(a1,b1)  r2=max(a2,b2)....rn=max(an,bn)

    因此当r1=a1时,,b1的取值为(0,a1-1)(先假设a1!=b1) 一共有r1种,然后如果r1=b1,同理也有r1种

    因此一共有(2r1)种。如果a1=b1还会有一种,所以总共有2*r1+1种,所以先计算sum*=2*ans+1。(ans是每一步的质数的个数)

    到目前为止我们已经求出来了一共有多少种选择,,即sum种。2*ans+1是奇数,一共有sum对,应题目要求我们需要i<=j

    sum/2会保留整数,因此我们需要加一个 1

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    typedef long long ll;
    const int N=1E7+7;
    const int NN=1E6+7;
    bool p[N]={1,1,0};
    int prime[NN];
    int k=0; 
    void pre(){
        memset(prime,0,sizeof(prime));
        for(int i=2;i<=N;i++){
            if(p[i]==0){
                prime[k++]=i;
                for(int j=i+i;j<=N;j+=i)
                    p[j]=1;
            }
        }
        
    }    
    int main(){
        pre();
        int t;
        cin>>t;
        for(int ii=1; ii<=t; ii++){
            ll n;
            cin>>n;
            ll sum=1;
            for(int i=0;i<k&&prime[i] <=n;i++){
                int ans=0;
                if(n%prime[i]==0){
                    while(n%prime[i]==0){
                        ans++;
                        n/=prime[i];
                    }
                    sum*=(2*ans+1);
                }            
            }
            if(n>1) sum*=2+1;
            printf("Case %d: %lld
    ",ii,sum/2+1);  
        }
        
        return 0;
    } 
  • 相关阅读:
    LPTSTR、LPCSTR、LPCTSTR、LPSTR的意义
    字符,字节和编码
    堆和栈的区别
    js正则表达式限制文本框只能输入数字,小数点,英文字母
    WPF 中的 LinkButton
    WPF中DataGrid的应用
    C#获取web.config配置文件内容
    js中格式化时间字符串
    WPF中的用户控件(UserControl)
    VS2010安装其他版本framework的问题解决方案
  • 原文地址:https://www.cnblogs.com/Accepting/p/11343219.html
Copyright © 2011-2022 走看看