-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrimeTime2.cpp
197 lines (174 loc) · 4.38 KB
/
PrimeTime2.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// PrimeTime.cpp
// Created by David del Val on 10/04/2021
//
//
//https://github.com/Ddelval/Competitive-programming/blob/master/template.cpp
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
template <typename T, typename Q>
inline ostream &operator<<(ostream &o, pair<T, Q> p);
// ====================================================== //
// =================== Container IO =================== //
// ====================================================== //
template <bool B, typename T = void>
using Enable_if = typename std::enable_if<B, T>::type;
struct subs_fail {};
template <typename T>
struct subs_succeeded : std::true_type {};
template <>
struct subs_succeeded<subs_fail> : std::false_type {};
template <typename T>
struct get_iter_res {
private:
template <typename X>
static auto check(X const &x) -> decltype(x.begin());
static subs_fail check(...);
public:
using type = decltype(check(std::declval<T>()));
};
template <typename T>
struct Has_iterator : subs_succeeded<typename get_iter_res<T>::type> {};
template <>
struct Has_iterator<string> : subs_fail {};
template <typename T>
Enable_if<Has_iterator<T>::value, ostream &> operator<<(ostream &o, T val) {
bool first = true;
for (auto it = val.begin(); it != val.end(); ++it) {
if (!first)
o << " ";
first = false;
o << *it;
}
return o;
}
template <typename T = ll>
inline vector<T> readVector(int size) {
vector<T> v;
v.reserve(size);
T a;
for (int i = 0; i < size; ++i) {
cin >> a;
v.push_back(a);
}
return v;
}
// ====================================================== //
// ================== Pairs operations ================== //
// ====================================================== //
inline pii operator+(pii a, pii b) {
return {a.fi + b.fi, a.se + b.se};
}
template <typename T, typename Q>
inline ostream &operator<<(ostream &o, pair<T, Q> p) {
o << "(" << p.fi << "," << p.se << ")";
return o;
}
//gcd(0, n) = n
inline long long _gcd(long long a, long long b) {
while (b)
b %= a ^= b ^= a ^= b;
return a;
}
ll inf = LLONG_MAX / 10;
int iinf = INT_MAX / 10;
#ifdef _LOCAL_
//Local constraints
#else
// Judge constraints
#endif
vi g_primes;
void calculatePrimes() {
int lim = 500;
for (int i = 2; i < lim; ++i) {
bool valid = true;
int l2 = sqrt(i);
for (auto p : g_primes) {
if (p > l2) {
break;
}
if (i % p == 0) {
valid = false;
break;
}
}
if (valid) {
g_primes.pb(i);
}
}
reverse(all(g_primes));
}
vector<pii> decompose(int n) {
vector<pii> res;
for (auto a : g_primes) {
int cnt = 0;
while (a <= n && n % a == 0) {
cnt++;
n /= a;
}
if (cnt) {
res.pb({a, cnt});
}
if (n == 1) {
break;
}
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
calculatePrimes();
//cout << g_primes << endl;
cin >> t;
int z = 1;
while (t--) {
int m;
cin >> m;
vi primes;
map<int, int> pc;
ll sum = 0;
for (int i = 0; i < m; ++i) {
int p, n;
cin >> p >> n;
pc[p] = n;
sum += p * n;
}
ll ans = 0;
//cout << "sum " << sum << endl;
//cout << pc << endl;
for (ll psum = 2; psum <=; --psum) {
auto r = decompose(psum);
//cout << psum << " " << r << endl;
int possible = true;
ll comp = 0;
ll prod = 1;
for (auto it : r) {
if (pc[it.fi] < it.se) {
possible = false;
}
comp += it.fi * it.se;
prod *= pow(it.fi, it.se);
}
if ((sum - comp) == prod && possible) {
ans = prod;
break;
}
}
cout << "Case #" << z << ": " << ans << "\n";
z++;
}
return 0;
}