您的位置:首页 > 其它

【中国剩余定理】【容斥原理】【快速乘法】【数论】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,所以要用快速乘法(和快速幂类似,只是乘改成加)

//
//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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: