HDU4135 Co-prime(容斥原理)
题目求[A,B]区间内与N互质数的个数。
- 可以通过求出区间内与N互质数的个数的前缀和,即[1,X],来得出[A,B]。
- 那么现在问题是求出[1,X]区间内与N互质数的个数,考虑这个问题的逆问题:[1,X]区间内与N不互质数的个数。
- 于是就可以先处理出N的所有质因数{p0,p1,p2,...,pn}。
- 而[1,X]能被pi整除的数有$\lfloor \frac X{p_i} \rfloor$个,再利用容斥原理除掉质因数公倍数重复计数的部分就能求出不互质个数。
- 最后X减去不互质个数就是互质个数了。
#include<cstdio>
#include<cstring>
using namespace std;
int prime[],pn;
long long calc(long long n){
long long res=;
for(int i=; i<(<<pn); ++i){
int tmp=,cnt=;
for(int j=; j<pn; ++j){
if(((i>>j)&)==) continue;
++cnt;
tmp*=prime[j];
}
if(cnt&) res+=n/tmp;
else res-=n/tmp;
}
return n-res;
}
int main(){
long long a,b;
int t,n;
scanf("%d",&t);
for(int cse=; cse<=t; ++cse){
scanf("%lld%lld%d",&a,&b,&n);
pn=;
for(int i=; i*i<=n; ++i){
if(n%i) continue;
while(n%i==) n/=i;
prime[pn++]=i;
}
if(n!=) prime[pn++]=n;
printf("Case #%d: %lld\n",cse,calc(b)-calc(a-));
}
return ;
}
HDU4135 Co-prime(容斥原理)的更多相关文章
- hdu4135 Co-prime【容斥原理】
Co-prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- [HDU4135]CO Prime(容斥)
也许更好的阅读体验 \(\mathcal{Description}\) \(t\)组询问,每次询问\(l,r,k\),问\([l,r]\)内有多少数与\(k\)互质 \(0<l<=r< ...
- hdu4135 Co-prime 容斥原理
Given a number N, you are asked to count the number of integers between A and B inclusive which are ...
- hdu4135容斥原理 组合遍历
容斥原理实现的关键在于:组合遍历,即如何遍历2^n种组合. 容斥原理的三种写法: DFS 队列数组 位数组 #include<stdio.h> #include<iostream&g ...
- HDU4135(容斥原理)
Co-prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 容斥原理的(二进制思想和质因子分解+模板)hdu4135+ecf81.D
题:http://acm.hdu.edu.cn/showproblem.php?pid=4135 题意:求[A,B]与N互质的数的个数 #include<iostream> #includ ...
- Codeforces1036F Relatively Prime Powers 【容斥原理】
题目分析: 这种题目标题写莫比乌斯反演会不会显得太恐怖了,那就容斥算了. gcd不为1的肯定可以开根.所以把根式结果算出来就行了. 辣鸡题目卡我精度. 代码: #include<bits/std ...
- HDU4135容斥原理
#include <cstdio> #include <string.h> #include <cmath> using namespace std; #defin ...
- hdu4059 The Boss on Mars(差分+容斥原理)
题意: 求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和. 知识点: 差分: 一阶差分: 设 则 为一阶差分. 二阶差分: n阶差分: 且可推出 性质: 1. ...
随机推荐
- Android相机、相册获取图片显示并保存到SD卡
Android相机.相册获取图片显示并保存到SD卡 [复制链接] 电梯直达 楼主 发表于 2013-3-13 19:51:43 | 只看该作者 |只看大图 本帖最后由 happy小妖同学 ...
- Android - 文件读写操作 总结
在android中的文件放在不同位置,它们的读取方式也有一些不同. 本文对android中对资源文件的读取.数据区文件的读取.SD卡文件的读取及RandomAccessFile的方式和方法进行了整理. ...
- 搭个 Web 服务器(一)
导读 我相信,如果你想成为一个更好的开发者,你必须对日常使用的软件系统的内部结构有更深的理解,包括编程语言.编译器与解释器.数据库及操作系统.Web 服务器及 Web 框架.而且,为了更好更深入地理解 ...
- CoreLoation
- (CLLocationManager *)locationManager { if (!_locationManager) { _locationManager = [[CLLocationMan ...
- 服务器部署之 cap deploy:setup
文章是从我的个人博客上粘贴过来的, 大家也可以访问 www.iwangzheng.com $ cap deploy:setup 执行到这一步的时候会时间较长,可以直接中断 * executing &q ...
- Android自动登录与记住密码
// 获取实例对象 sp = this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE); rem_pw ...
- Nested List Weight Sum I & II
Nested List Weight Sum I Given a nested list of integers, return the sum of all integers in the list ...
- selenium启动Chrome时,加载用户配置文件
selenium启动Chrome时,加载用户配置文件 Selenium操作浏览器是不加载任何配置的,网上找了半天,关于Firefox加载配置的多点,Chrome资料很少,下面是关于加载Chrome ...
- iOS和android游戏纹理优化和内存优化(cocos2d-x)(转载)
转自http://blog.csdn.net/langresser_king/article/details/8426708 (未完成) 1.2d游戏最占内存的无疑是图片资源. 2.cocos2d-x ...
- iOS UITableView 的beginUpdates和endUpdates
在官方文档中是这样介绍beginUpdates的 Call this method if you want subsequent insertions, deletion, and selection ...