【hdu4135】【hdu2841】【hdu1695】一类通过容斥定理求区间互质的方法
【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】一类通过容斥定理求区间互质的方法的更多相关文章
- HDU5768Lucky7(中国剩余定理+容斥定理)(区间个数统计)
When ?? was born, seven crows flew in and stopped beside him. In its childhood, ?? had been unfortun ...
- HDU 1796How many integers can you find(简单容斥定理)
How many integers can you find Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 ...
- 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 ...
- hdu_5213_Lucky(莫队算法+容斥定理)
题目连接:hdu_5213_Lucky 题意:给你n个数,一个K,m个询问,每个询问有l1,r1,l2,r2两个区间,让你选取两个数x,y,x,y的位置为xi,yi,满足l1<=xi<=r ...
- How Many Sets I(容斥定理)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3556 How Many Sets I Time Limit: 2 ...
- HDU - 4135 Co-prime 容斥定理
题意:给定区间和n,求区间中与n互素的数的个数, . 思路:利用容斥定理求得先求得区间与n互素的数的个数,设表示区间中与n互素的数的个数, 那么区间中与n互素的数的个数等于.详细分析见求指定区间内与n ...
- BZoj 2301 Problem b(容斥定理+莫比乌斯反演)
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MB Submit: 7732 Solved: 3750 [Submi ...
- BZOJ2839 : 集合计数 (广义容斥定理)
题目 一个有 \(N\) 个 元素的集合有 \(2^N\) 个不同子集(包含空集), 现在要在这 \(2^N\) 个集合中取出若干集合(至少一个), 使得它们的交集的元素个数为 \(K\) ,求取法的 ...
- HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
随机推荐
- piezo film 压电相关信息记录 (2018-05-04 更新)
piezo film 压电相关信息记录 起因需要使用 Piezo 做一些设计 http://www.te.com.cn/chn-zh/videos/transportation/piezo-film- ...
- sqlzoo练习题答案
title: SQL-Learning date: 2019-03-12 20:37:21 tags: SQL --- 这是关于在一个SQL学习网站的练习题答案记录:SQL教程 SQL基础 由一些简单 ...
- arm_linux QT+v4l 显示视频
1.参考(原创)基于ZedBoard的Webcam设计(三):视频的采集和动态显示 下载代码实测可用. 2.重新下载了csdn的代码,缺widget.h文件,后重新生成widget工程(自动产生wid ...
- GPS数据包格式解析
四种定位系统:1.美国的全球定位系统(Global Positioning System,GPS)2.俄罗斯的格罗拉斯(Global Nabigation Satellite System,GLONA ...
- 【经验】实现STL算法时遇到的模板编译错误问题
在实现set_union算法时调用了自己写的copy算法,出现了以下问题. Error 1 error C2665: 'xyz_stl::__copy' : none of the 2 overloa ...
- 转-SpringMVC——之 国际化
原文地址:http://www.cnblogs.com/liukemng/p/3750117.html 在系列(7)中我们讲了数据的格式化显示,Spring在做格式化展示的时候已经做了国际化处理,那么 ...
- Visual Studio Online 创建项目
VSO是微软为软件开发人员提供的一款基于云计算的开发平台.Team Foundation Server已经可以基于云端使用,无需再为配置和部署耗费多余的时间(PS:当初为了在服务器上部署这个鼓捣了4个 ...
- 仅用CSS3创建h5预加载双旋圈
<head> <meta charset="UTF-8"> <title></title> <style type=" ...
- mysql注入快速学习基础
前言: sql注入想学好,学通.必须得了解一下基础的SQL 语句.这里我快速理一理 正文: 搭建环境建议下phpsduy快速搭建 select * from kasi select 字段名 from ...
- 【C++】
C++声明function后面加上等于0(=0)何解? https://zhidao.baidu.com/question/1446181256925153340.html