C - (例题)整数分解,计数

Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

Given two positive integers G and L, could you tell me how many solutions of (x, y, z) there are, satisfying that gcd(x, y, z) = G and lcm(x, y, z) = L?
Note, gcd(x, y, z) means the greatest common divisor of x, y and z, while lcm(x, y, z) means the least common multiple of x, y and z.
Note 2, (1, 2, 3) and (1, 3, 2) are two different solutions.
 

Input

First line comes an integer T (T <= 12), telling the number of test cases.
The next T lines, each contains two positive 32-bit signed integers, G and L.
It’s guaranteed that each answer will fit in a 32-bit signed integer.
 

Output

For each test case, print one line with the number of solutions satisfying the conditions above.
 

Sample Input

2
6 72
7 33
 

Sample Output

72
0
题目大意:
给你两个数L,G,问你有多少有序数组(x,y,z)满足GCD(x,y,z)=G,LCM(x,y,z)=L,首先如果gcd(x,y,z)=G,
那么有gcd(x/G,y/G,z/G)=1(说明这三个数两两互素),此时应该满足lcm(x,y,z)=L/G,要求L/G为整数,则若L%G==0,则一定有解,(x,y,z都等于L/G即可)
反之无解
此时将L/G作正整数唯一分解,T=L/G=a1^b1*a2^b2*.......*an^bn,对于a1,要满足gcd(x/g,y/g,z/g)=1,a1^k则至少有一个k=0,同时
还得满足lcm(x/g,y/g,z/g)=l/g,则至少有一个k=b1,这样就有三种情况(0,0,b1)(b1,b1,0)(0,1~b1-1,b1)共有6+6(b1-1)=6*b1种,其他的同理
由分步乘法计数原理,最终答案为(6*b1)*(6*b2)*........(6*bn)
代码:
  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=2e5;//
  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=;
  16. for(ll i=;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=0;
  32. for(int i=2;i<maxn;i++)
  33. {
  34. if(vis[i]) prime[tot++]=i;
  35. for(int j=0;j<tot&&prime[j]*i<maxn;j++)
  36. {
  37. vis[i*prime[j]]=false;
  38. if(i%prime[j]==0) 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 pow_mod(int m,int n)
  70. {
  71. ll pw=;
  72. while(n)
  73. {
  74. if(n&) pw*=m;
  75. m*=m;
  76. n/=;
  77. }
  78. return pw;
  79. }
  80. int kase;
  81. int main()
  82. {
  83. int T;
  84. ll L,G;
  85. getprime();
  86. scanf("%d",&T);
  87. kase=;
  88. while(T--)
  89. {
  90. scanf("%lld%lld",&G,&L);
  91. if(L%G) {printf("0\n");continue;}
  92. ll n=L/G;
  93. sbreak(n);
  94. ll sum=;
  95. for(int i=;i<cnt;i++)
  96. {
  97. sum*=(*b[i]);
  98. }
  99. printf("%lld\n",sum);
  100. }
  101. }

hdu4497 正整数唯一分解定理应用的更多相关文章

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

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

  2. lightoj 1236 正整数唯一分解定理

    A - (例题)整数分解 Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB     6 ...

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

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

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

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

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

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

  6. 简单数论之整除&质因数分解&唯一分解定理

    [整除] 若a被b整除,即a是b的倍数,那么记作b|a("|"是整除符号),读作"b整除a"或"a能被b整除".b叫做a的约数(或因数),a ...

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

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

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

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

  9. hdu1215-七夕节-(埃氏筛+唯一分解定理)

    七夕节 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

随机推荐

  1. hdu 1711 Number Sequence(KMP模板题)

    我的第一道KMP. 把两个数列分别当成KMP算法中的模式串和目标串,这道题就变成了一个KMP算法模板题. #include<stdio.h> #include<string.h> ...

  2. Java系列--第四篇 基于Maven的SSME之发送邮件

    在系列第一篇中,使用的是mybatis得到了一个小小的项目,而该项目的用户对象是有邮件地址的,如果按照邮件地址给对方去一封邮件会不会更能体现针对性呢,所以,我在这篇准备加入发送邮件的功能,利用的就是s ...

  3. [Mugeda HTML5技术教程之11]Mugeda API简介

    一.API 概述 Mugeda API 提供了一个简单的,结构化的方法来实时动态管理Mugeda内容.它提供了一下方法: •访问Mugeda内容中的对象. •获取和设置对象属性,如位置.旋转.比例.不 ...

  4. 实例讲解MySQL联合查询

    好了终于贴完了MySQL联合查询的内容了,加上上一篇一共2篇,都是我转载的,实例讲解MySQL联合查询.那下面就具体讲讲简单的JOIN的用法了.首先我们假设有2个表A和B,他们的表结构和字段分别为: ...

  5. Mediawiki.org的PHP编码约定

    http://www.mediawiki.org/wiki/Manual:Coding_conventions/PHP assignment作为expression来用看起来像个错误(looks su ...

  6. iOS平台在ffmpeg中使用librtmp

    转载请注明出处:http://www.cnblogs.com/fpzeng/p/3202344.html 系统版本:OS X 10.8 一.在iOS平台上交叉编译librtmp librtmp lin ...

  7. CentOS下安装JDK6u21和设置环境变量bin文件

    1.先通过SSH登录到Linux系统中,通过SSH文件管理工具把Linux的JDK安装包上传到/home/acm/JavaTools/JDK目录: 2.进入/home/acm/JavaTools/JD ...

  8. hdu Examining the Rooms

    这道题的知识点第一次听说 ,就是应用斯特林数.题目的意思是给你房间数N,和最多能破门的个数,让你求能全部把房间打开的概率! a[i][j]=a[i-1][j-1]+(i-1)*a[i-1][j]; # ...

  9. 抗忙,,建个MAVEN的私服仓库-NEXUS

    公司最近需求越来越上轨道,MAVEN的私服仓库-NEXUS构架起来哟.. 参考文档URL: http://www.linuxidc.com/Linux/2011-07/39578p3.htm http ...

  10. Codeforces 437E The Child and Polygon

    http://codeforces.com/problemset/problem/437/E 题意:求一个多边形划分成三角形的方案数 思路:区间dp,每次转移只从一个方向转移(L,R连线的某一侧),能 ...