您的位置:首页 > 其它

2016多校4 hdu 5768 Lucky7 数论+容斥原理

2016-07-28 21:35 465 查看

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=5768

题意

在给定范围内能够整除7并且不满足模pi等于ri的数的个数。

思路

代码

#include<cstdio>
#include<cstdlib>
#define LL long long
LL p[20],r[20];
int n;
LL sum;
LL powMod(LL a,int b,int mod){
LL ret=1;
while(b){
if(b&1) ret=ret*a%mod;
a=a*a%mod;
b/=2;
}
return ret;
}
void exgcd(LL a,LL b,LL& d,LL& x,LL& y){
if(b==0){
d=a;
x=1;
y=0;
}
else{
exgcd(b,a%b,d,y,x);
y-=a/b*x;
}
}
void dfs(int i,int nu,int x,LL pp,LL rr,LL b){
//printf("%d %d %d %d %lld\n",i,nu,x,mu,b);
if(nu==x){
sum+=b/pp;
if((b%pp)&&(b%pp>=rr)) ++sum;
return;
}
if(i==n) return;
LL ppp=pp,rrr=rr,d,xx,yy;
exgcd(ppp,p[i],d,xx,yy);
xx*=(r[i]-rr)/d;
xx=(xx%(p[i]/d)+p[i]/d)%(p[i]/d);
rrr+=xx*ppp;
ppp=ppp/d*p[i];
rrr=(rrr%ppp+ppp)%ppp;
dfs(i+1,nu+1,x,ppp,rrr,b);
dfs(i+1,nu,x,pp,rr,b);
}
LL rong(LL x){
LL s=0;
for(int i=1;i<=n;++i){
sum=0;
dfs(0,0,i,1,0,x);
//printf("rong%d %lld\n",i,sum);
if(i&1)
s+=sum;
else
s-=sum;
}
//printf("%lld\n",s);
return x-s;
}
int main(){
int t;
scanf("%d",&t);
for(int ca=1;ca<=t;++ca){
LL x,y;
LL ansx,ansy;
scanf("%d%I64d%I64d",&n,&x,&y);
x=(x-1)/7;
y=y/7;
for(int i=0;i<n;++i){
scanf("%d%d",p+i,r+i);
r[i]=powMod(7,p[i]-2,p[i])*r[i]%p[i];
}
ansx=rong(x);
ansy=rong(y);
printf("Case #%d: %I64d\n",ca,ansy-ansx);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: