1. GCD
  2. Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others)
  3. Total Submission(s): Accepted Submission(s):
  4.  
  5. Problem Description
  6. The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the largest divisor common to a and b,For example,(,)=,(,)=.
  7. (a,b) can be easily found by the Euclidean algorithm. Now Carp is considering a little more difficult problem:
  8. Given integers N and M, how many integer X satisfies <=X<=N and (X,N)>=M.
  9.  
  10. Input
  11. The first line of input is an integer T(T<=) representing the number of test cases. The following T lines each contains two numbers N and M (<=N<=, <=M<=N), representing a test case.
  12.  
  13. Output
  14. For each test case,output the answer on a single line.
  15.  
  16. Sample Input
  17.  
  18. Sample Output
  19.  
  20. 欧拉函数做法:
  21. /**
  22. 题目:GCD
  23. 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2588
  24. 题意:给定n,m。求x属于[1,n]。有多少个x满足gcd(x,n)>=m;
  25. 思路:
  26. x -> [1,n]
  27.  
  28. d = gcd(x,n) >= m
  29.  
  30. d肯定为n的约数。
  31.  
  32. 对一个确定的d = gcd(x,n);
  33.  
  34. 那么:gcd(x/d,n/d) = 1;
  35.  
  36. 满足上面式子的x为:f(n/d); f(y)表示y的欧拉函数。
  37.  
  38. sigma(f(n/d)) (d为n的约数且d>=m);
  39.  
  40. f(y) = y*(p1-1)/p1*(p2-1)/p2...*(pe-1)/pe;
  41.  
  42. */
  43. #include<iostream>
  44. #include<cstring>
  45. #include<algorithm>
  46. #include<cstdio>
  47. #include<vector>
  48. #include<map>
  49. #include<set>
  50. #include<cmath>
  51. #include<queue>
  52. #define LL long long
  53. using namespace std;
  54. typedef long long ll;
  55. typedef unsigned long long ull;
  56. ll Euler(ll x)
  57. {
  58. ll n = x;
  59. for(int i = ; i*i<=x; i++){
  60. if(x%i==){
  61. n = n/i*(i-);
  62. while(x%i==)x/=i;
  63. }
  64. }
  65. if(x>){
  66. n = n/x*(x-);
  67. }
  68. return n;
  69. }
  70. vector<int> v;
  71. int main()
  72. {
  73. int T;
  74. int n, m;
  75. cin>>T;
  76. while(T--)
  77. {
  78. scanf("%d%d",&n,&m);
  79. v.clear();
  80. for(int i = ; i*i<=n; i++){
  81. if(n%i==){
  82. if(i*i==n){
  83. if(i>=m) v.push_back(i);
  84. }
  85. else{
  86. if(i>=m) v.push_back(i);
  87. if(n/i>=m) v.push_back(n/i);
  88. }
  89. }
  90. }
  91. int len = v.size();
  92. int cnt = ;
  93. for(int i = ; i < len; i++){
  94. cnt += Euler(n/v[i]);
  95. }
  96. printf("%d\n",cnt);
  97. }
  98. return ;
  99. }
  100.  
  101. 容斥做法:
  102. /**
  103. 题目:GCD
  104. 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2588
  105. 题意:给定n,m。求x属于[1,n]。有多少个x满足gcd(x,n)>=m;
  106. 思路:
  107.  
  108. 显然d = gcd(x,n)中的d一定是n的约数。
  109.  
  110. 显然d = gcd(d,n); 先获得所有>=m的d;
  111.  
  112. 那么d的倍数为x=k*d,如果小于等于n,则一定也满足gcd(x,n)>=m;
  113.  
  114. k = n/d; 如果对每个d这样计算,会有重复的计算。
  115.  
  116. 当d = 2, 3时候,x=6会多计算一次。
  117.  
  118. 所以要对所有的d进行容斥处理。
  119.  
  120. 问题转化为:n的约数为d,求解d>=m的所有的d在n范围内至少有一个是d的倍数的数有多少个。
  121.  
  122. */
  123. #include<iostream>
  124. #include<cstring>
  125. #include<algorithm>
  126. #include<cstdio>
  127. #include<vector>
  128. #include<map>
  129. #include<set>
  130. #include<cmath>
  131. #include<queue>
  132. #define LL long long
  133. using namespace std;
  134. typedef long long ll;
  135. typedef unsigned long long ull;
  136. vector<int> v;
  137. int a[], z; ///注意a数组要开大些,约数个数还是不是100就够的。
  138. ll gcd(ll a,ll b)
  139. {
  140. return b==?a:gcd(b,a%b);
  141. }
  142. ll rc(ll n)
  143. {
  144. ll sum = ;
  145. ll mult;
  146. int ones;
  147. int len = v.size();
  148. int m = (<<len);
  149. //奇加偶减
  150. for(int i = ; i < m; i++){
  151. ones = ;
  152. mult = ;
  153. for(int j = ; j<len; j++){
  154. if(i&(<<j)){
  155. ones++;
  156. mult = mult/gcd(mult,v[j])*v[j];
  157. if(mult>n) break;
  158. }
  159. }
  160. if(ones%==){
  161. sum -= n/mult;
  162. }else
  163. {
  164. sum += n/mult;
  165. }
  166. }
  167. return sum;
  168. }
  169. int main()
  170. {
  171. int T;
  172. int n, m;
  173. cin>>T;
  174. while(T--)
  175. {
  176. scanf("%d%d",&n,&m);
  177. v.clear();
  178. z = ;
  179. for(int i = ; i*i<=n; i++){
  180. if(n%i==){
  181. if(i*i==n){
  182. if(i>=m) a[z++] = i;
  183. }
  184. else{
  185. if(i>=m) a[z++] = i;
  186. if(n/i>=m) a[z++] = n/i;
  187. }
  188. }
  189. }
  190. ///出去包含的,比如2,4那么4要去掉。以为4的倍数一定是2的倍数。
  191. sort(a,a+z);
  192. for(int i = ; i < z; i++){
  193. int sign = ;
  194. for(int j = ; j < i; j++){
  195. if(a[i]%a[j]==){
  196. sign = ; break;
  197. }
  198. }
  199. if(sign==){
  200. v.push_back(a[i]);
  201. }
  202. }
  203. printf("%lld\n",rc(n));
  204. }
  205. return ;
  206. }

