题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695

题目解析:

Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y) = k.

题目又说a==c==1,所以就是求[1,b]与[1,d]中gcd等于k的个数,因为若gcd(x,y)==z,那么gcd(x/z,y/z)==1,又因为不是z的倍数的肯定不是,所以不是z的倍数的可以直接去掉,所以只要将b和d除以k,然后就转化成了求两个范围中互质的对数了。即求[1,b/k],与[1,d/k]中互质的数目,让b<d,又因为 (x=5, y=7) and (x=7, y=5) are considered to be the same.

所以先求[1,b/k]中互质的数目,即phi[1]+phi[2]+phi[3].....+phi[b/k](其中phi[i]为i的欧拉函数值),再从区间[b/k+1,d/k]枚举与区间[1,b/k]中互质的数目。其中求与区间[1,b/k]中互质的数目可以通过容斥原理求得与区间[1,b/k]中不互质的数目,相减便可以求得结果。这题折腾了一中午,一直TLE,代码在后面贴了,之后看大神的博客知道了哪里超时的原因,每个数的质因子可以在打表求欧拉函数的时候顺便求出来,一种哈希的思想,这样就不用在枚举的时候每一个数在求一遍他的质因子了,好题!

代码如下:(608ms)

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <algorithm>
  5. #include <queue>
  6. #include <math.h>
  7. using namespace std;
  8. typedef __int64 ll;
  9. ll sum,phi[];
  10. int cnt[][],f[],a,b,c,d,x;
  11. void init()
  12. {
  13. memset(f,,sizeof(f));
  14. for(int i=; i<=; i++)
  15. {
  16. phi[i]=;
  17. f[i]=;
  18. }
  19. phi[]=;
  20. for(int i=; i<=; i++)
  21. {
  22. if(!phi[i])
  23. {
  24. for(ll j=i; j<=; j+=i)
  25. {
  26. if(!phi[j]) phi[j]=j;
  27. phi[j]=phi[j]/i*(i-);
  28. cnt[j][f[j]++]=i;//算是哈希吧,很精辟啊,这种写法要学习
  29. }
  30. }
  31. phi[i]+=phi[i-];
  32. }
  33. }
  34. ll gcd(ll A,ll B)
  35. {
  36. return B==?A:gcd(B,A%B);
  37. }
  38. void dfs(ll now,ll num,ll lcm,ll &coun,int key)
  39. {
  40. lcm=cnt[key][now]/gcd(cnt[key][now],lcm)*lcm;
  41. if(num&)
  42. {
  43. coun+=b/lcm;
  44. }
  45. else
  46. {
  47. coun-=b/lcm;
  48. }
  49. for(ll i=now+; i<f[key]; i++)
  50. dfs(i,num+,lcm,coun,key);
  51. }
  52. int main()
  53. {
  54. int T,K=;
  55. init();
  56. scanf("%d",&T);
  57. while(T--)
  58. {
  59. scanf("%d%d%d%d%d",&a,&b,&c,&d,&x);
  60. if(x == || x > b || x > d)
  61. {
  62. printf("Case %d: 0\n",++K);
  63. continue;
  64. }
  65. b/=x;
  66. d/=x;
  67. sum=;
  68. if(b>d) swap(b,d);
  69. sum+=phi[b];
  70. for(int i=b+; i<=d; i++)
  71. {
  72. ll coun=;
  73. for(int j=; j<f[i]; j++)
  74. {
  75. dfs(j,,cnt[i][j],coun,i);
  76. }
  77. sum+=(b-coun);
  78. }
  79. printf("Case %d: %I64d\n",++K,sum);
  80. }
  81. return ;
  82. }

TLE的:(TLE了一中午 3000ms++)

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <algorithm>
  5. #include <queue>
  6. #include <math.h>
  7. using namespace std;
  8. typedef __int64 ll;
  9. ll a,b,c,d,x,sum,top,cnt[],we;//********
  10. int phi[],su[],prime[];
  11. void init()
  12. {
  13. we=;
  14. prime[we++]=;
  15. su[]=su[]=;
  16. su[]=;
  17. for(int i=; i<; i++)
  18. if(i%==) su[i]=;
  19. else su[i]=;
  20. double t=sqrt(*1.0);
  21. for(ll i=; i<=t; i++)
  22. {
  23. if(su[i])
  24. {
  25. for(ll j=i*i; j<; j=j+i)
  26. {
  27. su[j]=;
  28. }
  29. }
  30. }
  31. for(ll i=; i<=; i++)
  32. {
  33. if(su[i]) prime[we++]=i;
  34. }
  35. memset(phi,,sizeof(phi));
  36. phi[]=;
  37. for(ll i=; i<=; i++)
  38. {
  39. if(!phi[i])
  40. {
  41. for(ll j=i; j<=; j+=i)
  42. {
  43. if(!phi[j]) phi[j]=j;
  44. phi[j]=phi[j]/i*(i-);
  45. }
  46. }
  47. }
  48. }
  49. ll gcd(ll A,ll B)
  50. {
  51. return B==?A:gcd(B,A%B);
  52. }
  53. void dfs(ll now,ll num,ll lcm,ll &coun)
  54. {
  55. lcm=cnt[now]/gcd(cnt[now],lcm)*lcm;
  56. if(num&)
  57. {
  58. coun+=b/lcm;
  59. //printf("hsum======%I64d\n",sum);
  60. }
  61. else
  62. {
  63. coun-=b/lcm;
  64. }
  65. for(ll i=now+; i<top; i++)
  66. dfs(i,num+,lcm,coun);
  67. }
  68. void cal(ll key,ll &coun)
  69. {
  70. top=;
  71. ll KK=;
  72. for(ll i=prime[]; i<=key; i=prime[++KK])
  73. {
  74. if(key%i==)
  75. {
  76. cnt[top++]=i;
  77. key/=i;
  78. while(key%i==)
  79. key/=i;
  80. }
  81. }
  82. if(key!=)
  83. {
  84. cnt[top++]=key;
  85. }
  86. for(ll i=; i<top; i++)
  87. {
  88. dfs(i,,cnt[i],coun);
  89. }
  90. }
  91. int main()
  92. {
  93. int T,K=;
  94. init();
  95. scanf("%d",&T);
  96. while(T--)
  97. {
  98. scanf("%I64d%I64d%I64d%I64d%I64d",&a,&b,&c,&d,&x);
  99. if(x == || x > b || x > d)
  100. {
  101. printf("Case %d: 0\n",++K);
  102. continue;
  103. }
  104.  
  105. b/=x;
  106. d/=x;
  107. sum=;
  108. if(b>d) swap(b,d);
  109. //cout<<"b=="<<b<<" "<<"d=="<<d<<endl;
  110. for(int i=; i<=b; i++)
  111. {
  112. sum+=phi[i];
  113. }
  114. //cout<<"sumsss=="<<sum<<endl;
  115. for(ll i=b+; i<=d; i++)
  116. {
  117. if(su[i])
  118. {
  119. sum+=b;
  120. continue;
  121. }
  122. ll coun=;
  123. cal(i,coun);
  124. sum+=(b-coun);
  125. }
  126. printf("Case %d: %I64d\n",++K,sum);
  127. }
  128. return ;
  129. }

