标签:
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 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.
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 |
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<string> #include<iostream> #include<queue> #include<cmath> #include<map> #include<stack> #include<set> using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) const int INF=0x3f3f3f3f; typedef long long LL; const int maxn=1e6+100; void exgcd(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 m[],LL b[],int k)//m:模数 b:余数 { LL n=1,xx,yy; LL ans=0; for(int i=0;i<k;i++) n*=m[i]; for(int i=0;i<k;i++) { LL t=n/m[i]; exgcd(t,m[i],xx,yy); ans=(ans+xx*t*b[i])%n; } return (ans%n+n)%n; } LL p[45],r[45]; int main() { int t,n,cas=1; scanf("%d",&t); while(t--) { scanf("%d",&n); REP(i,n) scanf("%lld%lld",&p[i],&r[i]); LL ans=China(p,r,n); printf("Case %d: %lld\n",cas++,ans); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
Light OJ 1319 Monkey Tradition(中国剩余定理)
标签:
原文地址:http://blog.csdn.net/u013582254/article/details/47415049