-
Notifications
You must be signed in to change notification settings - Fork 7
/
ModularEquation.cpp
269 lines (247 loc) · 5.7 KB
/
ModularEquation.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
D. Moderate Modular Mode
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
YouKn0wWho has two even integers x and y. Help him to find an integer n such that 1≤n≤2⋅1018 and nmodx=ymodn. Here, amodb denotes the remainder of a after division by b. If there are multiple such integers, output any. It can be shown that such an integer always exists under the given constraints.
Input
The first line contains a single integer t (1≤t≤105) — the number of test cases.
The first and only line of each test case contains two integers x and y (2≤x,y≤109, both are even).
Output
For each test case, print a single integer n (1≤n≤2⋅1018) that satisfies the condition mentioned in the statement. If there are multiple such integers, output any. It can be shown that such an integer always exists under the given constraints.
Example
inputCopy
4
4 8
4 2
420 420
69420 42068
outputCopy
4
10
420
9969128
Note
In the first test case, 4mod4=8mod4=0.
In the second test case, 10mod4=2mod10=2.
In the third test case, 420mod420=420mod420=0.
*/
#include<bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <chrono>
#include <complex>
using namespace std;
#define ll long long
#define ld long double
#define ui unsigned int
#define ull unsigned ll
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define hashmap unordered_map
#define hashset unordered_set
#define lb lower_bound
#define ub upper_bound
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define ff first
#define ss second
#define foi(n) for(ll i=0;i<n;i++)
#define foj(n) for(ll j=0;j<n;j++)
#define fok(n) for(ll k=0;k<n;k++)
#define forr(i,a,b) for(ll i=a;i<b;i++)
#define forrr(i,b,a) for(ll i=b;i>=a;i--)
#define forrrr(i,a,b,k) for(ll i=a;i<b;i=i+k)
#define graph vector<vector<ll>>
#define sz(v) v.size()
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef pair< ll, pll> plll;
typedef queue<ll> qll;
typedef vector<plll> vplll;
typedef vector<set<ll>> vsll;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef map<string, int> msi;
typedef map<int, int> mii;
typedef map<ll, ll> mll;
typedef map<ll, vll> mvll;
typedef map<vll, ll> mvlll;
typedef map<char, ll> mcl;
typedef map<pair<ll, ll>, ll> mplll;
typedef unordered_map<char, ll> umcl;
typedef unordered_map< ll, char> umlc;
typedef unordered_map< ll, ld> umld;
typedef map<int, string> mis;
typedef pair<string, int> psi;
typedef pair<string, string> pss;
typedef priority_queue <ll> pq;
typedef priority_queue<pii, vector<pii>, greater<pii> > pqq;
typedef priority_queue<ll, vector<ll>, greater<ll>> prq;
const ll MOD = 1000000007;
const ll modx = 998244353;
ld PI = 3.1415926535897;
const ll N = 200010;
void solve();
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input1.txt", "r", stdin);
freopen("error1.txt", "w", stderr);
freopen("output1.txt", "w", stdout);
#endif
ll t ; cin >> t;
while (t--)
{
solve();
cout << "\n";
}
cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
return 0;
}
ll ceils(ll x, ll y) {
return x / y + ((x % y) != 0);
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
ll powmod(ll x, ll y) {
ll res = 1;
for (ll i = 0; i < y; i++) {
res = res * x % MOD;
}
return res;
}
bool isPrime(ll n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (ll i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
bool COMP(pll l, pll r) {
return l.ss < r.ss;
}
ll kadanesAlgo(vll a)
{
ll n = a.size();
ll currMax = 0;
ll mx = INT_MIN;
foi(n)
{
currMax += a[i];
if (currMax <= a[i])
{
currMax = a[i];
}
mx = max(currMax, mx);
}
return mx;
}
ll ask(ll x, ll y, ll n) {
cout << "?" << " ";
foi(n - 1) {
cout << x << " ";
}
cout << y << endl;
ll s;
cin >> s;
return s;
}
ll binpow(ll a, ll b, ll m) {
a %= m;
ll res = 1;
while (b > 0) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
bool is_sorted(vector<ll>a) {
ll x = 0;
for (ll i = 0; i < a.size() - 1; i++) {
if (a[i] < a[i + 1])
x++;
}
return x == a.size() - 1;
}
bool check(string a, string b) {
ll n = a.size(), m = b.size();
if (a < b) {
return true;
}
if (a > b) {
return false;
}
foi(a.size()) {
if (a[i] > b[i]) {
return false;
}
else if (a[i] < b[i]) {
return true;
}
}
return false;
}
bool isSubSequence(string a, string b, ll m, ll n) {
ll j = 0;
for (ll i = 0; i < n and j < m; i++)
if (a[j] == b[i])
j++;
return (j == m);
}
ll power(ll n, ll x) {
ll ans = 1;
foi(n) {
ans *= x;
}
return ans;
}
void solve() {
ll n;
cin >> n;
vll a(n, 0);
foi(n) {
cin >> a[i];
}
while (a.size()) {
ll idx = a.size() - 1;
while (idx > -1 and a[idx] % (idx + 2) == 0) {
idx--;
}
if (idx < 0) {
cout << "No";
return;
}
a.erase(a.begin() + idx);
}
cout << "Yes";
}