http://acm.hdu.edu.cn/showproblem.php?pid=4135

给定一个数n,求某个区间[a,b]内有多少数与这个数互质。

对于一个给定的区间,我们如果能够求出这个区间内所有与其不互质的数的个数的话,那么互质数的个数也就求出来。

任何与N不互质的数一定是其某一个质因子的倍数,所以我们通过某一个质因子能够确定一个范围内的有多少个数该质因子的倍数的数。但是这并不是所有不互质数的充分条件,对于另外的质因子同样满足能够确定一系列的数,我们要做的就是容斥定理求他们的并集。num除以奇数个数相乘的时候是加,num除以偶数个数相乘的时候是减。

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <string>
  6. #include <queue>
  7. #include <stack>
  8. #include <iostream>
  9. #include <algorithm>
  10. using namespace std;
  11. #define RD(x) scanf("%d",&x)
  12. #define RD2(x,y) scanf("%d%d",&x,&y)
  13. #define RD3(x,y,z) scanf("%I64d%I64d%I64d",&x,&y,&z)
  14. #define clr0(x) memset(x,0,sizeof(x))
  15. typedef long long LL;
  16. const int maxn = 1e5+5;
  17. LL n,a,b;
  18. vector<LL> q;
  19. LL ans(LL x)
  20. {
  21. LL res = 0;
  22. for(LL i = 1;i < (1LL<<q.size());++i){
  23. LL u = 1,cnt = 0;
  24. for(int j = 0;j < q.size();++j)
  25. if(i & (1LL<<j))
  26. cnt++,u *= q[j];
  27. if(cnt & 1)
  28. res += x / u;
  29. else
  30. res -= x / u;
  31. }
  32. return res;
  33. }
  34. int main() {
  35. int _;RD(_);for(int cas = 1;cas <= _;++cas){
  36. printf("Case #%d: ",cas);
  37. RD3(a,b,n);
  38. q.clear();
  39. for(LL j = 2;j * j <= n;++j)if(n%j == 0){
  40. q.push_back(j);
  41. while(n%j == 0)
  42. n/=j;
  43. }
  44. if(n != 1)
  45. q.push_back(n);
  46. printf("%I64d\n",b - ans(b) - (a - 1 - ans(a-1)));
  47.  
  48. }
  49. return 0;
  50. }

hdu 4135 [a,b]中n互质数个数+容斥的更多相关文章

  1. hdu 1796 How many integers can you find 容斥第一题

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  2. HDU 1796How many integers can you find(简单容斥定理)

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  3. HDU - 5201 :The Monkey King (组合数 & 容斥)

    As everyone known, The Monkey King is Son Goku. He and his offspring live in Mountain of Flowers and ...

  4. HDU 1796 How many integers can you find (容斥)

    题意:给定一个数 n,和一个集合 m,问你小于的 n的所有正数能整除 m的任意一个的数目. 析:简单容斥,就是 1 个数的倍数 - 2个数的最小公倍数 + 3个数的最小公倍数 + ...(-1)^(n ...

  5. HDU - 4059: The Boss on Mars (容斥 拉格朗日 小小的优化搜索)

    pro: T次询问,每次给出N(N<1e8),求所有Σi^4 (i<=N,且gcd(i,N)==1) ; sol:  因为N比较小,我们可以求出素因子,然后容斥.  主要问题就是求1到P的 ...

  6. HDU 1796 How many integers can you find 容斥入门

    How many integers can you find Problem Description   Now you get a number N, and a M-integers set, y ...

  7. HDU 6106 17多校6 Classes(容斥简单题)

    Problem Description The school set up three elective courses, assuming that these courses are A, B, ...

  8. hdu 1796 How many integers can you find 容斥定理

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  9. hdu 4135 a到b的范围中多少数与n互质(容斥)

    Co-prime 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4135 input The first line on input contains ...

随机推荐

  1. Eclipse.Error.gen already exists but is not a source folder.

    在Eclipse ADT来开发Android App时会遇到以下问题:"myproject/gen already exists but is not a source folder. Co ...

  2. spring读取properties的几种方式

    参考链接:http://www.cnblogs.com/zxf330301/p/6184139.html

  3. 立即响应ScrollView上的子视图的手势

    self.myScrollView.delaysContentTouches = YES; self.myScrollView.CanCancelContentTouches=NO; 写了一个继承sc ...

  4. 【原创】DOTNET动态调试破解Spoon,及MSI安装包文件替换技术

    提到Spoon可能大家还会感到陌生,但是如果提及XenoCode那么研究过DOTNET的人应该都知道吧.Spoon的前身就是XenoCode,虽然没有了PostBuild这个混淆软件,但是虚拟化技术仍 ...

  5. ubuntu 下通过ftp命令下载文件

    /*连接*/ $ ftp 192.168.180.2Connected to 192.168.180.2.Name (192.168.180.2:rivsidn): admin  Password: ...

  6. mysqli_query数据库有数据,查不出来

    MySQLDB.class.php <?php /** * 数据库操作工具类 */ class MySQLDB { // 定义相关属性 private $host;// 主机地址 private ...

  7. hbase shell 命令

    HBase使用教程 时间 2014-06-01 20:02:18 IT社区推荐资讯 原文  http://itindex.net/detail/49825-hbase 主题 HBase 1     基 ...

  8. 2017/2/11CSS基础

    一:html中div: 1.DIV标签应用于 Style Sheet(样式表)方面会更显威力,它最终目的是给设计者另一种组织能力,有 Class.Style.title.ID 等属性. 2.<d ...

  9. OneZero第三周第三次站立会议(2016.4.6)

    1. 时间: 13:05--13:15  共计10分钟. 2. 成员: X 夏一鸣 * 组长 (博客:http://www.cnblogs.com/xiaym896/), G 郭又铭 (博客:http ...

  10. [C#]WinForm动态删除控件 Controls.Remove()

    今天遇到一个奇怪的问题,在WinForm内动态添加Button后再动态的移除,发生稀奇古怪的现象,Button控件只被规律的移除,没有完全移除 foreach (Control c in this.C ...