Lucky7(容斥原理)

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.
 
说出来你可能不信,现在我要分析这个问题,我的博客从来就不会有代码,除了模板代码,,,,
题意: 不懂的看看。。。
就是X-Y 中的除以pi,余数不能为ai,求这样的所有的数,,,
现在我再想的是是不是把所有的整除7的都算出来, 然后|!A1∩!A2∩......∩!An|  = sum-奇数的+偶数的组合,,就是典型的容斥原理啊。。。
!A1使我们要求的东西,,不如让!A1代表除以pi,余数不为ai的东西,然后A1就是代表除以pi余数=ai的情况。
sum=y/7-(x-1)/7;   然后我们现在的重中之重就是求A1 这个东西怎么求,,,,我们注意到的东西就是0<pi<1e5;  但是我们发现了一个小小点的问题,,,
如果x%p[i]=y; 这个y在前面出现过那么的话 ,我们设前一个modp[i]=y的数为s,那么(x+7)%p[i]=(s+7)%p[i]; 所以我们可以在1e5的时间找到modp[i]=a[i]的数,因为这个东西是有周期的啊、、、然后发现我好想发现的没有用的东西,然后这个题还是做不出来,然后就是看别人的代码想到了中国剩余定理,然后这个题好像还有一个小的trick ,就是说乘的时候会爆longlong 这他妈的就尴尬啊。。。然后根据我的判断现在只能二分乘法了。。。。
 
 
 
 
 
 
 
 
 
 
原文地址:https://www.cnblogs.com/Heilce/p/6403546.html