hdu 4135 [a,b]中n互质数个数+容斥
http://acm.hdu.edu.cn/showproblem.php?pid=4135
给定一个数n,求某个区间[a,b]内有多少数与这个数互质。
对于一个给定的区间,我们如果能够求出这个区间内所有与其不互质的数的个数的话,那么互质数的个数也就求出来。
任何与N不互质的数一定是其某一个质因子的倍数,所以我们通过某一个质因子能够确定一个范围内的有多少个数该质因子的倍数的数。但是这并不是所有不互质数的充分条件,对于另外的质因子同样满足能够确定一系列的数,我们要做的就是容斥定理求他们的并集。num除以奇数个数相乘的时候是加,num除以偶数个数相乘的时候是减。
- #include <cstdio>
- #include <cstdlib>
- #include <cmath>
- #include <cstring>
- #include <string>
- #include <queue>
- #include <stack>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- #define RD(x) scanf("%d",&x)
- #define RD2(x,y) scanf("%d%d",&x,&y)
- #define RD3(x,y,z) scanf("%I64d%I64d%I64d",&x,&y,&z)
- #define clr0(x) memset(x,0,sizeof(x))
- typedef long long LL;
- const int maxn = 1e5+5;
- LL n,a,b;
- vector<LL> q;
- LL ans(LL x)
- {
- LL res = 0;
- for(LL i = 1;i < (1LL<<q.size());++i){
- LL u = 1,cnt = 0;
- for(int j = 0;j < q.size();++j)
- if(i & (1LL<<j))
- cnt++,u *= q[j];
- if(cnt & 1)
- res += x / u;
- else
- res -= x / u;
- }
- return res;
- }
- int main() {
- int _;RD(_);for(int cas = 1;cas <= _;++cas){
- printf("Case #%d: ",cas);
- RD3(a,b,n);
- q.clear();
- for(LL j = 2;j * j <= n;++j)if(n%j == 0){
- q.push_back(j);
- while(n%j == 0)
- n/=j;
- }
- if(n != 1)
- q.push_back(n);
- printf("%I64d\n",b - ans(b) - (a - 1 - ans(a-1)));
- }
- return 0;
- }
hdu 4135 [a,b]中n互质数个数+容斥的更多相关文章
- 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 ...
- HDU 1796How many integers can you find(简单容斥定理)
How many integers can you find Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 ...
- HDU - 5201 :The Monkey King (组合数 & 容斥)
As everyone known, The Monkey King is Son Goku. He and his offspring live in Mountain of Flowers and ...
- HDU 1796 How many integers can you find (容斥)
题意:给定一个数 n,和一个集合 m,问你小于的 n的所有正数能整除 m的任意一个的数目. 析:简单容斥,就是 1 个数的倍数 - 2个数的最小公倍数 + 3个数的最小公倍数 + ...(-1)^(n ...
- HDU - 4059: The Boss on Mars (容斥 拉格朗日 小小的优化搜索)
pro: T次询问,每次给出N(N<1e8),求所有Σi^4 (i<=N,且gcd(i,N)==1) ; sol: 因为N比较小,我们可以求出素因子,然后容斥. 主要问题就是求1到P的 ...
- 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 ...
- HDU 6106 17多校6 Classes(容斥简单题)
Problem Description The school set up three elective courses, assuming that these courses are A, B, ...
- 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 ...
- hdu 4135 a到b的范围中多少数与n互质(容斥)
Co-prime 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4135 input The first line on input contains ...
随机推荐
- 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 ...
- spring读取properties的几种方式
参考链接:http://www.cnblogs.com/zxf330301/p/6184139.html
- 立即响应ScrollView上的子视图的手势
self.myScrollView.delaysContentTouches = YES; self.myScrollView.CanCancelContentTouches=NO; 写了一个继承sc ...
- 【原创】DOTNET动态调试破解Spoon,及MSI安装包文件替换技术
提到Spoon可能大家还会感到陌生,但是如果提及XenoCode那么研究过DOTNET的人应该都知道吧.Spoon的前身就是XenoCode,虽然没有了PostBuild这个混淆软件,但是虚拟化技术仍 ...
- ubuntu 下通过ftp命令下载文件
/*连接*/ $ ftp 192.168.180.2Connected to 192.168.180.2.Name (192.168.180.2:rivsidn): admin Password: ...
- mysqli_query数据库有数据,查不出来
MySQLDB.class.php <?php /** * 数据库操作工具类 */ class MySQLDB { // 定义相关属性 private $host;// 主机地址 private ...
- hbase shell 命令
HBase使用教程 时间 2014-06-01 20:02:18 IT社区推荐资讯 原文 http://itindex.net/detail/49825-hbase 主题 HBase 1 基 ...
- 2017/2/11CSS基础
一:html中div: 1.DIV标签应用于 Style Sheet(样式表)方面会更显威力,它最终目的是给设计者另一种组织能力,有 Class.Style.title.ID 等属性. 2.<d ...
- OneZero第三周第三次站立会议(2016.4.6)
1. 时间: 13:05--13:15 共计10分钟. 2. 成员: X 夏一鸣 * 组长 (博客:http://www.cnblogs.com/xiaym896/), G 郭又铭 (博客:http ...
- [C#]WinForm动态删除控件 Controls.Remove()
今天遇到一个奇怪的问题,在WinForm内动态添加Button后再动态的移除,发生稀奇古怪的现象,Button控件只被规律的移除,没有完全移除 foreach (Control c in this.C ...