您的位置:首页 > 其它

lightoj 1319 - Monkey Tradition 【CRT】

2015-11-08 12:36 1021 查看
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

 

PROBLEM SETTER: TANVIR HASSAN
SPECIAL THANKS: JANE ALAM JAN

题意:CRT裸题吧,注意题目只要最小的非负解,0满足。

AC代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN 50
#define MAXM 50000000
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
void exgcd(LL a, LL b, LL &d, LL &x, LL &y)
{
if(b == 0) {d = a, x = 1, y = 0;}
else
{
exgcd(b, a%b, d, y, x);
y -= x * (a / b);
}
}
LL gcd(LL a, LL b){
return b == 0 ? a : gcd(b, a%b);
}
void CRT(int l, int r, LL *m, LL *a)
{
LL LCM = 1;
for(int i = l; i <= r; i++)
LCM = LCM / gcd(LCM, m[i]) * m[i];
for(int i = l+1; i <= r; i++)
{
LL A = m[l], B = m[i], d, x, y, c = a[i]-a[l];
exgcd(A, B, d, x, y);
if(c % d)
{
printf("Impossible\n");
return ;
}
LL mod = m[i] / d;
LL k = ((x * c / d) % mod + mod) % mod;
a[l] = m[l] * k + a[l];
m[l] = m[i] / d * m[l];
}
//if(a[l] == 0)
//    a[l] = LCM;
printf("%lld\n", a[l]);
}
LL a[MAXN], m[MAXN];
int main()
{
int t, kcase = 1;
Ri(t);
W(t)
{
int n; Ri(n);
for(int i = 0; i < n; i++)
Rl(m[i]), Rl(a[i]);
printf("Case %d: ", kcase++);
CRT(0, n-1, m, a);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: