HDU 4135 Co-prime
思路:直接用求(b,1)范围内互质的数,(a-1,1)范围内互质的数。再求反
就是敲一下容斥模板
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
#define clc(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
const int N=;
#define LL long long
// inline int r(){
// int x=0,f=1;char ch=getchar();
// while(ch>'9'||ch<'0'){if(ch=='-') f=-1;ch=getchar();}
// while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
// return x*f;
// } int T;
LL a,b,n;
int pri[];
int k;
void fun(LL x){
k=;
for(int i=;i*i<=x;i++){
if(x&&x%i==){
pri[k++]=i;
while(x&&x%i==){
x/=i;
}
}
}
if(x>) pri[k++]=x;
}
LL exclu(LL num,int m){
LL ans=,tmp,times;
for(int i=;i<(LL)(<<m);i++){
tmp=,times=;
for(int j=;j<m;j++){
if(i&((LL)(<<j)))
times++,tmp*=pri[j];
}
if(times&) ans+=num/tmp;
else ans-=num/tmp;
}
return ans;
}
int main(){
scanf("%d",&T); int cas=;
while(T--){
scanf("%I64d%I64d%I64d",&a,&b,&n);
clc(pri,);
fun(n);
LL ans1=exclu(a-,k);
LL ans2=exclu(b,k);
printf("Case #%d: %I64d\n",cas++,b-ans2-(a--ans1));
}
return ;
}
HDU 4135 Co-prime的更多相关文章
- HDU 4135 容斥
问a,b区间内与n互质个数,a,b<=1e15,n<=1e9 n才1e9考虑分解对因子的组合进行容斥,因为19个最小的不同素数乘积即已大于LL了,枚举状态复杂度不会很高.然后差分就好了. ...
- [容斥原理] hdu 4135 Co-prime
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4135 Co-prime Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 5901 count prime & code vs 3223 素数密度
hdu5901题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5901 code vs 3223题目链接:http://codevs.cn/problem ...
- HDU 4135
http://acm.hdu.edu.cn/showproblem.php?pid=4135 求[A,B]内与N互素的数字个数 首先对N分解质因数,对于一个质因数,1-n与它不互素的数字个数是n/(这 ...
- hdu 4135 a到b的范围中多少数与n互质(容斥)
Co-prime 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4135 input The first line on input contains ...
- hdu (kruska and prime) 继续畅通工程
题目http://acm.hdu.edu.cn/showproblem.php?pid=1879 复习一下最小生成树的两个基本算法. 由于存在道路是否已修建的问题,如果已修建,那么该条道路的成本即为0 ...
- hdu 4135 [a,b]中n互质数个数+容斥
http://acm.hdu.edu.cn/showproblem.php?pid=4135 给定一个数n,求某个区间[a,b]内有多少数与这个数互质. 对于一个给定的区间,我们如果能够求出这个区间内 ...
- 容斥 - HDU 4135 Co-prime
Co-prime Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=4135 推荐: 容斥原理 Mean: 给你一个区间[l,r]和一 ...
- 【容斥】HDU 4135 Co-prime
acm.hdu.edu.cn/showproblem.php?pid=4135 [题意] 询问[a,b]中与n互质的数有多少个 [思路] 考虑[1,m]中与n互质的数有多少个,答案就是query(b) ...
- 【hdu 4135】Co-prime
[题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=4135 [题意] 让你求出[a..b]这个区间内和N互质的数的个数; [题解] 利用前缀和,求出[1 ...
随机推荐
- codeforces Codeforces Round #345 (Div. 1) C. Table Compression 排序+并查集
C. Table Compression Little Petya is now fond of data compression algorithms. He has already studied ...
- poj 1733 Parity game
Parity game 题意:一个长度为N(N < 1e9)内的01串,之后有K(K <= 5000)组叙述,表示区间[l,r]之间1的个数为odd还是even:问在第一个叙述矛盾前说了几 ...
- <一> ASP.NET Html 表单
把客户端数据发送到服务器端用<form>标签 htmlpage.html Default2.aspx.cs
- sqlserver分页;mysql分页;orcale分页 的sql 查询语句
一,sqlserver分页: )) ID FROM 表名 AS 表名1_1 ORDER BY ID)) ORDER BY ID; 二:mysql分页 )*每页显示的数据条数,当前页码 * 每页显示的数 ...
- 解决Linux/aix 下的websphere log4j不生效
websphere 解决Linux/aix下的log4j不生效 在目录: /IBM/WebSphere/AppServer/profiles/AppSrv01/properties 增加一个文件:可以 ...
- java Class的Long id初始化 为0的问题android数据库操做出现的 android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
java的class中的Long类型变量调用默认的 构造函数new后会被初始化为0. 这句话大家可能感觉这么低级的事情还用你说? 我想说的是这个会产生的一个应用场景 和 避免方法 场景:db插入时候p ...
- UVA 825 Walkiing on the safe side
根据地图,要求固定两点间最短路径的条数 . 这题的输入数据就是个坑,题目有没有说明数据之间有多个空格,结尾换行符之前也不止一个空格,WA了好几遍,以后这种情况看来都要默认按照多空格的情况处理了. 可以 ...
- SPRING IN ACTION 第4版笔记-第四章Aspect-oriented Spring-001-什么是AOP
一. Aspect就是把会在应用中的不同地方重复出现的非业务功能的模块化,比如日志.事务.安全.缓存 In software development, functions that span mult ...
- udelay、mdelay、ndelay、msleep使用比较说明
时间单位: 毫秒(ms).微秒 (μs).纳秒(ns).皮秒(ps).飞秒(fs).阿秒.渺秒 1 s = 10^3 ms = 10^6 us = 10^9 ns = 10^12 ps = ...
- Service Oriented Architecture
What is Service Oriented Architecture (SOA)? There have been so many interpretations of this through ...