HDU 5768lucky7 (Multi-school fourth field) tolerance + Chinese remainder theorem (extended Euclidean negation) + fast multiplication

Source: Internet
Author: User

Address: http://acm.hdu.edu.cn/showproblem.php?pid=5768

Lucky7

Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 754 Accepted Submission (s): 279

Problem descriptionwhen?? Was 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.

Inputon The 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 1 1003 25 30 1 100

Sample outputcase #1:7Case #2:14 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: Just ask how many lucky numbers, and then the lucky number, is to be divisible by 7 number, but these numbers may be contaminated, which will be contaminated, there are n methods will be contaminated, each method gives an AI, a pi, if the lucky number%pi equals AI will be contaminated, and then give you an interval, Ask how many lucky numbers have not been contaminated in this interval. The problem: This question was first known to be a Chinese residual theorem, and then read a number of problems, found to use the inclusion of the Chinese remainder theorem, and then found to explode long long, you must add a fast multiplication =. =, and then the inverse of the element can not be used Fermat theorem =.  =, because the fast power to explode long long, with fast multiplication will T (I good vagetable a), and finally only learned a wave of Euclid to seek inverse yuan tat. First to the calibration process to see what he is writing, and then found that the standard process is the first state compression, and then the repulsion =. =, look at the marking process for a long time, I am not hopeless qaq. Code:
1#include <cstdio>2#include <cmath>3#include <iostream>4#include <algorithm>5#include <vector>6#include <stack>7#include <cstring>8#include <queue>9#include <Set>Ten#include <string> One#include <map> A #defineINF 9223372036854775807 - #defineINF 9e7+5 - #definePI ACOs (-1) the using namespacestd; -typedefLong Longll; -typedefDoubledb; -typedefLong LongLL; + Const intMAXN = 1e2 +5; - Const intMoD = 1e9 +7; + ConstDB EPS = 1e-9; A ll N, L, R, Ai[maxn], PI[MAXN]; at  - intBitcount (ll x) { -     returnX? Bitcount (x >>1) + (X&AMP;1LL):0; -}//the number of equations to be selected for the current state -  - ll Q_mul (ll A, ll B, ll MoD) { inLL ret =0; -      while(b) { to         if(B &1) ret = (ret + a)%MoD; +b >>=1; -A = (a + a)%MoD; the     } *     returnret; $}//Fast multiplicationPanax Notoginseng  - voidEX_GCD (ll A, ll B, ll &x, LL &y) { the ll D; +     if(b==0){ Ax=1, y=0; the         return; +     } -EX_GCD (b,a%b,y,x); $y-=a/b*x; $}//here is a direct copy of the function of the standard range.  -  - ll CRT (ll p[], LL a[], ll CNT) { thell M =1; -ll res =0;Wuyi      for(inti =0; I <= CNT; i++) M *=P[i]; the      for(inti =0; I <= CNT; i++) { -ll m = m/P[i], x, y; Wu EX_GCD (M, P[i], x, y); -Res = (res + q_mul (Q_mul (x, M, m), A[i], m))%M; About     } $     return(res + M)%M; - } -  - ll solve (ll x) { All P[MAXN], m[maxn], ans =0; +p[0] =7, m[0] =0; the     if(!x)return 0; -      for(LL i =1; I < (1LL << N); i++) { $ll num = Bitcount (i), t =7, cnt =0; the          for(LL j =0; J < N; J + +) { the             if(I & (1LL <<j)) { theP[++CNT] =Pi[j]; theM[CNT] =Ai[j]; -T *=Pi[j]; in             } the         } thell res =CRT (P, M, CNT); About         if(Res > x)Continue; the         if(Num &1) ans-= (x-res)/T +1;//Let's just try . the         ElseAns + = (x-res)/T +1; the     } +     returnAns + x/7; - } the Bayi intMain () { the     //Cin.sync_with_stdio (false); the     //freopen ("Tt.txt", "R", stdin); -     //freopen ("Isharp.out", "w", stdout); -     intT, CAS =1; the  theCIN >>T; the      while(t--) { theCIN >> N >> L >>R; -          for(inti =0; I < n; i++) theCIN >> Pi[i] >>Ai[i]; theprintf"Case #%d:%i64d\n", cas++, Solve (R)-Solve (l1)); the     }94     return 0; the}

HDU 5768lucky7 (Multi-school fourth field) tolerance + Chinese remainder theorem (extended Euclidean negation) + fast multiplication

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.