您的位置:首页 > 其它

中国剩余定理+容斥_____Lucky7( hdu 5768 2016多校第四场 )

2016-08-28 16:47 253 查看
Problem Description

When ?? was born, seven crows flew in and stopped beside him. In its childhood, ?? had been unfortunately fall into the sea. While it was dying, seven dolphins arched its body and sent it back to the shore. It is said that ?? used to surrounded by 7 candles
when he faced a extremely difficult problem, and always solve it in seven minutes. 

?? once wrote an autobiography, which mentioned something about himself. In his book, it said seven is his favorite number and he thinks that a number can be divisible by seven can bring him good luck. On the other hand, ?? abhors some other prime numbers and
thinks a number x divided by pi which is one of these prime numbers with a given remainder ai will bring him bad luck. In this case, many of his lucky numbers are sullied because they can be divisible by 7 and also has a remainder of ai when it is divided
by the prime number pi.

Now give you a pair of x and y, and N pairs of ai and pi, please find out how many numbers between x and y can bring ?? good luck.

 

Input

On the first line there is an integer T(T≤20) representing the number of test cases.

Each test case starts with three integers three intergers n, x, y(0<=n<=15,0<x<y<1018)
on a line where n is the number of pirmes. 

Following on n lines each contains two integers pi, ai where pi is the pirme and ?? abhors the numbers have a remainder of ai when they are divided by pi. 

It is guranteed that all the pi are distinct and pi!=7. 

It is also guaranteed that p1*p2*…*pn<=1018 and
0<ai<pi<=105for
every i∈(1…n).

 

Output

For each test case, first output "Case #x: ",x=1,2,3...., then output the correct answer on a line.

 

Sample Input

2
2 1 100
3 2
5 3
0 1 100

 

Sample Output

Case #1: 7
Case #2: 14

HintFor Case 1: 7,21,42,49,70,84,91 are the seven numbers.
For Case2: 7,14,21,28,35,42,49,56,63,70,77,84,91,98 are the fourteen numbers.

 
题意:
给一个区间[L,R]和n对数ai,pi,问区间有多少个数m满足是7的倍数并且对于任意一对ai,pi都有m%pi != ai。所有pi都是相互互质。

分析:
因为一共最多15对数。那么最多2^15个状态。所以可以用容斥排除重复的部分。可以将7的倍数这一个条件也看做是同余方程组的其中一个。不过要提前判断一下,如果题目给你的pi中有7的倍数。那么判断这一对的ai,如果ai=0.显然无论如何这一对一定满足所以输出0。如果ai!=0显然无论这一对都不会满足。
注意的是在使用中国剩余定理的时候三个数相乘可能会超longlong所以需要使用快速乘法。

代码:
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std;
typedef long long ll;
int k;
ll left,right,ans;
ll m[16],a[16],M[40000],s[40000];
ll gcd(ll a,ll b)
{
return b==0?a:gcd(b,a%b);
}
ll gcd(ll a,ll b,ll &x,ll &y)
{
ll t,ret;
if(!b)
{
x = 1;
y = 0;
return a;
}
ret=gcd(b,a%b,x,y);
t=x;
x=y;
y=t-a/b*y;
return ret;
}
ll mul(ll a,ll b,ll mod)
{
if(b < 0) return (-mul(a,-b,mod)%mod+mod);
if(b == 0) return 0;
if(b == 1) return a%mod;
if(b%2 == 0) return mul((a+a)%mod,b/2,mod);
return (mul((a+a)%mod,b/2,mod)+a)%mod;
}
ll lmes(ll a[],ll m[],int n,int num,ll& tm)
{
ll ret = 0;
tm = 7;
for(int i = 0 ; i < n ; ++i){
if(num&(1<<i))
{
tm*=m[i];
}
}
for(int i = 0 ; i < n ; ++i)
{
if(!(num&(1<<i)))continue;
ll mi = tm/m[i],x,y;
gcd(mi,m[i],x,y);
ret = (ret + mul(mul(a[i],x,tm),mi,tm))%tm;
}
if(ret<0) ret = (ret+tm);
return ret;
}
int bitcount(int num)
{
int c =0;
for (c =0; num; ++c)
{
num &= (num -1) ; // 清除最低位的1
}
return c ;
}
void init()
{
for(int i = 1 ; i < (1<<k) ; ++ i)
{
s[i] = lmes(a,m,k,i,M[i]);
}
ans = right/7 - left/7;
}
ll cnt(ll ta,ll tm)
{
ll cnt1 = right/tm + (right%tm>=ta?1:0);
ll cnt2 = left/tm + (left%tm>=ta?1:0);
//  printf("%lld %lld %lld %lld\n",ta,tm,cnt1,cnt2);
return cnt1 - cnt2;
}
void work()
{
init();
for(int i = 1 ; i < (1<<k) ; ++i)
{
if(bitcount(i)%2==0)
{
ans += cnt(s[i],M[i]);
}
else
{
ans -= cnt(s[i],M[i]);
}
}
printf("%lld\n",ans);
}
int main()
{
int t,_case=0;
scanf("%d",&t);
while(t--)
{
scanf("%d%lld%lld",&k,&left,&right);
left --;
int tot = 0;
bool flag = false;
for(int i = 0 ; i < k ; i ++)
{
scanf("%lld%lld",&m[tot],&a[tot]);
if(m[tot] % 7 == 0)
{
if(a[tot] == 0) flag = true;
else tot --;
}
tot++;
}
k = tot;
printf("Case #%d: ",++_case);
if(flag)printf("0\n");
else
work();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息