hdu2588 GCD 给定n,m。求x属于[1,n]。有多少个x满足gcd(x,n)>=m; 容斥或者欧拉函数的更多相关文章

  1. 【hdu-2588】GCD(容斥定理+欧拉函数+GCD()原理)

    GCD Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissio ...

  2. hdoj 1787 GCD Again【欧拉函数】

    GCD Again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. BZOJ2818: Gcd 欧拉函数求前缀和

    给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 如果两个数的x,y最大公约数是z,那么x/z,y/z一定是互质的 然后找到所有的素数,然后用欧拉函数求一 ...

  4. Trees in a Wood. UVA 10214 欧拉函数或者容斥定理 给定a,b求 |x|<=a, |y|<=b这个范围内的所有整点不包括原点都种一棵树。求出你站在原点向四周看到的树的数量/总的树的数量的值。

    /** 题目:Trees in a Wood. UVA 10214 链接:https://vjudge.net/problem/UVA-10214 题意:给定a,b求 |x|<=a, |y|&l ...

  5. hdu2588 GCD (欧拉函数)

    GCD 题意:输入N,M(2<=N<=1000000000, 1<=M<=N), 设1<=X<=N,求使gcd(X,N)>=M的X的个数.  (文末有题) 知 ...

  6. HDU2588:GCD(欧拉函数的应用)

    题目链接:传送门 题目需求:Given integers N and M, how many integer X satisfies 1<=X<=N and (X,N)>=M.(2& ...

  7. (hdu step 7.2.2)GCD Again(欧拉函数的简单应用——求[1,n)中与n不互质的元素的个数)

    题目: GCD Again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. hdu2588 gcd 欧拉函数

    GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  9. BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 4436  Solved: 1957[Submit][Status][Discuss ...

随机推荐

  1. Http报头Accept与Content-Type的区别(转)

    1.Accept属于请求头, Content-Type属于实体头. Http报头分为通用报头,请求报头,响应报头和实体报头. 请求方的http报头结构:通用报头|请求报头|实体报头 响应方的http报 ...

  2. MySQL查询时区分大小写(转)

    说明:在MySQL查询时要区分大小写会涉及到两个概念character set和collation,这两个概念在表设计时或者在查询时都可以指定的,详细参考:http://www.cnblogs.com ...

  3. x-forwarded-for之深度挖掘

    如今利用nginx做负载均衡的实例已经很多了,针对不同的应用场合,还有很多需要注意的地方,本文要说的就是在通过CDN 后到达nginx做负载均衡时请求头中的X-Forwarded-For项到底发生了什 ...

  4. Calendar抽象类返回自己和Integer.TYPE和int.class

    public class Calend { public static void main(String[] args) { Calendar cal=Calendar.getInstance();/ ...

  5. Android App引导页这些坑你自己犯过吗?

    场景:測试机:华为荣耀6x 今天我自己掉入一个非常蠢蠢的坑,一个引导页搞了20多分钟.无论我怎么測试用真机还是模拟器都无法执行,可是我写的demo全然没问题,好无语,我都怀疑我是不是搞android, ...

  6. qml自学笔记------自己写相似于劲舞团的按键小游戏(中)

    接上篇<qml自学笔记------自己写类似于劲舞团的按键小游戏(上)> 第三部分DisplayPart.qml 代码的其它部分都是渣,就这里花了点时间,整个小游戏就靠这个文件. 首先,屏 ...

  7. docker入门——简介

    从这里起航 本系列有感于<第一本Docker书>,当我拿到这本书时感觉如获至宝. 为了培养自己对docker的兴趣,不断鞭策自己,我决定开始写这个系列的博客——<站在蓝鲸的背上思考& ...

  8. Python学习笔记(四)多进程的使用

    python中多进程与Linux 下的C基本相同.   fork的基本使用   先看最简单的例子: # coding: utf-8 import os def my_fork(): pid = os. ...

  9. css选择器和xpath对照表

  10. Spring搭配Ehcache实例解析

    转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/50538085 本文出自[我是干勾鱼的博客] 1 Ehcache简单介绍 EhCa ...