Lightoj 1319-monkey Tradition (Chinese remainder theorem)

Source: Internet
Author: User

1319-monkey Tradition
PDF (中文版) Statistics Forum
Time Limit:2 second (s) Memory limit:32 MB

In ' Monkeyland ', there is a traditional game called "Bamboo climbing". The rules of the game are as follows:

1) There is n monkeys who play this game and there is n bamboos of Equal heights. Let the height be L meters.

2) Each monkey stands in front of a bamboo and every monkey is assigned a different bamboo.

3) When the whistle was blown, the monkeys start climbing the bamboos and they are not allowed to jump to a different Bamboo throughout the game.

4) Since They is monkeys, they usually climb by jumping. The ith Monkey can jump exactly pi meters (Pi is a prime). After a while when a monkey finds so he cannot jump because one more jump could get him out of the bamboo, he reports the Remaining length Ri that he was not able to cover.

5) and before the game, each monkey is assigned a distinct pi.

6) The monkey, who has the lowest ri, wins.

Now, the organizers has found all the information of the game last year, but unluckily they haven ' t found the height of T He bamboo. To is more exact, they knowN, all pi and corresponding ri, but not L. So, you came forward and found the task challenging and so, you want to find L, from the given information.

Input

Input starts with an integer T (≤10000), denoting the number of test cases.

Each case is starts with a line containing an integer n (1≤n≤12). Each of the next n lines contains integers pi (1 < pi < Max, Pi is a prime) and ri (0 < R I < pi). All pi would be distinct.

Output

For each case, print the case number and the minimum possible value of L that satisfies the above conditions. If There is no solution, print ' impossible '.

Sample Input Output for Sample Input

2

3

5 4

7 6

11 3

4

2 1

3 2

5 3

7 1

Case 1:69

Case 2:113

Problem setter:tanvir hassanspecial thanks:jane ALAM JAN Code First, theorem later learn;
//Analysis: First, we first study the remainder theorem of China: The remainder theorem of China//in the case of the module Mi 22, the Chinese remainder theorem can be used to satisfy://N=B1 (mod m1)//n=b2 (mod m2)//n=b3 (mod m3)//....//I feel like there's no egg in this analysis ...#include <cstdio>#defineLL Long LongLL p[ $], r[ $];voidEXGCD (ll A, ll B, ll &xx, LL &yy) {    if(b==0) {xx=1; yy=0; return; } EXGCD (b, a%b, xx, yy); LL T=xx; Xx=yy; yy=t-a/b*yy; return;} LL China (ll s[], LL b[],intK//s: modulus; b: Remainder{LL n=1, XX, yy; LL ans=0;  for(intI=0; i<k; i++) n*=S[i];  for(intI=0; i<k; i++) {LL T=n/S[i];        EXGCD (t, S[i], XX, yy); Ans= (Ans+xx*t*b[i])%N; }    return(ans%n+n)%N;}intMain () {intT, N, q=1; scanf ("%d", &t);  while(t--) {scanf ("%d", &N);  for(intI=0; i<n; i++) scanf ("%lld%lld", &p[i], &R[i]); LL ans=China (p, r, N); printf ("Case %d:%lld\n", q++, ans); }     return 0;}

Lightoj 1319-monkey Tradition (Chinese remainder theorem)

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.