HDU1695:GCD(容斥原理+欧拉函数+质因数分解)好题的更多相关文章

  1. T^TOJ - 1251 - 。◕‿◕。TMD - 欧拉函数 - 质因数分解

    http://www.fjutacm.com/Problem.jsp?pid=1251 想了很久,一开始居然还直接枚举因子d,计算重复了. 首先你要找与n的最大公因子大于m的x的个数. \[\sum\ ...

  2. HDU1695 GCD (欧拉函数+容斥原理)

    F - GCD Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  3. HDU 1695 GCD (容斥原理+欧拉函数)

    题目链接 题意 : 从[a,b]中找一个x,[c,d]中找一个y,要求GCD(x,y)= k.求满足这样条件的(x,y)的对数.(3,5)和(5,3)视为一组样例 . 思路 :要求满足GCD(x,y) ...

  4. HDU 1695 GCD(欧拉函数+容斥原理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:x位于区间[a, b],y位于区间[c, d],求满足GCD(x, y) = k的(x, ...

  5. hdu 1695 GCD (欧拉函数、容斥原理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  6. HDU 1695 GCD (欧拉函数+容斥原理)

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

  7. hdu 1695 GCD(欧拉函数+容斥)

    Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD( ...

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

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

  9. hdu2588 GCD (欧拉函数)

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

随机推荐

  1. 【转自IT虾米网:www.itxm.net】外部应用和drools-wb6.1集成解决方案

    一.手把手教你集成外部应用和drools workbench6.1 1.         首先按照官方文档安装workbench ,我用的是最完整版的jbpm6-console的平台系统,里面既包含j ...

  2. Storm-源码分析-Streaming Grouping (backtype.storm.daemon.executor)

    executor在发送outbounding message的时候, 需要决定发送到next component的哪些tasks 这里就需要用到streaming grouping,   1. mk- ...

  3. [java] Unsupported major.minor version 51.0 错误解决方案

    jdk1.6工程中使用外部jar包中类出现:Unsupported major.minor version 51.0原因分析:出现上述错误是因为:外部jar包使用jdk1.7(jdk7)编译,而使用此 ...

  4. 虚幻4 - ARPG实战教程(第一季)

    在广受欢迎的的<虚幻4高速开发入门>视频教程之后.我收到了许多的反馈,当中大量的同学想要一个实战类的教程.于是,我花了一段时间准备之后,推出了新的一系列实战教程. 希望以深入浅出的方式.解 ...

  5. 墨卡托投影, GPS 坐标转像素, GPS 坐标转距离

    Before: 1. 研究的需要, 在 google map 上爬取了一些的静态卫星地图图片,每张图片的像素为 256*256 2. 通过 photshop 将这些地图碎片手动拼成了地图, 地图只是覆 ...

  6. PyQt4文件对话框QFileDialog

    文件对话框允许用户选择文件或文件夹,被选择的文件可进行读或写操作. #!/usr/bin/python # -*- coding: utf-8 -*- import sys from PyQt4 im ...

  7. 【Mysql】 case ... when ... 用法

    sql语句查询时给某个空字段赋值 SELECT CASE WHEN field= '' THEN ' WHEN fieldIS NULL THEN ' ELSE field END FROM tabl ...

  8. 1.Math函数对象

    // 属性 Math.E //自然对数的底数(2.718281828459045) Math.PI //圆周率(3.141592653589793) Math.LN2 //2的自然对数(0.69314 ...

  9. centos6.5安装sendmail

    1.下载安装sendEmail(下载绿色版,解压可直接使用) wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1. ...

  10. mysql客户端不能插入中文字符

    问题:输入中文报错:Incorrect string value 步骤: 1.查看MySQL编码设置 show variables like '%character%'; 2.重新设置编码(注意:ut ...