您的位置:首页 > 其它

HDU 5768 Lucky7-中国剩余定理+容斥原理

2016-08-01 23:43 246 查看
http://acm.hdu.edu.cn/showproblem.php?pid=5768

注意爆ll

容斥原理求一下【1,x】里有多少个满足的即可

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>
using namespace std;
long long m[20],a[20],mm[20],aa[20];

void extend_Euclid(long long a, long long b, long long &x, long long &y)
{
if(b == 0)
{
x = 1;
y = 0;
return;
}
extend_Euclid(b, a % b, x, y);
long long tmp = x;
x = y;
y = tmp - (a / b) * y;
}

long long CRT(long long a[],long long  m[],int n,long long & M)
{
M = 1;
long long ans = 0;
for(int i=1; i<=n; i++)
M *= m[i];
for(int i=1; i<=n; i++)
{
long long x, y;
long long Mi = M / m[i];
extend_Euclid(Mi, m[i], x, y);
x = (x%m[i]+m[i])%m[i];
ans = (ans + ((Mi % M) * (x % M) % M* a[i]) % M) % M;
}
if(ans < 0) ans += M;
return ans;
}

int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int cnt=1;
int tt; scanf("%d",&tt);
while (tt--)
{
int n;
long long x,y,ans=0;
scanf("%d%lld%lld",&n,&x,&y);
for (int i=1; i<=n; i++)
scanf("%lld%lld",&m[i],&a[i]);
for (int state=0; state<(1<<n); state++)
{
int nn=0;
for (int j=1; j<=n; j++)
{
if (state & (1<<(j-1)))
{
mm[++nn]=m[j];
aa[nn]=a[j];
}
}
mm[++nn]=7;
aa[nn]=0;
long long M;
long long tmp=CRT(aa,mm,nn,M);
long long numx,numy;
if (x-1<tmp) numx=0;
else numx=(x-tmp-1)/M+1;
if (y<tmp) numy=0;
else numy=(y-tmp)/M+1;
long long num=numy-numx;
if (nn % 2)
ans+=num;
else ans-=num;
}

printf("Case #%d: %lld\n",cnt++,ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: