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

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(T20) 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 p1p2pn<=1018 and 0<ai<pi<=105 for every i(1n) .
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.

这题,诶,读完题就知道是什么思路了,除数都是质数,而且两两不等,明显的中国剩余定理的普通形式,而且才15个,明显的互斥原理,二进制枚举所有情况就行。。。但是当时训练心态已经崩了,被一到简单题搞死。。。这题坑点还不少,还有超long long的情况要处理,今天静下心来,写完了它。

分析:
假设根本没有同余式,那么答案就是:

ans=y7x17

但是,因为有了同余式,会有某些即使7的倍数有满足这些同余式的数,那这些数应该剔除,很显然的容斥。
我们现在来求假设在n组同余式下,范围在 [1,y] 内有哪些数既是7的倍数,又是全部满足这些同余式的个数
也就是:
7x=a1(mod p1)
7x=a2(mod p2)
...
7x=an(mod pn)

根据中国定理我们有:
7x=Mk+i=1naitiMi
M=p1p2pnMi=Mpi,tiMiti=1(mod pi)()

那么其实可以写成
7x=Mk+M

现在我们要看的就是有多少的x满足这个式子,这个显然是用扩展欧几里得来求一个特解,设:
7x=Mk+M
那么
x=xM+Mk(xM<M)(k)

因为:
1<=x<=y7=num

所以:
ans=numxMM+1(num>=xM),ans=0

代码实现上要注意longlong的溢出

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#define N 50005
using namespace std;
typedef long long ll;
void gcd(ll a,ll b,ll& d,ll& x,ll& y)
{
    if(!b)
    {
        d=a;
        x=1;
        y=0;
    }
    else
    {
        gcd(b,a%b,d,y,x);
        y-=x*(a/b);
    }
}//扩展欧几里得
ll quickmul(ll a, ll b, ll mod) {
    a %= mod;
    ll ret = 0;
    while(b) {
        if(b & 1) ret = (ret + a) % mod;
        b >>= 1;
        a = (a + a) % mod;
    }
    return ret;
}//防爆long long
ll getInv(ll  M,ll m)//求逆元
{
    ll d,x,y;
    gcd(M,m,d,x,y);
    while(x<0)
    x+=m;
    return x;
}
int m[20];
int a[20];
int n;
int main()
{
    int t;
    long long x,y;
    int ca=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%lld%lld",&n,&x,&y);
        for(int i=0;i<n;i++)
            scanf("%d%d",m+i,a+i);

        ll ans1=y/7;
        ll num1=ans1;
        ll ans2=(x-1)/7;
        ll num2=ans2;//x,y相当于求两次而已
        for(int s=1;s<(1<<n);s++)
        {
            int countt=0;//偶数则加,由容斥得
            long long M=1;
            for(int j=0;j<n;j++)
                if((1<<j)&s)
                {
                    countt++;
                    M*=m[j];
                }
            //cout<<"haha"<<endl;
            ll x0=0;
            for(int j=0;j<n;j++)
                if((1<<j)&s)
                    x0=(x0+quickmul(M/m[j],getInv(M/m[j],m[j])*a[j]%M,M))%M;
            //cout<<x0<<" "<<M<<endl;
            ll x,y,d;
            gcd(7,M,d,x,y);
            while(x<0)
                x+=M;
            x=quickmul(x,x0,M);
            //cout<<"x:"<<x<<" "<<(num1-x)/M<<endl;
            x-=M;//x为负数的好处就不需要加1,也不需要判断大小
            if(countt&1)
            {
                ans1-=(num1-x)/M;
                ans2-=(num2-x)/M;   
            }
            else
            {
                ans1+=(num1-x)/M;
                ans2+=(num2-x)/M;
            }
        }
        printf("Case #%d: %lld\n",ca++,ans1-ans2);  
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值