【HDU4135】Co-prime

题意

给出三个整数N,A,B。问在区间[A,B]内,与N互质的数的个数。其中N<=10^9,A,B<=10^15。

分析

容斥定理的模板题。可以通过容斥定理求出[1,n]与x互质的数的个数。方法是先将x进行质因子分解,然后对于每个质因子pi,[1,n]内可以被pi整除的数目为n/pi。可以通过容斥定理解决逆命题,既[1,n]与x不互质的数目。n/p1+n/p2+n/p3-n/p1p2-n/p1p3-n/p2p3+n/p1p2p3。既奇数是加,偶数是减。具体的做法一般是通过二进制枚举来进行。质因子分解N的时间复杂度是O(sqrt(N)),然后枚举质因子的时间复杂度是O(2^(num)),其中num是质因子的数目。我们知道,这个num一般来说是非常小的,所以这个算法的时间复杂度是非常优秀的。

然后对于这个题,我们求出[1,B]与N互质的个数再减去[1,A]互质的个数。

code如下

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream> using namespace std;
typedef long long LL;
int T,N,num;
LL A,B;
int a[];
void prime(int n){
num=;
for(int i=;i*i<=n;i++){
if((n%i)==){
num++;
a[num]=i;
while((n%i)==){
n/=i;
}
}
}
if(n>){
num++;
a[num]=n;
}
return ;
}
LL solve(LL r,int n){
prime(n);
LL res=;
for(int i=;i<(<<num);i++){
int kk=;
LL div=;
for(int j=;j<=num;j++){
if(i&(<<(j-))){
kk++;
div*=a[j];
}
}
if(kk%)
res+=r/div;
else
res-=r/div;
}
return r-res;
}
int main(){
scanf("%d",&T);
for(int t=;t<=T;t++){
scanf("%lld%lld%d",&A,&B,&N);
LL ans=solve(B,N)-solve(A-,N);
printf("Case #%d: %lld\n",t,ans);
}
return ;
}

【HDU2841】visible tree

题意

有一片树林m*n,从(1,1)开始,每个整数点都有一棵树。famer站在点(0,0),问他能看见的树有几棵。其中n,m<=100000.

分析

先来看有哪些树没有办法被看到。对于点(xi,yi),当xi和yi可以同时被一个大于1的整数k整除,则点(xi,yi)无法被看到。也就是说,当且仅当这个点的横纵坐标互质时,才可以被看到。也就是说求[1,n]和[1,m]内有多少互质的点。到此为止,本题已经转换为上一个题的形式。然后枚举[1,m]作为x,然后[1,n]作为区间,按照上题方案进行求解。

code 如下

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream> using namespace std;
typedef long long LL;
int T,n,m,num;
int a[];
void prime(int x){
num=;
for(int i=;i*i<=x;i++){
if(x%i==){
num++;
a[num]=i;
while(x%i==&&x){
x/=i;
}
}
}
if(x>){
num++;
a[num]=x;
}
return;
}
LL solve(LL r,int x){
LL res=;
for(int i=;i<(<<num);i++){
LL k=,aa=;
for(int j=;j<=num;j++){
if(i&(<<(j-))){
k++;
aa*=a[j];
}
}
if(k%)
res+=r/aa;
else
res-=r/aa;
}
return r-res;
}
int main(){
scanf("%d",&T); for(int t=;t<=T;t++){
scanf("%d%d",&n,&m);
if(n>m)swap(n,m);
LL ans=; for(int i=;i<=n;i++){
prime(i);
ans+=solve(m,i);
//cout<<solve(m,i)<<endl;
}
printf("%lld\n",ans);
}
return ;
}
 

【HDU1695】GCD

题意

给出a,b,c,d,k,其中保证a=1,c=1。问在区间[a,b]和区间[c,d]内有多少不同的对gcd(x,y)=k。

分析

如果gcd(x,y)=k,则显然gcd(x/k,y/k)=1,既互质。我们令b<d,然后b/=k,d/=k。则题目转化为在区间[1,b]和区间[1,d]有多少不同对互质。

有没有感觉和上面那道题很像?就一个不同点,这个题目求的是不同对。所以不可以按照上面这个题直接进行枚举。对于区间[1,b],我们可以直接枚举求和欧拉函数。对于区间[b+1,d]我们可以按照上面的方法通过容斥定理进行求解。

