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. ...
随机推荐
- leetCode 典型回溯例子
39题,翻译题目:给定一组候选集和一个目标值,在c的所有组合中,找出所有总和等于T的组合. 候选数组c中同一个数可以被选择多次(不限次数) 分析: 典型的回溯法应用 对数组里面的每个数,用递归的方式相 ...
- hdu-2063-过山车(匈牙利算法)
过山车 Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找 ...
- 玩转X-CTR100 | STM32F4 l X-Assistant串口助手控制功能
我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ] X-CTR100控制器配套的X-Assis ...
- ACM-ICPC 2018 徐州赛区网络预赛 G. Trace
There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx , yy ...
- FutureTask
因为实现了runnable接口,所以重写了run方法 Future接口如果用在多线程中,实现类一般是有一个volatile的属性,用来标志状态,比如state,如果事情做完了,那么会设置state为成 ...
- android-DNS服务找不到
1.重启eclipse 2.重新建立AVD 3.在建立AVD时sd卡数值不要填
- synchronized(三)
package com.bjsxt.base.sync003; /** * 对象锁的同步和异步问题 * @author alienware * */public class MyObject { pu ...
- mysql创建用户并给用户分配权限
1.登录Mysql [root@xufeng Desktop]# mysql -u root -pEnter password: Welcome to the MySQL monitor. Comma ...
- python-oop的理解
OOP是面向对象编程,特征分别是封装.继承.多态.抽象. 封装:封装是指将对象信息状态通过访问权限修饰符隐藏在对象内部,不允许外部程序直接访问,如果外部程序要访问对象内部,可以调用内部提供的get或s ...
- C# Sublime text3 环境配置(一)
下载地址:http://www.sublimetext.com/3 1.安装完之后,tools菜单下最下一个点一下,安装Package Control 插件2.Preferences菜单下,点Pack ...