HDU5768 Lucky7[中国剩余定理+容斥定理]

E - Lucky7
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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<) 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<= and 0<ai<pi<=for 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组同余式  求出[x,y]中7的倍数的个数 但是要剔除掉符合同余式的数  输出剩余还有多少个7的倍数


先用容斥挑同余方程  然后利用容斥的特性  把重复的去掉

以前没有做过中国剩余定理的题目  所以比赛期间并没有想到

后来去看了一下中国剩余定理  然后再写的这个题 公式运用不怎么熟

一开始我是想起sgu106那种列公式  Mx+ans=7y这种方法来算出个数的

但是后来我想到把  x%7==0这条公式也丢到同余式里面计算
因为题目 p1*p2*……*pn<=1e18   再乘上7 也不会爆long long     ( long long 达到9*1e18 )

因为题解上面说有可能会在求中国剩余定理的时候爆long long  所以用到了快速加 就是那个add的函数


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#define e edge[num[i]]
using namespace std;
typedef long long ll;
const int N=20;
ll x,y,yu,yd;
int n;
int num[N];
struct node
{
    int a,p;
}edge[N];
ll upper(ll a,ll b)//向上取整
{
    if (a<=0)
        return a/b;
    return (a-1)/b+1;
}
ll lower(ll a,ll b)//向下取整
{
    if (a>=0)
        return a/b;
    return (a+1)/b-1;
}
ll exgcd(ll a, ll b,ll &x,ll &y)
{
    if (!b)
    {
        x=1;
        y=0;
        return a;
    }
    ll gcd=exgcd(b,a%b,x,y);
    ll tmp=x;
    x=y;
    y=tmp-a/b*x;
    return gcd;
}
ll add(ll a,ll b,ll m)
{
    ll ans=0;
    while (b)
    {
        if (b&1)
            ans=(ans+a)%m;
        a=(a+a)%m;
        b>>=1;
    }
    return ans;
}
ll crt(int s)
{
    ll M=7,ans=0,lc=1;//把M初始化为7 即把  x%7==0加进去
    for (int i=0 ; i<s ; i++)
        M*=e.p;
    for (int i=0 ; i<s ; i++)
    {
        ll x,y;
        ll Mi=M/e.p;
        exgcd(Mi,e.p,x,y);
        ans=(ans+add(add(x,Mi,M),e.a,M)+M)%M;// ans可能是一个负数 取模的时候注意
    }
    return (y+M-ans)/M-(x-1+M-ans)/M;//范围是在[x,y]里面取 包括了x 所以取x-1
}

ll rc()
{
    if (!n)
        return yu-yd+1;
    ll ans=0;
    for (int i=1 ; i<(1<<n) ; i++)
    {
        int s=0;
        for (int j=0 ; j<n ; j++)
            if (i&(1<<j))
                num[s++]=j;
        if(s&1)
            ans+=crt(s);
        else
            ans-=crt(s);
    }
    return yu-yd+1-ans;
}
int main()
{
//    FILE *fp=fopen("1005.in","r");
    int T;
    scanf("%d",&T);
    for (int test=1 ; test<=T ; test++)
    {
        scanf("%d%lld%lld",&n,&x,&y);
        for (int i=0 ; i<n ; i++)
            scanf("%d%d",&edge[i].p,&edge[i].a);
        yd=upper(x,7);
        yu=lower(y,7);
        printf("Case #%d: %lld\n",test,rc());
    }
    return 0;
}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ControlBear/article/details/52141086
上一篇HDU1796 How many integers can you find[容斥定理]
下一篇LightOJ1028 1090 1138 Trailing Zeroes (I)(Ⅱ)(Ⅲ)[算数基本定理/打表/打表+二分]
想对作者说点什么? 我来说一句
相关热词

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

关闭
关闭
关闭