【中国剩余定理】【容斥原理】【快速乘法】【数论】HDU 5768 Lucky7
2016-08-14 00:34
495 查看
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5768
题目大意:
T组数据,求L~R中满足:1.是7的倍数,2.对n个素数有 %pi!=ai 的数的个数。
题目思路:
【中国剩余定理】【容斥原理】【快速乘法】【数论】
因为都是素数所以两两互素,满足中国剩余定理的条件。
把7加到素数中,a=0,这样就变成解n+1个同余方程的通解(最小解)。之后算L~R中有多少解。
但是由于中国剩余定理的条件是同时成立的,而题目是或的关系,所以要用容斥原理叠加删减。
中间过程中可能会爆long long,所以要用快速乘法(和快速幂类似,只是乘改成加)
View Code
http://acm.hdu.edu.cn/showproblem.php?pid=5768
题目大意:
T组数据,求L~R中满足:1.是7的倍数,2.对n个素数有 %pi!=ai 的数的个数。
题目思路:
【中国剩余定理】【容斥原理】【快速乘法】【数论】
因为都是素数所以两两互素,满足中国剩余定理的条件。
把7加到素数中,a=0,这样就变成解n+1个同余方程的通解(最小解)。之后算L~R中有多少解。
但是由于中国剩余定理的条件是同时成立的,而题目是或的关系,所以要用容斥原理叠加删减。
中间过程中可能会爆long long,所以要用快速乘法(和快速幂类似,只是乘改成加)
// //by coolxxx // #include<iostream> #include<algorithm> #include<string> #include<iomanip> #include<memory.h> #include<time.h> #include<stdio.h> #include<stdlib.h> #include<string.h> //#include<stdbool.h> #include<math.h> #define min(a,b) ((a)<(b)?(a):(b)) #define max(a,b) ((a)>(b)?(a):(b)) #define abs(a) ((a)>0?(a):(-(a))) #define lowbit(a) (a&(-a)) #define sqr(a) ((a)*(a)) #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b)) #define eps (1e-8) #define J 10000000 #define MAX 0x7f7f7f7f #define PI 3.1415926535897 #define N 24 using namespace std; typedef long long LL; int cas,cass; int n,m,lll; LL l,r,ans; LL p ,a ; bool u ; LL cheng(LL a,LL b,LL mod) { LL sum=0; for(;b;b>>=1) { if(b&1LL)sum=(sum+a)%mod; a=(a+a)%mod; } return sum; } LL exgcd(LL a,LL b,LL &x,LL &y) { if(!b){x=1,y=0;return a;} LL d=exgcd(b,a%b,y,x); y-=a/b*x; return d; } LL china(int nn) { LL sum=0,tot=1,tott,x,y; int i; for(i=1;i<=nn;i++)if(u[i])tot*=p[i]; for(i=1;i<=nn;i++) { if(!u[i])continue; tott=tot/p[i]; exgcd(tott,p[i],x,y); x=(x%p[i]+p[i])%p[i]; sum=((sum+cheng(a[i]*tott%tot,x,tot))%tot+tot)%tot; } sum=(r+tot-sum)/tot-(l-1+tot-sum)/tot; return sum; } int main() { #ifndef ONLINE_JUDGE // freopen("1.txt","r",stdin); // freopen("2.txt","w",stdout); #endif int i,j,k,ii; // for(scanf("%d",&cas);cas;cas--) for(scanf("%d",&cas),cass=1;cass<=cas;cass++) // while(~scanf("%s",s)) // while(~scanf("%d",&n)) { ans=0; printf("Case #%d: ",cass); scanf("%d%lld%lld",&n,&l,&r); for(i=1;i<=n;i++) scanf("%lld%lld",&p[i],&a[i]); lll=1<<n; n++; u =1;p =7;a =0; for(i=0;i<lll;i++) { for(j=i,k=0,ii=1;ii<n;j>>=1,ii++) { u[ii]=j&1; k+=u[ii]; } k=k&1?-1:1; ans+=1LL*k*china(n); } printf("%lld\n",ans); } return 0; } /* // // */
View Code
相关文章推荐
- 【中国剩余定理】【容斥原理】【快速乘法】【数论】HDU 5768 Lucky7
- HDU 5768 Lucky7 (中国剩余定理 + 容斥 + 快速乘法)
- hdu 5768 Lucky7 容斥原理 中国剩余定理
- HDU 5768 Lucky7 数论 中国剩余定理
- HDU 5768 Lucky7(2016 Multi-University Training Contest 4 -1005)——中国剩余定理 + 容斥原理
- HDU 5768 Lucky7 容斥原理+中国剩余定理(互质)
- HDU --- 5446 Unknown Treasure 数论综合【Lucas定理 + 中国剩余定理 + 快速乘 + 思维】
- HDU - 5768 容斥原理 + 中国剩余定理
- HDU 5446 Lucas + 中国剩余定理 + 快速乘法
- hdu-5768 Lucky7(容斥定理+中国剩余定理)
- HDU 5768Lucky7(多校第四场)容斥+中国剩余定理(扩展欧几里德求逆元的)+快速乘法
- hdu 5768 中国剩余定理 + 容斥原理
- HDU 5768 Lucky7 中国剩余定理+状压+容斥+快速乘法
- 一些关于中国剩余定理的数论题(POJ 2891/HDU 3579/HDU 1573/HDU 1930)
- 2013长春网赛1009 hdu 4767 Bell(矩阵快速幂+中国剩余定理)
- hdu 5446 lucas + 中国剩余定理 + 快速乘 (快速乘板子,中国剩余定理板子,lucas最新板子)
- 2016多校4 hdu 5768 Lucky7 数论+容斥原理
- HDU-5768-Lucky7-中国剩余定律+容斥
- hdu5768 Lucky7 中国剩余定理 + 容斥原理
- HDU 3579-数论训练A题 exgcd加中国剩余定理