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

##题目链接
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;  
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值