Hello Kiki
Problem Description
One day I was shopping in the supermarket. There was a cashier counting coins seriously when a little kid running and singing "门前大桥下游过一群鸭,快来快来 数一数,二四六七八". And then the cashier put the counted coins back morosely and count again...
Hello Kiki is such a lovely girl that she loves doing counting in a different way. For example, when she is counting X coins, she count them N times. Each time she divide the coins into several same sized groups and write down the group size Mi and the number of the remaining coins Ai on her note.
One day Kiki's father found her note and he wanted to know how much coins Kiki was counting.
Input
The first line is T indicating the number of test cases.
Each case contains N on the first line, Mi(1 <= i <= N) on the second line, and corresponding Ai(1 <= i <= N) on the third line.
All numbers in the input and output are integers.
1 <= T <= 100, 1 <= N <= 6, 1 <= Mi <= 50, 0 <= Ai < Mi
Output
For each case output the least positive integer X which Kiki was counting in the sample output format. If there is no solution then output -1.
Sample Input
2
2
14 57
5 56
5
19 54 40 24 80
11 2 36 20 76
Sample Output
Case 1: 341
Case 2: 5996
Author
digiter (Special Thanks echo)
Source
2010 ACM-ICPC Multi-University Training Contest(14)——Host by BJTU
题目类型:线性同余方程组
算法分析:直接转化为X = Ai mod Mi求解即可
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
#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 n, a[maxn], r[maxn]; long long gcd (long long a, long long b) { if (b == 0) return a; return gcd (b, a % b); } 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 mod_equ_set () { long long M = a[0], R = r[0], x, y, d; for (int i = 1; i < n; i++) { d = gcd_ex (M, a[i], x, y); if ((r[i] - R) % d) return -1; x = (r[i] - R) / d * x % (a[i] / d); R += x * M; M = M / d * a[i]; R %= M; } return R > 0 ? R : R + M; } int main() { // ifstream cin ("aaa.txt"); int t, flag = 1; cin >> t; while (t--) { cout << "Case " << flag++ << ": "; cin >> n; long long lcm = 1; for (int i = 0; i < n; i++) { cin >> a[i]; lcm = lcm / gcd (max (lcm, a[i]), min (lcm, a[i])) * a[i]; } for (int i = 0; i < n; i++) cin >> r[i]; long long ans = mod_equ_set (); cout << ans << endl; } return 0; } |
- « 上一篇:hdu3549
- hdu3589:下一篇 »