Co-prime

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3313    Accepted Submission(s): 1286

Problem Description
Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N.
Two integers are said to be co-prime or relatively prime if they have no common positive divisors other than 1 or, equivalently, if their greatest common divisor is 1. The number 1 is relatively prime to every integer.
 
Input
The first line on input contains T (0 < T <= 100) the number of test cases, each of the next T lines contains three integers A, B, N where (1 <= A <= B <= 1015) and (1 <=N <= 109).
 
Output
For each test case, print the number of integers between A and B inclusive which are relatively prime to N. Follow the output format below.
 
Sample Input
2
1 10 2
3 15 5
 
Sample Output
Case #1: 5
Case #2: 10

Hint

In the first test case, the five integers in range [1,10] which are relatively prime to 2 are {1,3,5,7,9}.

 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  1434 1502 4136 4137 4138 
思路:素数打表+容斥原理;
因为要求在[n,m]中与互质的数的个数。
先打表求素数,然后分解k,求出k由哪些素数组成,然后我们可以用容斥求出[n,m]中与k不互质的数,然后区间长度减下即可;
每个数的质因数个数不会超过20个。
  1. 1 #include<stdio.h>
  2. 2 #include<algorithm>
  3. 3 #include<iostream>
  4. 4 #include<stdlib.h>
  5. 5 #include<string.h>
  6. 6 #include<vector>
  7. 7 #include<queue>
  8. 8 #include<stack>
  9. 9 using namespace std;
  10. 10 long long gcd(long long n,long long m);
  11. 11 bool prime[100005];
  12. 12 int ans[100005];
  13. 13 int bns[100005];
  14. 14 int dd[100005];
  15. 15 typedef long long LL;
  16. 16 int main(void)
  17. 17 {
  18. 18 int i,j,k;
  19. 19 scanf("%d",&k);
  20. 20 int s;
  21. 21 LL n,m,x;
  22. 22 for(i=2; i<=1000; i++)
  23. 23 {
  24. 24 if(!prime[i])
  25. 25 {
  26. 26 for(j=i; i*j<=100000; j++)
  27. 27 {
  28. 28 prime[i*j]=true;
  29. 29 }
  30. 30 }
  31. 31 }
  32. 32 int cnt=0;
  33. 33 for(i=2; i<=100000; i++)
  34. 34 {
  35. 35 if(!prime[i])
  36. 36 {
  37. 37 ans[cnt++]=i;
  38. 38 }
  39. 39 }
  40. 40 for(s=1; s<=k; s++)
  41. 41 {
  42. 42 int uu=0;
  43. 43 memset(dd,0,sizeof(dd));
  44. 44 scanf("%lld %lld %lld",&n,&m,&x);
  45. 45 while(x>=1&&uu<cnt)
  46. 46 {
  47. 47 if(x%ans[uu]==0)
  48. 48 {
  49. 49 dd[ans[uu]]=1;
  50. 50 x/=ans[uu];
  51. 51 }
  52. 52 else
  53. 53 {
  54. 54 uu++;
  55. 55 }
  56. 56 }
  57. 57 int qq=0;
  58. 58 for(i=2; i<=100000; i++)
  59. 59 {
  60. 60 if(dd[i])
  61. 61 {
  62. 62 bns[qq++]=i;
  63. 63 }
  64. 64 }
  65. 65 if(x!=1)
  66. 66 bns[qq++]=x;
  67. 67 n--;
  68. 68
  69. 69 LL nn=0;
  70. 70 LL mm=0;
  71. 71 for(i=1; i<=(1<<qq)-1; i++)
  72. 72 {
  73. 73 int xx=0; LL sum=1;
  74. 74 int flag=0;
  75. 75 for(j=0; j<qq; j++)
  76. 76 {
  77. 77 if(i&(1<<j))
  78. 78 {
  79. 79 xx++;
  80. 80 LL cc=gcd(sum,bns[j]);
  81. 81 sum=sum/cc*bns[j];
  82. 82 if(sum>m)
  83. 83 {
  84. 84 flag=1;
  85. 85 break;
  86. 86 }
  87. 87 }
  88. 88 }
  89. 89 if(flag)
  90. 90 continue;
  91. 91 else
  92. 92 {
  93. 93 if(xx%2==0)
  94. 94 {
  95. 95 nn-=n/sum;
  96. 96 mm-=m/sum;
  97. 97 }
  98. 98 else
  99. 99 {
  100. 100 nn+=n/sum;
  101. 101 mm+=m/sum;
  102. 102 }
  103. 103 }
  104. 104 }m-=mm;n-=nn;
  105. 105 printf("Case #%d: ",s);
  106. 106 printf("%lld\n",m-n);
  107. 107 }
  108. 108 return 0;
  109. 109 }
  110. 110 long long gcd(long long n,long long m)
  111. 111 {
  112. 112 if(m==0)
  113. 113 return n;
  114. 114 else if(n%m==0)
  115. 115 return m;
  116. 116 else return gcd(m,n%m);
  117. 117 }
 

