【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. Word动态替换文本

    public class WordTest2 { public static void main(String[] args) { /** 此Map存放动态替换的内容,key-Word中定义的变量,v ...

  2. 用Eclipse进行java学习的步骤

    用Eclipse进行java学习的步骤(1)File,new,File Project->在New Java Project页面的Project name文本框中填入名称,点击finish(2) ...

  3. wordpress域名解析到了网站,但是点击其他页面会出现ip而不是域名

         1.前提域名可以访问你的网站证明解析没问题 2.那就是wp后台的设置问题,将url和站点url改为你的域名http://www.eovision.cc清理缓存即可 亲测可用,如果改了出现页面 ...

  4. Android JNI访问Java成员

    在 JNI 调用中,不仅仅 Java 可以调用本地方法,本地方法也可以调用 Java 中的方法和成员变量. Java 中的类封装了属性和方法,想要访问 Java 中的属性和方法,首先要获得 Java ...

  5. strcpy手写——面试

    #include<stdio.h> #include<string.h> ]={]; char* strcpy(char *to,char *from){ if(NULL==t ...

  6. Python一些特殊用法(map、reduce、filter、lambda、列表推导式等)

    Map函数: 原型:map(function, sequence),作用是将一个列表映射到另一个列表, 使用方法: def f(x): return x**2 l = range(1,10) map( ...

  7. 老齐python-基础6(循环 if while for)

    1.条件语句if 依据某个条件,满足这个条件后执行下面的内容 >>> if bool(conj): #语法 do something >>> a = 8 >& ...

  8. ubuntu :安装skype聊天工具

    如题,今天就想搞个软件在ubuntu能聊天,查一下skype,好像网上有人说不是每个安装包都用的了,skype-ubuntu-precise_4.2.0.13-1_i386.deb可以, 我在微盘下载 ...

  9. REST API权限集成设计

    REST API权限集成设计 应用分为两大部分,前端html+后端Rest服务,前端html和后端Rest服务部署完全分离. 目标:可访问资源都处于权限控制之下(意味着通过浏览器地址栏的任意url都会 ...

  10. Jquery each循环中中断

    在each代码块内不能使用break和continue,要实现break和continue的功能的话,要使用其它的方式 break----用return false; continue --用retu ...