-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP10717.cpp
62 lines (53 loc) · 1.2 KB
/
P10717.cpp
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
#include <iostream>
#include <stdio.h>
using namespace std;
typedef unsigned long long ul;
ul gcd(ul a, ul b) {
ul c;
while(a != 0) {
c = a;
a = b%a;
b = c;
}
return b;
}
ul lcm(ul a, ul b) {
ul g = gcd(a,b);
return a*b/g;
}
int main() {
int n, t;
ul table, coins[50];
while(true) {
// Read case:
cin >> n >> t;
if(n == 0 && t == 0)
return 0;
for(int i = 0; i < n; ++i)
cin >> coins[i];
for(int i = 0; i < t; ++i) {
cin >> table;
ul bestLarger = 1000000000000;
ul bestSmaller = 0;
for(int c0 = 0; c0 < n; ++c0) {
for(int c1 = c0+1; c1 < n; ++c1) {
ul lcm1 = lcm(coins[c1], coins[c0]);
for(int c2 = c1+1; c2 < n; ++c2) {
ul lcm2 = lcm(coins[c2], lcm1);
for(int c3 = c2+1; c3 < n; ++c3) {
ul lcm3 = lcm(coins[c3], lcm2);
//cerr << lcm1 << " " << lcm2 << " " << lcm3 << ", table=" << table << endl;
ul larger = ((table+lcm3-1) / lcm3) * lcm3;
if(larger < bestLarger)
bestLarger = larger;
ul smaller = (table / lcm3) * lcm3;
if(smaller > bestSmaller)
bestSmaller = smaller;
}
}
}
}
cout << bestSmaller << " " << bestLarger << endl;
}
}
}