【中国剩余定理】【容斥原理】【快速乘法】【数论】HDU 5768 Lucky7
来源:互联网 发布:上海市人口普查数据 编辑:程序博客网 时间:2024/06/05 16:57
题目链接:
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 24using namespace std;typedef long long LL;int cas,cass;int n,m,lll;LL l,r,ans;LL p[N],a[N];bool u[N];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);#endifint 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[n]=1;p[n]=7;a[n]=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;}/*////*/
0 0
- 【中国剩余定理】【容斥原理】【快速乘法】【数论】HDU 5768 Lucky7
- HDU 5768 Lucky7 中国剩余定理+状压+容斥+快速乘法
- HDU 5768 (Lucky7 中国剩余定理+容斥原理)
- HDU 5768Lucky7-中国剩余定理+容斥原理
- HDU 5768 Lucky7(中国剩余定理+容斥原理)
- hdu 5768 Lucky7 容斥原理 中国剩余定理
- HDU 5768Lucky7(多校第四场)容斥+中国剩余定理(扩展欧几里德求逆元的)+快速乘法
- HDU 5768 Lucky7 数论 中国剩余定理
- HDU-5768-Lucky7(中国剩余定理+容斥)
- hdu5768 Lucky7 中国剩余定理 + 容斥原理
- HDU 5768 Lucky7(2016 Multi-University Training Contest 4 -1005)——中国剩余定理 + 容斥原理
- HDU 5768 Lucky7 中国剩余定理
- HDU-5768-Lucky7-中国剩余定律+容斥
- HDU5768 Lucky7[中国剩余定理+容斥定理]
- hdu 5768 中国剩余定理 + 容斥原理
- HDU5768:Lucky7(中国剩余定理 & 容斥)
- 【数论】hdu5768 Lucky7(中国剩余定理)
- 2016多校4 hdu 5768 Lucky7 数论+容斥原理
- 【模拟】HDU 5752 Sqrt Bo
- 【模拟】HDU 5762 Teacher Bo
- 【博弈论】HDU 5754 Life Winner Bo
- laravel 学习笔记 I
- 【数学】HDU 5761 Rower Bo
- 【中国剩余定理】【容斥原理】【快速乘法】【数论】HDU 5768 Lucky7
- 【模拟】HDU 5774 Where Amazing Happens
- 【归并排序】【逆序数】HDU 5775 Bubble Sort
- 【动态规划】【KMP】HDU 5763 Another Meaning
- 【动态规划】【二分】【最长上升子序列】HDU 5773 The All-purpose Zero
- 【动态规划】Vijos P1037 搭建双塔
- 【动态规划】Vijos P1104 采药(NOIP2005普及组第三题)
- 【动态规划】Vijos P1313 金明的预算方案(NOIP提高组2006第二题)
- html改成jsp页面,中文出现乱码