1319 - Monkey Tradition
1) There are N monkeys who play this game and there are N bamboos of equal heights. Let the height be L meters.In 'MonkeyLand', there is a traditional game called "Bamboo Climbing". The rules of the game are as follows:
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 know N, 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 |
235 47 6
11 3 4 2 1 3 2 5 3 7 1 |
Case 1: 69Case 2: 113 |
题目类型:CRT
算法分析:直接使用中国剩余定理求解即可,注意在迭代求解时要注意%M防止溢出ans = (ans % M + M) % M,本题不存在Impossible的情况
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r using namespace std; const int INF = 0x7FFFFFFF; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int MOD = 7; const int maxn = 10 + 66; long long M, n, a[maxn], r[maxn]; long long gcd_ex (long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1; y = 0; return a; } long long d = gcd_ex (b, a % b, y, x); y = y - a / b * x; return d; } long long CRT () { M = 1; long long i, Mi, x, y, d, ans = 0; for (i = 0; i < n; i++) M *= a[i]; for (i = 0; i < n; i++) { Mi = M / a[i]; d = gcd_ex (Mi, a[i], x, y); ans = (ans + Mi * x * r[i]) % M; } ans = (ans % M + M) % M; return ans; } int main() { // freopen ("aaa.txt", "r", stdin); int t, flag = 1; scanf ("%lld", &t); while (t--) { printf ("Case %d: ", flag++); scanf ("%lld", &n); for (int i = 0; i < n; i++) scanf ("%lld%lld", &a[i], &r[i]); long long ans = CRT (); printf ("%lld\n", ans); } return 0; } |
- « 上一篇:lightoj1294
- lightoj1325:下一篇 »