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; }
相关文章推荐
- 排序算法
- 地图要素修改
- 全局的Context实现
- Git学习(3)
- poj3666 Making the Grade
- HDD is Outdated Technology(标记有技巧)
- java算法——分解质因数
- thinkphp介绍及访问方式
- jQuery对象与DOM对象
- Svn与Git的区别
- D3.js 之面包圈图
- Android屏幕适配全攻略(最权威的官方适配指导)
- HDU 1213
- android异常收集- java.lang.NumberFormatException
- 商城项目总结(一)
- Linux基础系列4(ls,cp命令详解)
- android相关素材以及网站
- Html与CSS快速入门03-CSS基础应用
- 层叠上下文(The stacking context)
- HelloWorld