HDU5768:Lucky7(中国剩余定理 & 容斥)

来源:互联网 发布:java页面跳转代码 编辑:程序博客网 时间:2024/06/05 22:57

Lucky7

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1938    Accepted Submission(s): 732


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
22 1 1003 25 30 1 100
 

Sample Output
Case #1: 7Case #2: 14
Hint
For 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.
 

Author
FZU
 

Source
2016 Multi-University Training Contest 4

题意:给N,L, R和N个数对(xi,yi),要求L到R之间有几个数K能整除7,且满足对所有的i,K % xi != yi。

思路:中国剩余定理可以求满足K % xi = yi的解,这里使用容斥原理,先求出所有7的倍数ans,减去满足其中一个且为7的倍数,加上满足其中两个个且为7的倍数... ...,因此把% 7 = 0也强制加进去,最后乘法要用快速乘法,否则爆long long。

# include <iostream># include <cstdio># include <cstring>using namespace std;typedef long long LL;const int maxn = 50;LL m[maxn], r[maxn], n, L, R;bool s[maxn];LL extend_Euclid(LL a, LL b, LL &x, LL &y)//扩展欧几里得。{    if(b==0)    {        x = 1; y = 0;        return a;    }    LL r = extend_Euclid(b, a%b, y, x);    y -= a/b*x;    return r;}LL cal(LL r, LL l, LL p){return (r-l)/p;}LL mult(LL a, LL b, LL m){    LL res = 0;    for(;b;b>>=1)    {        if(b&1) res = (res + a)%m;        a = (a<<1)%m;    }    return res;}LL China(LL L, LL R){    LL M = 1, ans = 0;    for (int i = 0; i <= n; ++i) if(s[i]){M *= m[i];}    for(int i = 0;i <= n;i++) if(s[i])    {        LL N = M/m[i];        LL x, y;        extend_Euclid(N, m[i], x, y);        x = (x%m[i] + m[i]) % m[i];        ans = ((ans+mult(r[i]*N%M, x, M))%M + M) % M ;//满足条件的最小解。解系为ans + k*M(k为整数,M为所有质因子的乘积(lcm))。    }    LL ret = cal(R+M, ans, M) - cal(L-1+M, ans, M);//计算解系在区间内有几个解。    return ret;}int main(){    int T, cas=1;    scanf("%d",&T);    while(T--)    {        scanf("%lld%lld%lld",&n,&L,&R);        for(int i=0; i<n; ++i) {scanf("%lld%lld",&m[i],&r[i]);}        m[n] = 7; r[n] = 0, s[n] = true;        LL ans = 0;        int up = 1LL<<n;        for(int i=0; i<up; ++i)        {            int t = i, k = 0;            for(int j=0; j<n; ++j)            {                s[j] = t&1;                t >>= 1;                k += s[j];            }            k = k&1?-1:1;            ans += 1LL*k*China(L, R);        }        printf("Case #%d: %lld\n",cas++, ans);    }    return 0;}


原创粉丝点击