lightoj 1319 - Monkey Tradition (中国剩余定理)

来源:互联网 发布:安徽省大数据产业协会 编辑:程序博客网 时间:2024/05/17 22:35
1319 - Monkey Tradition
PDF (English)StatisticsForum
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 are N monkeys who play this game and there are 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 is blown, the monkeys start climbing the bamboos and they are not allowed to jump to a different bamboo throughout the game.

4)       Since they are monkeys, they usually climb by jumping. And in each jump, the ith monkey can jump exactly pi meters (pi is a prime). After a while when a monkey finds that he cannot jump because one more jump may get him out of the bamboo, he reports the remaining length ri that he is 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 have found all the information of the game last year, but unluckily they haven't found the height of the bamboo. To be 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 starts with a line containing an integer n (1 ≤ n ≤ 12). Each of the next n lines contains two integers pi (1 < pi < 40, pi is a prime) and ri(0 < ri < pi). All pi will 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

 这道题可以采用中国剩余定理,但是只能处理两两互质的情况,看了别人代码有些地方还是不特别的懂。

先说一下我的理解举个例子:

5     4

7     6

11    3

设要求的数是X:

X=5(mod 4)

x=7 (mod 6)

x=11 (mod 3)

在中国剩余定理中有这样情况

我们先从n1这个角度出发,已知n1满足除以5余4,能不能使得 n1+n2 的和仍然满足除以5余4?进而使得n1+n2+n3的和仍然满足除以5余4?

这是成立的即:

  1. 为使n1+n2+n3的和满足除以5余4,n2和n3必须是5的倍数。
  2. 为使n1+n2+n3的和满足除以7余6,n1和n3必须是7的倍数。
  3. 为使n1+n2+n3的和满足除以11余3,n1和n2必须是11的倍数。

    因此,为使n1+n2+n3的和作为“孙子问题”的一个最终解,需满足:

  1. n1除以5余4,且是11和7的公倍数。
  2. n2除以7余6,且是5和11的公倍数。
  3. n3除以11余3,且是7和5的公倍数。 
这里有个技巧比如说对于5来说从7和11公倍数中找到一个最小的数除以5余数为1,再乘以4,每个都是这样算然后将算出得数相加,这样求出的并不是最小的X,需要对X处理求余,即X%(5*7*11);

<span style="font-family:Courier New;font-size:24px;">#include<algorithm>#include<iostream>#include<cstring>#include<cmath>using namespace std;int pi[20],ri[20];int n;long long sum,ans;void gcd(long long a,long long b,long long &x,long long &y){if(b==0){x=1;y=0;return ;}gcd(b,a%b,x,y);long long t=x;x=y;y=t-y*(a/b);return ;}long long CRT(){long long x,y;for(int i=1;i<=n;i++){long long s=sum/pi[i];gcd(s,pi[i],x,y);ans=(ans+s*ri[i]*x)%sum;}return (ans%sum+sum)%sum;}int main(){int t,cas=1;scanf("%d",&t);while(t--){scanf("%d",&n);sum=1;for(int i=1;i<=n;i++){scanf("%d%d",&pi[i],&ri[i]);sum*=pi[i];}ans=0;ans=CRT();printf("Case %d: %lld\n",cas++,ans);}return 0;}</span>

0 0