zoukankan      html  css  js  c++  java
  • LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1236

    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(i, j) = n, (i, j)这样的数对有多少种,其中i<=j;(lcm(i, j)表示i,j的最小公倍数)

    前几天才写得唯一分离定理的,然而并没有想到这道题与唯一分离定理有什么关联(问了学姐才知道),还是定理没有理解透彻,唉~

    求约数,倍数,质因数,gcd,lcm,都应该想到这个定理的

    算术基本定理(唯一分离定理)的内涵用一句话来概括就是:

    一个数的每一个质因子的不同幂对应不同的因数。

     我们可以利用唯一分离定理:
    n = p1^x1*p2^x2*p3^x3*...*ps^xs;
    n = lcm(i, j);
    假设n = p1^x1;那么i、j有两种:
    (1)i = p1^x1,则 j = p1^m(m属于[0,x1]),  这样(i,j)共有  (x1 + 1)种
    (2)j = p1^x1,则 i = p1^n(n属于[0,x1]),  这样(i,j)共有  (x1 + 1)种
    那么总共就有ans = 2*(x1 + 1)种,又因为当m = n时(1)和(2)这两种情况是一样的,所以最终总情况ans-1,即ans = 2*(x1 + 1) - 1 = 2*x1 + 1
    当n = p1^x1*p2^x2*p3^x3*...*ps^xs时总情况ans = (2*x1+1)*(2*x2+1)*(2*x3+1)*...*(2*xs+1);
    上面求的ans是i>j和i<j都可以即(i,j)和(j,i)重复了(除了(n,n)只算了一种),而题中求的是i<=j,所以ans /= 2;
    还有一种(n,n)的情况得加上
     
    #include<stdio.h>
    #include<math.h>
    #include<string.h>
    #include<stdlib.h>
    #include<algorithm>
    
    using namespace std;
    const int N = 1e7 + 10;
    typedef long long ll;
    
    int prime[700010], k;
    bool Isprime[N];
    
    void Prime()
    {
        k = 0;
        memset(Isprime, true, sizeof(Isprime));
        Isprime[1] = false;
        for(int i = 2 ; i < N ; i++)
        {
            if(Isprime[i])
            {
                prime[k++] = i;
                for(int j = 2 ; i * j < N ;j++)
                    Isprime[i * j] = false;
            }
        }
    }//素数筛选
    
    int main()
    {
        int t, p = 0;
        ll n;
        Prime();
        scanf("%d", &t);
        while(t--)
        {
            p++;
            int x;
            ll ans = 1;
            scanf("%lld", &n);
            for(int i = 0 ; i < k && prime[i] * prime[i] <= n; i++)
            {
                x = 0;
                if(n % prime[i] == 0)
                {
                    while(n % prime[i] == 0)
                    {
                        x++;
                        n /= prime[i];
                    }
                }
                ans *= (2 * x + 1);
            }
            if(n > 1)
                ans *= 3;
            printf("Case %d: %lld
    ", p, ans / 2 + 1);
        }
        return 0;
    }
  • 相关阅读:
    SpringBoot获取配置文件,就这么简单。
    IDEA 插件推荐 —— 让你写出好代码的神器!
    太高效了!玩了这么久的Linux,居然不知道这7个终端快捷键!
    万字长文!一次性弄懂 Nginx 处理 HTTP 请求的 11 个阶段
    一个排查了大半天儿的问题,差点又让 MyBatis 背锅
    使用Flutter开发的抖音国际版
    一文回顾Redis五大对象(数据类型)
    Gradle系列之初识Gradle
    OpenJFX DJ 风格 Java 桌面音乐播放器
    【高并发】高并发秒杀系统架构解密,不是所有的秒杀都是秒杀!
  • 原文地址:https://www.cnblogs.com/qq2424260747/p/4936943.html
Copyright © 2011-2022 走看看