A - (例题)整数分解

Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Submit Status

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,让你寻找有多少i,j满足

lcm(i,j)=n&&1<=i<=j<=n

思路分析:首先n的范围十分大(1e14),暴力做肯定会超时,对于LCM,GCD,我们常考虑正整数唯一分解定理,

定理内容:对于任意一个大于1的数都可以唯一分解为若干个素数的乘积,即n=a1^b1*a2^b2*......an^bn;

我们先研究其中一个素因子a1,首先i和j唯一分解后肯定有a1^k(0~b1),同时又因为LCM(i,j)=n,则肯定有一个

数k=b1,可能的种数有(2*(b1+1)-1)(因为k1=b1&&k2=b1的情况多算了一次),由分步乘法技术原理可得

总共的可能性有t=2*b1+1)(2*b2+1)(2*b3+1)........,但是注意题目要求i<=j,i==j的情况只有可能有一种,那就是

i==j==n,由对称性,i<j的情况有(t-1)/2种,所以最后的答案就是(t+1)/2;

tip:正整数唯一分解需要进行两步 1.素数筛(到sqrt(n)即可) 2.枚举素数,进行唯一分解

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include<algorithm>
  5. #include <cmath>
  6. using namespace std;
  7. typedef long long ll;
  8. const int maxn=1e7+;//
  9. bool vis[maxn];
  10. ll prime[maxn/];
  11. int tot;
  12. /*void getprime()//因为n的范围是1e14,打表只需要打到sqrt(n)即可,最多只可能有一个素因子大于sqrt(n),最后特判一下即可;
  13. {
  14. memset(vis,true,sizeof(vis));
  15. tot=0;
  16. for(ll i=2;i<maxn;i++)
  17. {
  18. if(vis[i])
  19. {
  20. prime[tot++]=i;
  21. for(ll j=i*i;j<maxn;j+=i)
  22. {
  23. vis[j]=false;
  24. }
  25. }
  26. }
  27. }*/
  28. void Eulerprime()
  29. {
  30. memset(vis,true,sizeof(vis));
  31. int tot=;
  32. for(int i=;i<maxn;i++)
  33. {
  34. if(vis[i]) prime[tot++]=i;
  35. for(int j=;j<tot&&prime[j]*i<maxn;j++)
  36. {
  37. vis[i*prime[j]]=false;
  38. if(i%prime[j]==) break;
  39. }
  40. }
  41. }
  42. int a[],b[];
  43. int cnt=;
  44. void sbreak(ll n)//正整数唯一分解
  45. {
  46. memset(a,,sizeof(a));
  47. memset(b,,sizeof(b));
  48. cnt=;
  49. for(int i=;prime[i]*prime[i]<=n;i++)
  50. {
  51. if(n%prime[i]==)
  52. {
  53. a[cnt]=prime[i];
  54. while(n%prime[i]==)
  55. {
  56. b[cnt]++;
  57. n/=prime[i];
  58. }
  59. cnt++;
  60. }
  61. }
  62. if(n!=)
  63. {
  64. a[cnt]=n;
  65. b[cnt]=;
  66. cnt++;//为了使两种情况分解后素因子下标都是0~cnt-1;
  67. }
  68. }
  69. int kase;
  70. int main()
  71. {
  72. int T;
  73. ll n;
  74. Eulerprime();
  75. scanf("%d",&T);
  76. kase=;
  77. while(T--)
  78. {
  79. scanf("%lld",&n);
  80. sbreak(n);
  81. ll ans=;
  82. for(ll i=;i<cnt;i++)
  83. {
  84. ans*=(*b[i]+);
  85. }
  86. ans=(ans+)/;
  87. printf("Case %d: %lld\n",++kase,ans);
  88. }
  89. }

lightoj 1236 正整数唯一分解定理的更多相关文章

  1. hdu1215 正整数唯一分解定理应用

    B - (例题)因子和 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64 ...

  2. hdu4497 正整数唯一分解定理应用

    C - (例题)整数分解,计数 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65535KB    ...

  3. LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1236 Pairs Forming LCM Time Limit:2000MS     Memor ...

  4. LightOJ - 1236 (唯一分解定理)

    题意:求有多少对数对(i,j)满足lcm(i,j) = n,1<=i<=j, 1<=n<=1e14. 分析:根据整数的唯一分解定理,n可以分解为(p1^e1)*(p2^e2)* ...

  5. LightOJ 1341 Aladdin and the Flying Carpet(唯一分解定理)

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

  6. LightOJ - 1341 Aladdin and the Flying Carpet 唯一分解定理LightOJ 1220Mysterious Bacteria

    题意: ttt 组数据,第一个给定飞毯的面积为 sss,第二个是毯子的最短的边的长度大于等于这个数,毯子是矩形但不是正方形. 思路: 求出 sss 的所有因子,因为不可能是矩形,所以可以除以 222, ...

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

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

  8. LightOJ - 1341唯一分解定理

    唯一分解定理 先分解面积,然后除2,再减去面积%长度==0的情况,注意毯子不能是正方形 #include<map> #include<set> #include<cmat ...

  9. Aladdin and the Flying Carpet LightOJ 1341 唯一分解定理

    题意:给出a,b,问有多少种长方形满足面积为a,最短边>=b? 首先简单讲一下唯一分解定理. 唯一分解定理:任何一个自然数N,都可以满足:,pi是质数. 且N的正因子个数为(1+a1)*(1+a ...

随机推荐

  1. mybatis常用语句

    <trim>标签中,prefix 前缀,suffix后缀, suffixOverrides语句最后如果有指定符号,则去除此符号, prefixOverrides语句最前面如果有指定符号,则 ...

  2. Java开发环境安装

    一.安装JDK(java development kit) 下载地址:www.oracle.com/technetwork/java/javase/downloads 二.配置Java环境变量 1.J ...

  3. javascript学习教程之---如何从一个tab切换到banner幻灯片的转换2

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  4. Nginx的安装及反向代理设置

    因为项目的缘故,接触到了Nginx的安装和反向代理设置,和大家分享下. 一.Nginx的下载.安装cd /homewget http://nginx.org/download/nginx-1.0.5. ...

  5. git基础使用小记

    一.安装步骤省略二.运行“Git Bash“在打开的窗口中输入:ssh-keygen -t rsa -C "my@gmail.com" 会提示SSH Public Keys存放的位 ...

  6. C 带指针样式的时钟

    #include <stdio.h> #include <malloc.h>#include<graphics.h>#include<conio.h> ...

  7. iOS界面调试工具 Reveal-备用

    Reveal是一个iOS程序界面调试工具.使用Reveal,我们可以在iOS开发时动态地查看和修改应用程序的界面. 对于动态或复杂的交互界面,手写UI是不可避免的.通过Reveal,我们可以方便地调试 ...

  8. MTKdroidToolsV2.53 MTK安卓提取线刷资料的工具 使用教程

    备份的时候需插入1G以上内存卡,并确保机器电量充足. 机器需要root才能备份. 最新版本 支持大部分机型 一键root

  9. activity,view,window,windowmanager代码阅读总结及相互关系

    ActivityThread类:performLaunchActivity函数: activity.attach(appContext, this, getInstrumentation(), r.t ...

  10. (转) Linux的capability深入分析(2)

    一)capability的工具介绍   在我们的试验环境是RHEL6,libcap-2.16软件包中包含了相关的capability设置及查看工作,如下:   rpm -ql libcap-2.16- ...