hdu4135 Co-prime 容斥原理
Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N.
Two integers are said to be co-prime or relatively prime if they have no common positive divisors other than 1 or, equivalently, if their greatest common divisor is 1. The number 1 is relatively prime to every integer.
容斥原理
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long ll; int pnum[]; int main(){
int T;
scanf("%d",&T);
for(int q=;q<=T;++q){
ll a,b;
int n;
scanf("%lld%lld%d",&a,&b,&n);
a--;
ll ans=;
int cnt=;
for(int i=;i*(ll)i<=n;++i){
if(!(n%i)){
pnum[++cnt]=i;
while(!(n%i))n/=i;
}
}
if(n>)pnum[++cnt]=n;
for(int i=;i<(<<cnt);++i){
int bit=;
int mul=;
for(int j=;j<=cnt;++j){
if(i&(<<(j-))){
bit++;
mul*=pnum[j];
}
}
if(bit%)ans+=b/mul-a/mul;
else ans-=b/mul-a/mul;
}
printf("Case #%d: %lld\n",q,b-a-ans);
}
return ;
}
hdu4135 Co-prime 容斥原理的更多相关文章
- HDU4135 Co-prime(容斥原理)
题目求[A,B]区间内与N互质数的个数. 可以通过求出区间内与N互质数的个数的前缀和,即[1,X],来得出[A,B]. 那么现在问题是求出[1,X]区间内与N互质数的个数,考虑这个问题的逆问题:[1, ...
- 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容斥原理 组合遍历
容斥原理实现的关键在于:组合遍历,即如何遍历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. ...
随机推荐
- bzoj2045
题解: 莫比乌斯反演经典题目 直接套公式好了 代码: #include<bits/stdc++.h> using namespace std; ; typedef long long ll ...
- 《Python》常用模块之collections模块
内置的数据类型: int float complex str list tuple dict set 基础数据类型: int float complex str list tuple ...
- 如何在Ubuntu中安装中文输入法
在使用ubuntu系统时,有的时候总觉得英文输入法不方便操作,总希望能有中文输入法可以辅助操作,那怎样才能在ubuntu中安装中文输入法呢?下面有一种简单的方法可以安装中文输入法. 如何在ubuntu ...
- box-shadow 边框阴影
box-shadow: 0 0 20px #000 inset;
- centos tar 常用
tar命令详解 -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用 ...
- Linux文件系统命令 rm
命令名:rm 功能:删除某一个文件或者目录 eg: renjg@renjg-HP-Compaq-Pro--MT:~/WorkSpace$ ls BM3 gf k8s minicom_download ...
- <Spark><Tuning and Debugging>
Overview 这一部分我们主要讨论如果配置一个Spark application,如何tune and debug Spark workloads 配置对Spark应用性能调优很重要.我们有必要理 ...
- js replace
<script type="text/javascript">// replace() 方法执行查找并替换操作,有两个参数,第一个是查找的正则表达式,第二个是callb ...
- Python 基础字典的增删改查
# 字典相关的代码## 1.字典的特性:a 字典的无序性: b 数据关联性强 C 键键值对 唯一一个映射数据类型# 字典的键必修二是可哈希的(不可变的数据类型:,字符串,数字的,布尔值,元祖)# 并且 ...
- Flask源码阅读-第一篇(flask包下的__main__.py)
源码: # -*- coding: utf-8 -*-""" flask.__main__ ~~~~~~~~~~~~~~ Alias for flask.run for ...