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,(1,2)=1,(12,18)=6. 
 (a,b) can be easily found by the Euclidean algorithm. Now Carp is considering a little more difficult problem: 
 Given integers N and M, how many integer X satisfies 1<=X<=N and (X,N)>=M.

InputThe first line of input is an integer T(T<=100) representing the number of test cases. The following T lines each contains two numbers N and M (2<=N<=1000000000, 1<=M<=N), representing a test case.OutputFor each test case,output the answer on a single line.Sample Input

  1. 3
  2. 1 1
  3. 10 2
  4. 10000 72

Sample Output

  1. 1
  2. 6
  3. 260

题意:求有多少个1<=X<=N,满足gcd(X,N)>=M。

思路:即求Σd=gcd(X,N)>=M;枚举d,而d是M的因子,不超过根号N个;对枚举的d,用欧拉公式求得有多少个X满足gcd(X,N)=d;

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn=;
  4. int p[maxn+],vis[maxn+],phi[maxn],cnt;
  5. void getprime()
  6. {
  7. phi[]=;
  8. for(int i=;i<=maxn;i++){
  9. if(!vis[i]) p[++cnt]=i,phi[i]=i-;
  10. for(int j=;j<=cnt&&i*p[j]<=maxn;j++){
  11. vis[i*p[j]]=; phi[i*p[j]]=phi[i]*(p[j]-);
  12. if(i%p[j]==){
  13. phi[i*p[j]]=phi[i]*p[j];
  14. break;
  15. }
  16. }
  17. }
  18. }
  19. int tot,ans,fac[maxn];
  20. void divide(int x)
  21. {
  22. for(int i=;i*i<=x;i++){
  23. if(x%i==){
  24. fac[++tot]=i;
  25. if(i*i!=x) fac[++tot]=x/i;
  26. }
  27. }
  28. }
  29. map<int,int>PHI;
  30. int getphi(int x)
  31. {
  32. if(x<=maxn) return phi[x];
  33. if(PHI[x]) return PHI[x];
  34. int res=x;
  35. for(int i=;i*i<=x;i++)
  36. if(x%i==) {
  37. res=res/i*(i-);
  38. while(x%i==) x/=i;
  39. }
  40. if(x>) res=res/x*(x-);
  41. PHI[x]=res;
  42. return res;
  43. }
  44. int main()
  45. {
  46. getprime();
  47. int T,N,M,i,j;
  48. scanf("%d",&T);
  49. while(T--){
  50. tot=ans=;
  51. scanf("%d%d",&N,&M);
  52. divide(N);
  53. for(i=;i<=tot;i++){
  54. if(N/fac[i]>=M) ans+=getphi(fac[i]);
  55. }
  56. printf("%d\n",ans);
  57. }
  58. return ;
  59. }

---------------------------------分界线--------------------------------

再来看HDU5514。。。(虽然复杂度看似有些巧合)。

只要枚举M的因子,然后验证如果是属于某个gcd(a,M)的倍数,就可以累加其所到位子,求和的时候利用对称性,有公式Σ=φ(x)*m/2;。

(容斥定理也可以做,但是我想不出来)。。。

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. const int maxn=;
  5. int p[maxn+],vis[maxn+],phi[maxn],cnt;
  6. void getprime()
  7. {
  8. phi[]=; phi[]=;
  9. for(int i=;i<=maxn;i++){
  10. if(!vis[i]) p[++cnt]=i,phi[i]=i-;
  11. for(int j=;j<=cnt&&i*p[j]<=maxn;j++){
  12. vis[i*p[j]]=; phi[i*p[j]]=phi[i]*(p[j]-);
  13. if(i%p[j]==){
  14. phi[i*p[j]]=phi[i]*p[j];
  15. break;
  16. }
  17. }
  18. }
  19. }
  20. int tot,fac[maxn];
  21. void divide(int x)
  22. {
  23. for(int i=;i*i<=x;i++){
  24. if(x%i==){
  25. fac[++tot]=i;
  26. if(i*i!=x) fac[++tot]=x/i;
  27. }
  28. }
  29. }
  30. int getphi(int x)
  31. {
  32. if(x<=maxn) return phi[x];
  33. int res=x;
  34. for(int i=;i*i<=x;i++)
  35. if(x%i==) {
  36. res=res/i*(i-);
  37. while(x%i==) x/=i;
  38. }
  39. if(x>) res=res/x*(x-);
  40. return res;
  41. }
  42. int a[maxn],N,M;
  43. bool check(int x)
  44. {
  45. for(int i=;i<=N;i++)
  46. if(x%a[i]==) return true;
  47. return false;
  48. }
  49. int main()
  50. {
  51. getprime();
  52. int T,Case=,i; ll ans;
  53. scanf("%d",&T);
  54. while(T--){
  55. tot=ans=;
  56. scanf("%d%d",&N,&M);
  57. for(i=;i<=N;i++){
  58. scanf("%d",&a[i]);
  59. a[i]=__gcd(a[i],M);
  60. }
  61. divide(M);
  62. for(i=;i<=tot;i++){
  63. if(fac[i]!=M&&check(fac[i])) ans+=(ll)getphi(M/fac[i])*M/;
  64. }
  65. printf("Case #%d: %lld\n",++Case, ans);
  66. }
  67. return ;
  68. }

