hdu5768Lucky7+容斥+中国剩余定理

本文介绍了一个具体的数学问题求解过程,通过使用容斥原理和中国剩余定理来找出指定区间内既为7的倍数又不符合特定条件的整数数量。文章详细解释了解题思路并提供了实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

Author
FZU

Source
2016 Multi-University Training Contest 4

题意:给出一个区间,求出区间中是7的倍数但是膜mi不等于ai的数的个数。
解法:显然是容斥。求出是7的倍数,然后与一个膜mi等于ai的,两个。。三个。。容斥搞就好了。。对于膜mi等于ai的情况。考虑中国剩余定理,直接将膜7等于0加进去。算出满足条件的第一个数,然后他是对M取膜。然后就直接求出区间中数的个数就好了。
注意:中国剩余定理里的乘法哪里可能会超long long用一种类似于快速幂的方法直接算出a*b%m的值就好。(被卡了这里一直wa…..)

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#define LL long long
using namespace std;

LL extend_gcd(LL a,LL b,LL &x,LL &y){
    if(b==0){
        x=1;y=0;
        return a;
    }
    else{
        LL r=extend_gcd(b,a%b,y,x);
        y-=x*(a/b);
        return r;
    }
}
LL mult(LL a, LL k, LL m){  //a*k%m
    LL res=0;
    while(k){
        if(k&1LL) res=(res+a)%m;
        k>>=1;
        a=(a<<1)%m;
    }
    return res;
}
LL CRT(LL a[],LL m[],int n,LL M){
    //LL M=1;
    //for(int i=0;i<n;i++) M*=m[i];
    LL ret=0;
    for(int i=0;i<n;i++){
        LL x,y;
        LL tm=M/m[i];
        extend_gcd(tm,m[i],x,y);
        x=(x%m[i]+m[i])%m[i];
        ret=(ret+mult(a[i]*tm%M,x,M))%M;
    }
    return (ret+M)%M;
}
int main(){

    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int t;
    scanf("%d",&t);
    for(int fo=1;fo<=t;fo++){
        int n;
        LL x,y;
        LL a[20],m[20];
        scanf("%d %I64d %I64d",&n,&x,&y);
        for(int i=0;i<n;i++) scanf("%I64d %I64d",&m[i],&a[i]);
        LL ans=y /7-(x-1)/7;
        LL tempa[20],tempm[20];
        int zt=(1<<n)-1;
        for(int i=1;i<=zt;i++){

            tempa[0]=0;
            tempm[0]=7;
            int cnt=1;
            for(int j=0;j<n;j++){
                if(i&(1<<j)){
                    tempa[cnt]=a[j];
                    tempm[cnt]=m[j];
                    cnt++;
                }
            }

            LL M=1;
            for(int j=0;j<cnt;j++) M*=tempm[j];
            LL chu=CRT(tempa,tempm,cnt,M);

            LL ans1=(y+M-chu)/M-(x-1+M-chu)/M;
            if(cnt%2==0) ans-=ans1;
            else ans+=ans1;
        }
        printf("Case #%d: ",fo);
        printf("%I64d\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值