Co-prime(hdu4135)的更多相关文章

  1. [HDU4135]CO Prime(容斥)

    也许更好的阅读体验 \(\mathcal{Description}\) \(t\)组询问,每次询问\(l,r,k\),问\([l,r]\)内有多少数与\(k\)互质 \(0<l<=r< ...

  2. 【hdu4135】【hdu2841】【hdu1695】一类通过容斥定理求区间互质的方法

    [HDU4135]Co-prime 题意 给出三个整数N,A,B.问在区间[A,B]内,与N互质的数的个数.其中N<=10^9,A,B<=10^15. 分析 容斥定理的模板题.可以通过容斥 ...

  3. Java 素数 prime numbers-LeetCode 204

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

  4. Prime Generator

    Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate ...

  5. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  6. UVa 524 Prime Ring Problem(回溯法)

    传送门 Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbe ...

  7. Sicily 1444: Prime Path(BFS)

    题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...

  8. hdu 5901 count prime & code vs 3223 素数密度

    hdu5901题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5901 code vs 3223题目链接:http://codevs.cn/problem ...

  9. 最小生成树 prime zoj1586

    题意:在n个星球,每2个星球之间的联通需要依靠一个网络适配器,每个星球喜欢的网络适配器的价钱不同,先给你一个n,然后n个数,代表第i个星球喜爱的网络适配器的价钱,然后给出一个矩阵M[i][j]代表第i ...

  10. 最小生成树 prime poj1258

    题意:给你一个矩阵M[i][j]表示i到j的距离 求最小生成树 思路:裸最小生成树 prime就可以了 最小生成树专题 AC代码: #include "iostream" #inc ...

随机推荐

  1. 源码分析-Producer

    消息生产者的代码都在client模块中,相对于RocketMQ来讲,消息生产者就是客户端,也是消息的提供者. 方法和属性 主要方法介绍 //创建主题 void createTopic(final St ...

  2. 【STM8】外挂存储器W25Q16

    好像有几张图片被强制缩小了?看到这篇博客的人先对你们说声抱歉,我不知道怎么设置 文字就可以很长(文章宽度的全部),图片就只有文章宽度的2/3宽度 开新分页应该就是原始尺寸了,这点还是和大家说抱歉... ...

  3. webservice--常用注解

    定义说明书的显示方法1.@WebService(serviceName="PojoService", portName="PojoPort", name=&qu ...

  4. ubantu打开摄像头失败

    摘要-针对ubantu20 sudo apt install v4l-utils v4l2-ctl --list-devices - cv2 install on ubantu20```针对ubant ...

  5. GO 时间处理

    比较大小 比较大小 先把当前时间格式化成相同格式的字符串,然后使用time的Before, After, Equal 方法即可. time1 := "2015-03-20 08:50:29& ...

  6. list通过比较器进行排序

    Collections.sort(dataList,new Comparator<BaseTransitData>(){            public int compare(Bas ...

  7. jquery总结和注意事项

    1.关于页面元素的引用通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用dom ...

  8. RAC常见的宏

    1. RAC           作用:用来给某个对象的某个属性绑定信号,只要产生信号内容就会把内容给属性赋值            RAC(_label, text) = _textField.ra ...

  9. Linux安装软件出错

    1.Delta RPMs disabled because /usr/bin/applydeltarpm not installed. yum provides '*/applydeltarpm' # ...

  10. 如何优雅正确地通过interrupt方法中断线程

    为什么废弃Thread的stop函数? 简单来说就是stop方法中断线程太过暴力随意,且会是否线程持有的锁,会导致线程安全问题.还有可能导致存在需要被释放的资源得不到释放,引发内存泄露.所以用stop ...