HDU 5768 Lucky7(CRT+容斥)


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


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.


题意:N个条件pi,ai,求出在[x,y]范围内,满足是7的倍数,且不满足被pi除余ai的数字的个数。

思路:假设N=3,n1为%7=0的个数,n2为%7=0且%p1=a1的个数,n3为%7=0且%p2=a2的个数,n4为%7=0且%p3=a3的个数,

n5为%7=0且%p1=a1且%p2=a2的个数,n6为%7=0且%p1=a1且%p3=a3的个数,n7为%7=0且%p2=a2且%p3=a3的个数,

n8为%7=0且%p1=a1且%p2=a2且%p3=a3的个数。则根据容斥原理,ans=n1-n2-n3-n4+n5+n6+n7-n8。

用2^n二进制数枚举每个同余条件是取还是不取,再根据中国剩余定理的模板可以求出满足若干个同余条件的最小非负整数,需要处理在[x,y]内的个数,注意边界。

另外中途乘法可能爆long long,所以用到俄罗斯农民乘法。


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
using namespace std;

typedef long long ll;
ll l,r;
int n;

ll a[20],m[20];
int temp[35000][18];
ll p[20],q[20];

void init()     //预处理二进制数
{
    memset(temp,0,sizeof(temp));
    for(int i=0;i<32768;i++)
    {
        int res=i;
        int cnt=0;
        while(res>0)
        {
            temp[i][cnt]=res%2;
            res/=2;
            cnt++;
        }
    }
}

//ll mul(ll x, ll y,ll mod)   //黑科技乘法
//{
//    return (x*y-(ll)(x/(long double)mod*y+0.001)*mod+mod)%mod;
//}

ll mul(ll x, ll y,ll mod)   //俄罗斯农民乘法
{
    ll ans = 0;
    while(y)
    {
        if(y&1)
            ans=(ans+x)%mod;
        x=(x+x)%mod;
        y>>=1;
        
    }
    return ans;
}

ll pow1(ll M,ll p,ll mod)     //快速幂
{
    ll ans = 1;
    M%=mod;
    while(p)
    {
        if(p&1)
            ans=mul(ans,M,mod);
        M=mul(M,M,mod);
        p>>=1;
    }
    return ans;
}

ll CRT(ll a[],ll m[],int n,ll &M)    //中国剩余定理
{
    M = 1;
    ll ans = 0;
    for(int i=1; i<=n; i++)
        M *= m[i];
    for(int i=1; i<=n; i++)
    {
        ll Mi = M / m[i];
        ll x=pow1(Mi,m[i]-2,m[i]);    //快速幂求逆元,欧几里得法可能会爆long long
        ans = (ans + mul(mul(Mi , x, M), a[i], M)) % M;
    }
    if(ans < 0) ans += M;
    return ans;
}

int main()
{
    int cas=1;
    init();
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%I64d%I64d",&n,&l,&r);
        a[1]=0;     //第一个同余条件恒为%7=0
        m[1]=7;
        ll M;
        for(int i=0;i<n;i++)
        {
            scanf("%I64d%I64d",&p[i],&q[i]);
        }
        ll x=CRT(a,m,1,M);
        ll ans=(r-x)/7-(l-1-x)/7;
        int sum=pow(2,n);
        for(int i=1;i<sum;i++)
        {
            int cnt=1;
            for(int j=0;j<n;j++)
            {
                if(temp[i][j]==1)    //若二进制当前位为1,则考虑此同余条件,加入a,m数组中
                {
                    cnt++;    //注意,a,m数组从2开始更新,第一个条件恒为%7=0
                    a[cnt]=q[j];
                    m[cnt]=p[j];
                }
            }
            x=CRT(a,m,cnt,M);
            if(cnt%2==0)     //偶数个条件则加
            {
                ans=ans-(r-x)/M+(l-1-x)/M;
                if(l-1>=x) ans++;      //注意区间的边界
                if(r>=x) ans--;
            }
            else      //奇数个则减
            {
                ans=ans+(r-x)/M-(l-1-x)/M;
                if(l-1>=x) ans--;
                if(r>=x) ans++;
            }
        }
        printf("Case #%d: %I64d\n",cas++,ans);
    }
    return 0;
}


阅读更多
换一批

没有更多推荐了,返回首页