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)的情况得加上
 
  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<string.h>
  4. #include<stdlib.h>
  5. #include<algorithm>
  6.  
  7. using namespace std;
  8. const int N = 1e7 + ;
  9. typedef long long ll;
  10.  
  11. int prime[], k;
  12. bool Isprime[N];
  13.  
  14. void Prime()
  15. {
  16. k = ;
  17. memset(Isprime, true, sizeof(Isprime));
  18. Isprime[] = false;
  19. for(int i = ; i < N ; i++)
  20. {
  21. if(Isprime[i])
  22. {
  23. prime[k++] = i;
  24. for(int j = ; i * j < N ;j++)
  25. Isprime[i * j] = false;
  26. }
  27. }
  28. }//素数筛选
  29.  
  30. int main()
  31. {
  32. int t, p = ;
  33. ll n;
  34. Prime();
  35. scanf("%d", &t);
  36. while(t--)
  37. {
  38. p++;
  39. int x;
  40. ll ans = ;
  41. scanf("%lld", &n);
  42. for(int i = ; i < k && prime[i] * prime[i] <= n; i++)
  43. {
  44. x = ;
  45. if(n % prime[i] == )
  46. {
  47. while(n % prime[i] == )
  48. {
  49. x++;
  50. n /= prime[i];
  51. }
  52. }
  53. ans *= ( * x + );
  54. }
  55. if(n > )
  56. ans *= ;
  57. printf("Case %d: %lld\n", p, ans / + );
  58. }
  59. return ;
  60. }

LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)的更多相关文章

  1. LightOj 1236 Pairs Forming LCM (素数筛选&&唯一分解定理)

    题目大意: 有一个数n,满足lcm(i,j)==n并且i<=j时,(i,j)有多少种情况? 解题思路: n可以表示为:n=p1^x1*p2^x1.....pk^xk. 假设lcm(a,b) == ...

  2. LightOJ 1236 - Pairs Forming LCM(素因子分解)

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

  3. LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000 ...

  4. 1341 - Aladdin and the Flying Carpet ---light oj (唯一分解定理+素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1341 题目大意: 给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. ...

  5. LightOJ - 1236 - Pairs Forming LCM(唯一分解定理)

    链接: https://vjudge.net/problem/LightOJ-1236 题意: Find the result of the following code: long long pai ...

  6. LightOj 1236 - Pairs Forming LCM (分解素因子,LCM )

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1236 题意:给你一个数n,求有多少对(i,  j)满足 LCM(i, j) = n, ...

  7. LightOJ 1236 Pairs Forming LCM 合数分解

    题意:求所有小于等于n的,x,y&&lcm(x,y)==n的个数 分析:因为n是最小公倍数,所以x,y都是n的因子,而且满足这样的因子必须保证互质,由于n=1e14,所以最多大概在2^ ...

  8. LightOJ 1236 Pairs Forming LCM【整数分解】

    题目链接: http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1236 题意: 找与n公倍数为n的个数. 分析: ...

  9. LightOJ 1220 Mysterious Bacteria(唯一分解定理 + 素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1220 Mysterious Bacteria Time Limit:500MS     Memo ...

随机推荐

  1. R语言randomForest包实现随机森林——iris数据集和kyphosis数据集

    library(randomForest)model.forest<-randomForest(Species~.,data=iris)pre.forest<-predict(model. ...

  2. Java Socket 模拟HTTP请求

    public static void main(String[] args) { try { String url = "192.168.1.103"; Socket socket ...

  3. HDU 1015 Safecracker

    解题思路:这题相当诡异,样例没过,交了,A了,呵呵,因为理论上是可以通过的,所以 我交了一发,然后就神奇的过了.首先要看懂题目. #include<cstdio> #include< ...

  4. 【Django】Python虚拟环境工具virtualenv

    教程 第一步:安装virtualenv $pip install virtualenv 第二步:开启虚拟环境的python $cd ENV/Scripts $activate.bat #启用virtu ...

  5. 转深入学习heritrix---体系结构(Overview of the crawler)

    Heritrix采用了模块化的设计,它由一些核心类(core classes)和可插件模块(pluggable modules)构成.核心类可以配置,但不能被覆盖,插件模块可以被由第三方模块取代. ( ...

  6. 剑指offer-第三章高质量的代码(调整数组顺序使得奇数位于偶数的前面)

    题目:输入一个整数数组,实现一个函数中调整该数组中数字的顺序,使得所有的奇数位于数组的前半部,所有偶数位于数组的后半部. 思路:用两个指针p1和p2,分别指向数组的头和尾部,p1只向后移,p2只向前移 ...

  7. Android点击按钮实现全屏的代码

    package com.hsx.test; import java.lang.reflect.Field; import android.app.Activity; import android.os ...

  8. linux的命令(1)

    系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...

  9. 实现输出h264直播流的rtmp服务器

    RTMP(Real Time Messaging Protocol)是常见的流媒体协议,用来传输音视频数据,结合flash,广泛用于直播.点播.聊天等应用,以及pc.移动.嵌入式等平台,是做流媒体开发 ...

  10. Java 中无参无返回值方法的使用

    如果方法不包含参数,且没有返回值,我们称为无参无返回值的方法. 方法的使用分两步: 第一步,定义方法 例如:下面代码定义了一个方法名为 show ,没有参数,且没有返回值的方法,执行的操作为输出 “ ...