zoukankan      html  css  js  c++  java
  • LightOJ1236 —— 唯一分解定理 + 最小公倍数

    题目链接:https://vjudge.net/problem/LightOJ-1236

    1236 - Pairs Forming LCM
    Time Limit: 2 second(s) Memory Limit: 32 MB

    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

    Output for Sample Input

    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

    题意:

    求1到n(1e14)之内,有多少对数(i,j),其中i<=j,使得LCM(i,j)= n,LCM为最小公倍数。

    题解:

    1.设pi为第i个质数。设两个数A、B,他们可表示为:A = p1^a1 * p2^a2…… ,B = p1^b1 * p2^b2……。

    那么他们的最小公倍数为:LCM(A, B) = p1^max(a1,b1) * p2^max(a2, b2)……

    2.对n进行质因数分解,得到: n = p1^c1 * p2^c2……。当 LCM(A, B) = n时, ci = max(ai, bi),即要么 ci = ai,要么ci = bi。

    3 当ci = ai时, bi的可选择范围为[0,ci]共ci+1种;同理当ci = bi时,ai也有ci+1种选择。但是 (ai=ci,bi=ci)被重复计算了一次,所以对于素数pi,总共有 2*ci+1种选择。所以,当不考虑A、B的大小时,总共有 ∏ 2*ci+1对(A,B),使得 LCM(A, B) = n。

    4.再考虑回A、B的大小限制,即A<=B,可知除了A = B = n时,其他的组合都出现了两次,即(A,B)和(B,A)都存在,而要门只需要A<=B的那一个。总的来说,最终有 ((∏ 2*ci+1)+1)/2对 (A,B)满足条件。

    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <vector>
     6 #include <cmath>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 using namespace std;
    13 typedef long long LL;
    14 const int INF = 2e9;
    15 const LL LNF = 9e18;
    16 const int MOD = 1e9+7;
    17 const int MAXN = 1e7+10;
    18 
    19 bool notprime[MAXN];
    20 int prime[MAXN/10];
    21 void getPrime()
    22 {
    23     memset(notprime, false, sizeof(notprime));
    24     notprime[0] = notprime[1] = true;
    25     prime[0] = 0;
    26     for (int i = 2; i<MAXN; i++)
    27     {
    28         if (!notprime[i])prime[++prime[0]] = i;
    29         for (int j = 1; j<=prime[0]&& prime[j]<MAXN/i; j++)
    30         {
    31             notprime[prime[j]*i] = true;
    32             if (i%prime[j] == 0) break;
    33         }
    34     }
    35 }
    36 
    37 int fatCnt;
    38 LL factor[1000][2];
    39 int getFactors(LL n)
    40 {
    41     LL tmp = n;
    42     fatCnt = 0;
    43     for(int i = 1; prime[i]<=tmp/prime[i]; i++)
    44     {
    45         if(tmp%prime[i]==0)
    46         {
    47             factor[++fatCnt][0] = prime[i];
    48             factor[fatCnt][1] = 0;
    49             while(tmp%prime[i]==0) tmp /= prime[i], factor[fatCnt][1]++;
    50         }
    51     }
    52     if(tmp>1) factor[++fatCnt][0] = tmp, factor[fatCnt][1] = 1;
    53 }
    54 
    55 int main()
    56 {
    57     getPrime();
    58     int T, kase = 0;
    59     scanf("%d", &T);
    60     while(T--)
    61     {
    62         LL n;
    63         scanf("%lld", &n);
    64         getFactors(n);
    65         LL sum = 1;
    66         for(int i = 1; i<=fatCnt; i++)
    67             sum *= 2*factor[i][1]+1;
    68 
    69         sum = (sum+1)/2;
    70         printf("Case %d: %lld
    ", ++kase, sum);
    71     }
    72 }
    View Code
  • 相关阅读:
    树莓派远程监控的实现
    frp内网渗透实现ssh外网访问家里树莓派(树莓派raspbian系统+腾讯云contos7)
    Linux下远程连接断开后如何让程序继续运行
    windows 远程连接登录树莓派桌面
    树莓派设置frpc开机启动
    树莓派 raspbian Linux 系统命令行 快捷键
    MyBatis如何防止SQL注入
    Apache POI导出excel表格
    SpringBoot文件上传
    SpringBoot整合定时任务
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/8371570.html
Copyright © 2011-2022 走看看