HDU5768Lucky7(中国剩余定理+容斥定理)(区间个数统计)

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.

InputOn 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<10181018) 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<=10181018 and 0<ai<pi<=105105for every i∈(1…n). 
OutputFor 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.

题意:

求区间[X,Y]中模7为0,为满足n对关系:膜m[i]不为r[i],问这样的数字有多少。满足m[]为素数,且不为7。

思路:

  • 容斥定理,保证了结果中不多算,不少算,不重复算。
  • 中国剩余定理,求出最小的x=c1满足线性同余方程组,则变成求以c1为起始量,M=∏m[]为等差的数列,在[L,R]中的个数,结合抽屉原理,这里是奇加偶减。
  • 保险起见,全部是long long
  • 这里其实是用的线性同余方程组求解的,如果用中国剩余定理,得用快速除法。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#define ll long long
using namespace std;
ll n,ans,tmp,m[20],r[20];
ll BIT(ll x) { ll res=0; while(x){if(x&1LL) res++;x>>=1;} return res&1LL?-1LL:1LL;}
void Ex_gcd(ll a,ll b,ll &d,ll &x,ll &y)
{
    if(b==0){ d=a; x=1; y=0; return ;};
    Ex_gcd(b,a%b,d,y,x);y-=a/b*x;
}
ll Ex_CRT(ll L,ll R,ll N)
{
    ll a,b,c,c1,c2,x,y,d,M=7;
    a=7;  c1=0;
    for(int i=0;i<n;i++){
        if(!(1LL<<i&N)) continue;// 状态
        M*=m[i];
        b=m[i];c2=r[i]; c=c2-c1;
        Ex_gcd(a,b,d,x,y);
        x=((c/d*x)%(b/d)+b/d)%(b/d);//最小正单元
        c1=a*x+c1;a=a*b/d;
    }
    return (R-c1+M)/M - (L-1-c1+M)/M;//以c1为起始量,M为等差的数列,在[L,R]中的个数。
}
int main()
{
    ll T,x,y,i,Case=0; scanf("%lld",&T);
    while(T--){
        ans=0;tmp=0;
        scanf("%lld%lld%lld",&n,&x,&y);
        for(i=0;i<n;i++)
           scanf("%lld%lld",&m[i],&r[i]);
        for(i=0;i<1LL<<n;i++) ans+=BIT(i)*Ex_CRT(x,y,i);
        printf("Case #%lld: %lld\n",++Case,ans);
    }   return 0;
}
时间: 2024-09-27 22:57:46

HDU5768Lucky7(中国剩余定理+容斥定理)(区间个数统计)的相关文章

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5768 给你n个同余方程组,然后给你l,r,问你l,r中有多少数%7=0且%ai != bi. 比较明显的中国剩余定理+容斥,容斥的时候每次要加上个(%7=0)这一组. 中间会爆longlong,所以在其中加上个快速乘法(类似矩阵快速幂).因为普通的a*b是直接a个b相加,很可能会爆.但是你可以将b拆分为二进制来加a,这样又快又可以防爆. 1 //#pragma comment(linker, "/S

hdu_5768_Lucky7(中国剩余定理+容斥)

题目链接:hdu_5768_Lucky7 题意: 给你一个区间,问你这个区间内是7的倍数,并且满足%a[i]不等于w[i]的数的个数 乍一看以为是数位DP,仔细看看条件,发现要用中国剩余定理,然后容斥一下就出答案了,不过这里在中国剩余定理里面的乘法会有数据爆long long ,所有要写一个高精度乘法,这里卡死很多人. 1 #include <bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;i++) 3 using namespace

hdu 5768(中国剩余定理+容斥)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5768: 题目分析: 因为满足任意一组pi和ai,即可使一个"幸运数"被"污染",我们可以想到通过容斥来处理这个问题.当我们选定了一系列pi和ai后,题意转化为求[x,y]中被7整除余0,且被这一系列pi除余ai的数的个数,可以看成若干个同余方程联立成的一次同余方程组.然后我们就可以很自然而然的想到了中国剩余定理.需要注意的是,在处理中国剩余定理的过程中,可能会发生超出L

题解报告:hdu 4135 Co-prime(容斥定理入门)

Problem Description Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N.Two integers are said to be co-prime or relatively prime if they have no common positive divisors other than

HDU1796 How many integers can you find【容斥定理】

题目链接: http://acm.hdu.edu.cn/showproblem.php? pid=1796 题目大意: 给你一个整数N.和M个整数的集合{A1.A2.-.Am}.集合内元素为非负数(包括零),求小于N的 正整数(1~N-1)中,能被M个整数的集合中随意一个元素整除的正整数个数. 比如N = 12.M = {2,3},在1~N-1中,能被2整除的数为{2,4,6.8.10},能被3整除的数为 {3.6,9}.则所求集合为{2,3,4.6,8,9,10},共7个,则答案为7. 思路:

HDU 1695 GCD(容斥定理)

GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7529    Accepted Submission(s): 2773 Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y

GCD SUM 强大的数论,容斥定理

GCD SUM Time Limit: 8000/4000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus Problem Description 给出N,M执行如下程序:long long  ans = 0,ansx = 0,ansy = 0;for(int i = 1; i <= N; i ++)   for(int j = 1; j <= M; j ++)       if(gcd(i,j)

poj1091跳蚤(容斥定理)

题目:Z城市居住着很多只跳蚤.在Z城市周六生活频道有一个娱乐节目.一只跳蚤将被请上一个高空钢丝的正中央.钢丝很长,可以看作是无限长.节目主持人会给该跳蚤发一张卡片.卡片上写有N+1个自然数.其中最后一个是M,而前N个数都不超过M,卡片上允许有相同的数字.跳蚤每次可以从卡片上任意选择一个自然数S,然后向左,或向右跳S个单位长度.而他最终的任务是跳到距离他左边一个单位长度的地方,并捡起位于那里的礼物.比如当N=2,M=18时,持有卡片(10, 15, 18)的跳蚤,就可以完成任务:他可以先向左跳10

AtCoder Beginner Contest 152 - F - Tree and Constraints (容斥定理+树上路径的性质)

AtCoder Beginner Contest 152 - F - Tree and Constraints (容斥定理+树上路径的性质) We have a tree with NN vertices numbered 11 to NN. The ii-th edge in this tree connects Vertex aiai and Vertex bibi. Consider painting each of these edges white or black. There ar