从HDU2588:GCD 到 HDU5514:Frogs (欧拉公式)的更多相关文章

  1. HDU5514 Frogs

    /* HDU5514 Frogs http://acm.hdu.edu.cn/showproblem.php?pid=5514 容斥原理 * * */ #include <cstdio> ...

  2. hdu2588 GCD (欧拉函数)

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

  3. HDU2588 GCD(欧拉函数)

    题目问[1,n]中与n的gcd大于等于m的数的个数. 好难想... 假设x满足条件,那么gcd(x,n)=d>=m,而x/d与n/d一定互质. 又x<=n,所以x/d<=n/d. 于 ...

  4. hdu2588 gcd 欧拉函数

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

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

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

  6. hdu2588 GCD

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

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

    GCD Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepted Sub ...

  8. 【做题】hdu5514 Frogs——另类容斥

    题意是给出n个m的约数,问[0,m-1]中至少被其中一个约数整除的整数和.(n<=10000,m<=1000000000) 直接容斥的话,是2^n再拖个log的复杂度,加上当前的数大于m时 ...

  9. hdu 5514 Frogs 容斥思想+gcd 银牌题

    Frogs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

随机推荐

  1. try...cath...finally中的return什么时候执行

    一finally可以没有,也可以只有一个.无论有没有发生异常,它总会在这个异常处理结构的最后运行.即使你在try块内用return返回了,在返回前,finally总是要执行,这以便让你有机会能够在异常 ...

  2. 2.8 The Object Model -- Enumerables

    在Ember.js中,枚举是包含许多子对象的任何对象,并允许你使用Ember.Enumerable API和那些子对象一起工作.在大部分应用程序中最常见的可枚举是本地JS数组,Ember.js扩展到符 ...

  3. RabittMQ实践(一): RabbitMQ的安装、启动

    安装:   启动监控管理器:rabbitmq-plugins enable rabbitmq_management 关闭监控管理器:rabbitmq-plugins disable rabbitmq_ ...

  4. 470. Implement Rand10() Using Rand7() (拒绝采样Reject Sampling)

    1. 问题 已提供一个Rand7()的API可以随机生成1到7的数字,使用Rand7实现Rand10,Rand10可以随机生成1到10的数字. 2. 思路 简单说: (1)通过(Rand N - 1) ...

  5. windows 系统相关配置

    1. 外接显示器分辨率调节:连接上外接下显示器,在本机空白处,右键,分辨率.然后选择显示器,设置显示相关配置. 详见:http://zhidao.baidu.com/question/13494806 ...

  6. Win10下安装Go开发环境

    关于Go语言有多么值得学习,这里就不做介绍了,既然看了这篇文章,想必是对有学习意向. 这里是在Windows下安装Go环境,去中文社区的下载栏目,https://studygolang.com/dl ...

  7. 一个好玩的CTF题

    一个CTF的题目,拿来学习学习 玩了好久,再加上学校一堆破事,最近又开始瞎弄了,找了几个CTF的题目,和别人写的一些内容,也当是学习,也当是看完之后的小结.顺便也说一下如果自己拿到这题目会从哪做起. ...

  8. openwrt编译系统生成ubi镜像的各变量解析

    1.MKUBIFS_OPTS的作用 传递参数给mkfs.ubifs 2.MKUBIFS_OPTS传递了哪些参数? 传递了最小输入输出单元大小.逻辑擦除块大小.最大物理擦除块的个数,分别由选项-m.-e ...

  9. UVA 11019 Matrix Matcher(二维hash + 尺取)题解

    题意:在n*m方格中找有几个x*y矩阵. 思路:二维hash,总体思路和一维差不太多,先把每行hash,变成一维的数组,再对这个一维数组hash变成二维hash.之前还在想怎么快速把一个矩阵的hash ...

  10. System.Data.SQLite未能加载文件或程序集

    1.简直是作死帝呀.不需要修改dll的名字,否则就坐等悲剧吧 如果项目中有x86和x64的dll,可以建两个不同的文件夹分别存放,但是千万不要修改掉默认的dll的名字 System.Data.SQLi ...