hud5768中国剩余定理加容斥原理

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<10181018) 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<=10181018 and 0<ai<pi<=105105for 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



        
  

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.

        
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
#define maxn 20
using namespace std;
ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if (b == 0) {x = 1; y = 0; return a;}
    ll d = exgcd(b, a%b, y, x);
    y -= a / b * x;
    return d;
}
ll mul(ll a,ll b,ll MOD)
{
    a%=MOD; b%=MOD;
    ll res = 0;
    while(b)
    {
        if (b&1) res = (res + a) % MOD;
        a <<= 1; if(a > MOD) a-=MOD;
        b >>= 1;
    }
    return res;
}
void cr(ll &ans,ll &M, ll a[],ll m[],int k) //X = a[i] ( mod m[i] )(m[i]两两互质)
{                                            //解为 X = ans + M * t (0 <= ans <= M)
    M = 1, ans = 0;
    ll x, y, mi;
    for (int i = 0; i < k; i++) M *= m[i];
    for (int i = 0; i < k; i++)
    {
        mi = M / m[i];
        exgcd(m[i], mi, x, y);
        ans = (ans + mul( mul(y, mi, M), a[i], M)) % M;
    }
    if(ans < 0) ans += M;
}
int t,n;
ll x,y;
ll m1[maxn];
ll a1[maxn];
ll m[maxn];
ll a[maxn];
ll cal(ll m,ll a)
{
    ll x0=x+m-a;
    ll y0=y+m-a;
    return y0/m-(x0-1)/m;
}
int main()
{
    scanf("%d",&t);
    int w=0;
    while(t--)
    {w++;
        scanf("%d%lld%lld",&n,&x,&y);
        int cnt=0;
        for(int i=0;i<n;i++)
            scanf("%lld%lld",&m1[i],&a1[i]);
        ll ans=cal(7,0);
        for(int i=1;i<(1<<n);i++)
        {
            cnt=0;
            for(int j=0;j<n;j++)
            {
                if(i&(1<<j))
                {
                    m[cnt]=m1[j];
                    a[cnt]=a1[j];
                    ++cnt;
                }
            }
            m[cnt]=7;
            a[cnt]=0;
            ++cnt;
            ll M,A;
            cr(A,M,a,m,cnt);
            if(cnt&1)
                ans+=cal(M,A);
            else
                ans-=cal(M,A);
        }
        printf("Case #%d: %lld\n",w,ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/89785672