【HDU 5768】Lucky7(CRT+容斥)

【HDU 5768】Lucky7(CRT+容斥)

Lucky7

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

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(T20) representing the number of test cases.
Each test case starts with three integers three intergers n, x,y(0n15,0<x<y<108) 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<=108 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.

Author
FZU

Source
2016 Multi-University Training Contest 4

题目大意。
输入n,l,r,表示n个不等式还有区间[l,r]
之后n个不等式由 mi ri 组成。(保证 mi 为素数)
表示所需查找的x对于任何一个不等式都要满足 x!=ri(modmi)

现要找出[l,r]中能被7整除且满足n个不等式的数。

因为 mi 为素数,7也是素数。
所以可以用CRT搞一下
先找出能被7整除的,然后减去被7和 mi 同时整出的,加上被7和 mi 还有 mj 同时整除的

也就是容斥那样搞一下。

CRT中直接乘有数据会爆LL,改成拆位乘法取余。

还有种方式是不搞7,因为 ==0(mod7) 这一条件都要满足。
可以在求出除了7之外的数的m和r后,用m乘7的逆元来搞一下。
这样可以避开那组数据。

代码如下:

#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread(ch) freopen(ch,"r",stdin)
#define fwrite(ch) freopen(ch,"w",stdout)

using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const int mod = 1e9+7;
const double eps = 1e-8;

LL extend_gcd(LL a,LL b,LL &x,LL &y)
{
    if(a == 0 && b == 0) return -1;
    if(b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }
    LL d = extend_gcd(b,a%b,y,x);
    y -= a/b*x;
    return d;
}

LL a[33],m[33];
LL tmp[233];

//按位乘法取余
LL mult(LL a,LL b,LL m)
{
    LL ans = 0;
    int tp = 0;
    while(a)
    {
        tmp[tp++] = a%10;
        a /= 10;
    }

    for(int i = tp-1; i >= 0; --i)
    {
        ans = (ans*10+(tmp[i]*b)%m)%m;
    }
    return ans;
}

LL M;
//模线性方程(互素的)
LL CRT(int n,int pos)
{
    M = 1;
    LL ans = 0;
    for(int i = 0; i < n; ++i)
    {
        if(((pos>>i)&1) == 0) continue;
        M *= m[i];
    }
    for(int i = 0; i < n; ++i)
    {
        if(((pos>>i)&1) == 0) continue;
        LL x,y;
        LL Mi = M/m[i];
        extend_gcd(Mi,m[i],x,y);
        //if(pos == 15) printf("%lld %lld %lld %lld %lld %lld\n",a[i],x,y,Mi,m[i],x*Mi+m[i]*y);
        //if(pos == 15 )printf("%lld %lld %lld\n",x,a[i]*Mi,M);
        ans = (ans+(mult(mult(Mi,a[i],M),x,M)))%M;
    }
    if(ans < 0) ans += M;
    return ans;
}

bool vis[65536];
LL l,r;
//容斥
LL Rc(int n,int pos)
{
    if(vis[pos]) return 0;
    vis[pos] = 1;
    LL ans = 0;
    LL x = CRT(n,pos);
    if(r >= x) ans = (r-x)/M+1;
    //printf("%d pos:%d ans:%lld x:%lld mod:%lld\n",n,pos,ans,x,M);
    LL pre = ans;
    if(l >= x)
    {
        ans -= (l-x)/M+1;
        if((l-x)%M == 0) ans++;
        //printf("now:%d pos:%d ans:%lld x:%lld mod:%lld\n",n,pos,pre-ans,x,M);
    }


    for(int i = 1; i < n; ++i)
    {
        if((pos>>i)&1) continue;
        ans += Rc(n,pos|(1<<i));
    }

    return -ans;
}

int main()
{
    //fread("1005.in");
    //fwrite("test.out");

    int t;
    int n;

    scanf("%d",&t);

    for(int z = 1; z <= t; ++z)
    {
        scanf("%d%lld%lld",&n,&l,&r);
        n++;
        m[0] = 7;
        a[0] = 0;

        for(int i = 1; i < n; ++i)
            scanf("%lld%lld",&m[i],&a[i]);

        memset(vis,0,sizeof(vis));
        printf("Case #%d: %lld\n",z,-Rc(n,1));
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值