题目链接: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+;
  17. const int MAXN = 1e7+;
  18.  
  19. bool notprime[MAXN];
  20. int prime[MAXN/];
  21. void getPrime()
  22. {
  23. memset(notprime, false, sizeof(notprime));
  24. notprime[] = notprime[] = true;
  25. prime[] = ;
  26. for (int i = ; i<MAXN; i++)
  27. {
  28. if (!notprime[i])prime[++prime[]] = i;
  29. for (int j = ; j<=prime[]&& prime[j]<MAXN/i; j++)
  30. {
  31. notprime[prime[j]*i] = true;
  32. if (i%prime[j] == ) break;
  33. }
  34. }
  35. }
  36.  
  37. int fatCnt;
  38. LL factor[][];
  39. int getFactors(LL n)
  40. {
  41. LL tmp = n;
  42. fatCnt = ;
  43. for(int i = ; prime[i]<=tmp/prime[i]; i++)
  44. {
  45. if(tmp%prime[i]==)
  46. {
  47. factor[++fatCnt][] = prime[i];
  48. factor[fatCnt][] = ;
  49. while(tmp%prime[i]==) tmp /= prime[i], factor[fatCnt][]++;
  50. }
  51. }
  52. if(tmp>) factor[++fatCnt][] = tmp, factor[fatCnt][] = ;
  53. }
  54.  
  55. int main()
  56. {
  57. getPrime();
  58. int T, kase = ;
  59. scanf("%d", &T);
  60. while(T--)
  61. {
  62. LL n;
  63. scanf("%lld", &n);
  64. getFactors(n);
  65. LL sum = ;
  66. for(int i = ; i<=fatCnt; i++)
  67. sum *= *factor[i][]+;
  68.  
  69. sum = (sum+)/;
  70. printf("Case %d: %lld\n", ++kase, sum);
  71. }
  72. }

LightOJ1236 —— 唯一分解定理 + 最小公倍数的更多相关文章

  1. Uva 10791 最小公倍数的最小和 唯一分解定理

    题目链接:https://vjudge.net/contest/156903#problem/C 题意:给一个数 n ,求至少 2个正整数,使得他们的最小公倍数为 n ,而且这些数之和最小. 分析: ...

  2. LightOJ-1236 Pairs Forming LCM 唯一分解定理

    题目链接:https://cn.vjudge.net/problem/LightOJ-1236 题意 给一整数n,求有多少对a和b(a<=b),使lcm(a, b)=n 注意数据范围n<= ...

  3. NOIP2009Hankson 的趣味题[唯一分解定理|暴力]

    题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现 在,刚刚放学回家的 Hankson 正在思考一个有趣的问题. 今天在课堂上,老师讲 ...

  4. UVa 10791 Minimum Sum LCM【唯一分解定理】

    题意:给出n,求至少两个正整数,使得它们的最小公倍数为n,且这些整数的和最小 看的紫书--- 用唯一分解定理,n=(a1)^p1*(a2)^p2---*(ak)^pk,当每一个(ak)^pk作为一个单 ...

  5. 唯一分解定理(以Minimun Sum LCM UVa 10791为例)

    唯一分解定理是指任何正整数都可以分解为一些素数的幂之积,即任意正整数n=a1^p1*a2^p2*...*ai^pi:其中ai为任意素数,pi为任意整数. 题意是输入整数n,求至少2个整数,使得它们的最 ...

  6. hdu4497-GCD and LCM-(欧拉筛+唯一分解定理+组合数)

    GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total ...

  7. Uva10791 唯一分解定理模板

    唯一分解定理: Uva10791 题意: 输入整数n,要求至少两个正整数,使得他们的最小公倍数为n,且这些整数的和最小 解法: 首先假设我们知道了一系列数字a1,a2,a3……an,他们的LCM是n, ...

  8. UVA10791-Minimum Sum LCM(唯一分解定理基本应用)

    原题:https://vjudge.net/problem/UVA-10791 基本思路:1.借助唯一分解定理分解数据.2.求和输出 知识点:1.筛法得素数 2.唯一分解定理模板代码 3.数论分析-唯 ...

  9. UVA - 10375 Choose and divide[唯一分解定理]

    UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

随机推荐

  1. java 判断请求来自手机端还是电脑端

    根据当前请求的特征,判断该请求是否来自手机终端,主要检测特殊的头信息,以及user-Agent这个header public static boolean isMobileDevice(HttpSer ...

  2. upper_bound——自己的实现

    int BSearch() { int ln(1),rn(n+1); while(ln+1<rn) { int mid=(ln+rn)>>1; if (Check(mid)) { l ...

  3. P2P技术简介(包括BT软件的分析)(转)

    这是一篇别人发表的论文,里面很全面的解释了P2P技术的实现,以及BT网络中应用P2P技术所设计的原理,并列举BT软件的一些专业名词的定义.由于论文发表的比较早,2005年时还没有DHT技术. (链接: ...

  4. spring管理事务

    2.1 事务管理器 Spring并不直接管理事务,而是提供了多种事务管理器,他们将事务管理的职责委托给Hibernate或者JTA等持久化机制所提供的相关平台框架的事务来实现. Spring事务管理器 ...

  5. spring-boot实战【01】:Hello World项目搭建

    原文:http://www.cnblogs.com/yucongblog/p/7355519.html 简介 在您第1次接触和学习Spring框架的时候,是否因为其繁杂的配置而退却了?在你第n次使用S ...

  6. 7.【nuxt起步】-Nuxt与后端数据交互

    接下来就是对接服务端接口,展示真实的数据 1.做了个虚拟接口地址:http://test.yms.cn/testjson.asp 输出数据: { "title": "单间 ...

  7. 赞一下TMS Software 和 AdvStringGrid

    非常久前给Support发Email问能不能在设计期给AdvStringGrid标题加个数字标识.每次我都是自己改代码加上去.这次升级到新版本号,没想到增加了这个功能: 功能虽小可是非常有用,非常多的 ...

  8. Win7如何自定义鼠标右键菜单 添加新建EXCEL文档

    鼠标右键添加新建EXCEL文档.reg Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\.xls] "Content Type ...

  9. 怎样制作gif图片?怎样制作你项目的动态效果图到你的csdn?

    怎样制作gif图?怎样上传你项目的动态效果图到你的csdn? 这仅仅是笔者用的方法.有其它方法的欢迎分享. 一张或几张展示了你的项目的功能及效果的动态图放在博客文章开头会为你的文章润色不少. 相信非常 ...

  10. spring 拦截器拦截点的配置

    实用正则org.springframework.aop.support.RegexpMethodPointcutAdvisor 然后 <property name="advice&qu ...