http://acm.hdu.edu.cn/showproblem.php?pid=5768
Lucky7
problem DescriptionWhen ?? is born, seven Crows flew in and stopped beside him. In its childhood,?? had been unfortunately fall into the sea. While it is dying, seven dolphins arched its body and sent it back to the shore. It's said that?? Used to surrounded by 7 candles if he faced a extremely difficult problem, and always solve it in seven minutes.
?? Once wrote an autobiography, which mentioned something about himself. In its book, it said seven are his favorite number and he thinks that a number can 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 the these prime numbers with a given Remai NDEr AI would bring him bad luck. In this case, many of his lucky numbers is sullied because they can be divisible by 7 and also have 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.
InputThe first line there was 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<Ten ) on a line where n is the number of pirmes.
Following on n lines each contains a integers pi, ai where pi is the Pirme and?? Abhors the numbers has a remainder of AI when they is divided by pi.
It is Guranteed, the PI is distinct and pi!=7.
It's also guaranteed that p1*p2*...*pn<=Ten and 0<ai<pi<=5for 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 Input22 13 25 30 1
Sample OutputCase #1:7Case #2:
HintFor Case 1:7,21,42,49,70,84,91 is the seven numbers. For case2:7,14,21,28,35,42,49,56,63,70,77,84,91,98 is the fourteen numbers. Test instructions: Find out that [L, R] can be divisible by 7 and does not meet the number of any one congruence.
1#include <cstdio>2#include <algorithm>3#include <iostream>4#include <cstring>5#include <queue>6#include <cmath>7 using namespacestd;8typedefLong LongLL;9 #defineN 20Ten One LL P[n], a[n]; A intBit[n]; - intN; - the ll Mul (ll A, ll B, ll M) - { - //Fast multiplication -LL ans =0; + while(b) { - if(B &1) ans = (ans + a)%m; +A <<=1; AA%=m; atb >>=1; - } - returnans; - } - -ll EXGCD (ll A, ll B, ll &x, LL &y) in { - if(b = =0) { tox =1; +y =0; - returnA; the } *LL r = exgcd (b, a%b, x, y); $ intt =x;Panax Notoginsengx =y; -y = t-a/b *y; the returnR; + } A the ll CRT (ll X, ll y) + { - //Chinese remainder theorem: Finding solutions that satisfy multiple congruence equations at the same time $LL M =1, ans =0; $ for(inti =0; I <= N; i++) { - if(Bit[i]) M *=P[i]; - } the for(inti =0; I <= N; i++) { - if(Bit[i]) {Wuyi LL x, y, Mi; theMi = M/P[i]; - exgcd (Mi, P[i], x, y); Wux = (x% p[i] + p[i])%P[i]; -Ans = (ans + mul (Mi * a[i]% m, x, M)% m + m)%M; About //ans is finding the solution of the least positive integer within M of the special solution $ } - } - //each over M can have a solution -LL res = (Y-ans + m)/M-(X-1-ans + M)/M; A returnRes; + } the - voidSolve (ll X, ll y) $ { theBit[n] =1; theLL ans =0; the intall =1<<N; the for(inti =0; i < All; i++) { - intTMP = i, k =0; in for(intj =0; J < N; J + +) { theBIT[J] = tmp &1; theTMP >>=1; AboutK + =Bit[j]; the } theK = k &1? -1:1; the //k is the calculation of how many congruence + //the principle of tolerance: odd minus, even plus, in particular, can see "Combinatorial mathematics" P108 - //calculates the number of a number that does not have a nature (satisfies any one congruence) theAns + = CRT (x, y) *K;Bayi } theprintf"%i64d\n", ans); the } - - intMain () the { the intT; thescanf"%d", &t); the for(intCAS =1; CAS <= t; cas++) { - LL x, y; thescanf"%d%i64d%i64d", &n, &x, &y); the for(inti =0; I < n; i++) thescanf"%i64d%i64d", &p[i], &a[i]);94P[n] =7, a[n] =0; theprintf"Case #%d:", CAs); the solve (x, y); the }98 return 0; About}
HDU 5768:lucky7 (Chinese remainder theorem + tolerant principle)