zoukankan      html  css  js  c++  java
  • LightOJ 1236

    B - Pairs Forming LCM
    Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

    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

    题意 给你一个数n 求满足lcm(a, b) == n, a <= b 的 (a,b) 的个数

    容易知道 n 是a, b的所有素因子取在a, b中较大指数的积

    先将n分解为素数指数积的形式 n = π(pi^ei) 那么对于每个素因子pi pi在a,b中的指数ai, bi 至少有一个等于pi, 另一个小于等于pi

    先不考虑a, b的大小 对于每个素因子pi

    1. 在a中的指数 ai == ei 那么 pi 在 b 中的指数可取 [0, ei] 中的所有数 有 ei + 1 种情况

    2. 在a中的指数 ai < ei 即 ai 在 [0, ei) 中 那么 pi 在 b 中的指数只能取 ei 有 ei 种情况

    那么对于每个素因子都有 2*ei + 1种情况 也就是满足条件的 (a, b) 有π(2*ei + 1)个 考虑大小时除了 (n, n) 所有的情况都出现了两次 那么满足a<=b的有(π(2*ei + 1)) / 2 + 1

    #include <iostream>
    #include <cstdio>
    using namespace std;
    typedef long long ll;
    const int maxn=1e7+5;//这里不能用typedef 1e7
    int prime[maxn/10]; //这里一定要除以10 否则会超内存 一共664579个素数
    int cnt=0;
    bool isprime[maxn];
    void getprime() //素筛打表
    {
        for(int i=2;i<=maxn;i++)
        {
            if(!isprime[i])
            {
                prime[cnt++]=i;
                for(int j=i+i;j<=maxn;j+=i)
                    isprime[j]=1;
            }
        }
    }
    int main()
    {
        getprime(); //先get素数表
        //cout<<cnt<<endl;
        int t,cas=1;
        cin>>t;
        while(t--)
        {
            ll n,sum,ans=1;
            cin>>n;
            for(int i=0;i<cnt;i++)
            {
                sum=0;
                if(prime[i]*prime[i]>n) //记得break
                break;
                if(n%prime[i]==0)
                {
                    while(n%prime[i]==0)
                    {
                        n/=prime[i];
                        sum++;
                    }
                ans=ans*(sum*2+1);
                }
            }
            if(n>1) ans*=3;
            ans=ans/2+1;
            printf("Case %d: %lld
    ",cas++,ans);
        }
        return 0;
    }
  • 相关阅读:
    第一次作业-准备篇
    个人作业——软件工程实践总结
    团队作业第二次—项目选题报告
    软工实践第三次作业(结对第二次作业)
    软工实践第二次作业(结对第一次作业)
    第一次作业-准备篇
    软工实践|个人作业——软件工程实践总结作业
    软工实践|团队作业第二次—项目选题报告
    软工实践|结对第二次—文献摘要热词统计及进阶需求
    软工实践|结对第一次—原型设计(文献摘要热词统计)
  • 原文地址:https://www.cnblogs.com/Ritchie/p/5296122.html
Copyright © 2011-2022 走看看