code如下

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream> using namespace std;
typedef long long LL; int T,a,b,c,d,k;
LL ans;
int euler(int n){ //返回euler(n)
int res=n,aa=n;
for(int i=;i*i<=aa;i++){
if(aa%i==){
res=res/i*(i-);//先进行除法是为了防止中间数据的溢出
while(aa%i==) aa/=i;
}
}
if(aa>) res=res/aa*(aa-);
return res;
}
int num;
int pri[];
void prime(int x){
num=;
for(int i=;i*i<=x;i++){
if(x%i==){
num++;
pri[num]=i;
while(x%i==){
x/=i;
}
}
}
if(x>){
num++;
pri[num]=x;
}
}
LL solve(LL r,int x){
LL res=;
for(int i=;i<(<<num);i++){
int k=,aa=;
for(int j=;j<=num;j++){
if(i&(<<(j-))){
k++;
aa*=pri[j];
}
}
if(k%)
res+=r/aa;
else
res-=r/aa;
}
return r-res;
}
int main(){
scanf("%d",&T);
for(int t=;t<=T;t++){
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
if(k==){
printf("Case %d: 0\n",t);
continue;
}
if(b>d)swap(b,d);
b/=k,d/=k;
ans=;
for(int i=;i<=b;i++){
ans+=euler(i);
}
for(LL i=b+;i<=d;i++){
prime(i);
ans+=solve(b,i);
}
printf("Case %d: %lld\n",t,ans);
}
return ;
}

【hdu4135】【hdu2841】【hdu1695】一类通过容斥定理求区间互质的方法的更多相关文章

  1. HDU5768Lucky7(中国剩余定理+容斥定理)(区间个数统计)

    When ?? was born, seven crows flew in and stopped beside him. In its childhood, ?? had been unfortun ...

  2. HDU 1796How many integers can you find(简单容斥定理)

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  3. Codeforces Round #330 (Div. 2) B. Pasha and Phone 容斥定理

    B. Pasha and Phone Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/595/pr ...

  4. hdu_5213_Lucky(莫队算法+容斥定理)

    题目连接:hdu_5213_Lucky 题意:给你n个数,一个K,m个询问,每个询问有l1,r1,l2,r2两个区间,让你选取两个数x,y,x,y的位置为xi,yi,满足l1<=xi<=r ...

  5. How Many Sets I(容斥定理)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3556 How Many Sets I Time Limit: 2 ...

  6. HDU - 4135 Co-prime 容斥定理

    题意:给定区间和n,求区间中与n互素的数的个数, . 思路:利用容斥定理求得先求得区间与n互素的数的个数,设表示区间中与n互素的数的个数, 那么区间中与n互素的数的个数等于.详细分析见求指定区间内与n ...

  7. BZoj 2301 Problem b(容斥定理+莫比乌斯反演)

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MB Submit: 7732  Solved: 3750 [Submi ...

  8. BZOJ2839 : 集合计数 (广义容斥定理)

    题目 一个有 \(N\) 个 元素的集合有 \(2^N\) 个不同子集(包含空集), 现在要在这 \(2^N\) 个集合中取出若干集合(至少一个), 使得它们的交集的元素个数为 \(K\) ,求取法的 ...

  9. HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

随机推荐

  1. Python猴子补丁

    属性在运行时的动态替换,叫做猴子补丁(Monkey Patch). 为什么叫猴子补丁 属性的运行时替换和猴子也没什么关系,关于猴子补丁的由来网上查到两种说法: 1,这个词原来为Guerrilla Pa ...

  2. spec.template.spec.initContainers[1].securityContext.privileged: Forbidden: disallowed by policy 问题解决

    主要是执行系统特权应用解决方法: api server   controller-manager 加上  --allow-privileged=true 即可 之后重启服务  

  3. php基础语法(数据类型、运算符)

    数据类型 标量类型: int, float, string, bool 复合类型: array, object 特殊类型: null, resouce 整数类型int, integer 字符串类型st ...

  4. php用smtp方式发送邮件

    http://www.daixiaorui.com/read/16.html 2个比较经典的PHP加密解密函数分享 http://www.jb51.net/article/51706.htm php5 ...

  5. win10下安装并启动zookeeper

    下载直接到zk的官网(zookeeper.apache.org)即可,点击右边的Releases,在Download下再点Download进入镜像下载页面,在给出的链接列表里选择一个镜像地址,进去后选 ...

  6. C语言实现简单的单向链表(创建、插入、删除)及等效STL实现代码

    实现个算法,懒得手写链表,于是用C++的forward_list,没有next()方法感觉很不好使,比如一个对单向链表的最简单功能要求: input: 1 2 5 3 4 output: 1-> ...

  7. JPype:实现在python中调用JAVA

    一.JPype简述 1.JPype是什么? JPype是一个能够让 python 代码方便地调用 Java 代码的工具,从而克服了 python 在某些领域(如服务器端编程)中的不足. 2.JPype ...

  8. Php处理时间的函数

    1,字符串与时间: 例如 $time = strtotime("2007-3-5"); echo date("Y-m-d H:i:s",$time); 2,当前 ...

  9. Linux学习笔记 - Shell 控制语句

    if 语句 语法: #!/bin/bash a= b= if [ $a -eq $b ] then echo "a 等于 b" elif [ $a -gt $b ] then ec ...

  10. HDU2546题解

    解题思路:先对价格排序(顺序或倒序都可以),然后,对前n-1(从1开始.排序方式为顺序)做容量为m(卡上余额)-5的01背包(背包体积和价值相等).假设dp[i][j]表示从前i个背包中挑选体积不超过 ...