-
Notifications
You must be signed in to change notification settings - Fork 1
/
A_Add_Odd_or_Subtract_Even.cpp
570 lines (473 loc) · 12.9 KB
/
A_Add_Odd_or_Subtract_Even.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define sza(x) ((int)x.size())
#define all(x) x.begin(), x.end()
#define revall(x) x.rbegin(), x.rend()
#define sortall(x) sort(all(x))
const int MAX_N = 2e5 + 5;
const int MAX_NN = 2e5 + 8;
const ll MOD = 1e9 + 7;
const ll INF = 1e18+20;
#define revall(x) x.rbegin(), x.rend()
#define ALL(x) sort(x.begin(), x.end())
#define sortall(x) sort(all(x))
#define reverseall(x) reverse(all(x))
///////////////////////////////////////////////////////////////////////////////////////////////////
#define SCD(t) scanf("%d",&t)
#define SCLD(t) scanf("%ld",&t)
#define SCLLD(t) scanf("%lld",&t)
#define SCC(t) scanf("%c",&t)
#define SCS(t) scanf("%s",t)
#define SCF(t) scanf("%f",&t)
#define SCLF(t) scanf("%lf",&t)
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOR(i, j, k, in) for (ll i=j ; i<k ; i+=in)
#define RFOR(i, j, k, in) for (ll i=j ; i>=k ; i-=in)
#define rall(cont) cont.end(), cont.begin()
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
///////////////////////////////////////////////////////////////////////////////////////////////////
#define MP make_pair
#define PB push_back
#define PF push_front
#define INF (int)1e9
#define EPS 1e-9
// #define MOD 998244353
#define ff first
#define ss second
#define PI 3.1415926535897932384626433832795
#define read(type) readInt<type>()
const double pi=acos(-1.0);
#define ll long long
typedef pair<int,int> PII;
typedef vector<long long> VI;
typedef vector<bool> VB;
typedef vector<char> VC;
typedef vector<char> VC;
typedef vector<vector<long long>> VII;
typedef vector<pair<ll,ll>>VPII;
typedef vector<string> VS;
typedef vector<PII> VIII;
typedef vector<VI> VVI;
// typedef vector<string> VS;
typedef map<ll,ll> MPII;
typedef map<char,int> MPCI;
typedef map<string,int> MPSI;
typedef set<long long> seti;
typedef multiset<int> MSETI;
typedef priority_queue<pair<int,int>> PQPII;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;
#define nl endl
/* clang-format off */
/* TYPES */
#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vvi vector<vector<int>>
#define vll vector<long long>
#define vd vector<double>
#define mii map<long,long>
#define MPC map<char,int>
#define miid map<double,long>
#define si set<int>
#define sc set<char>
#define vi vector<int>
#define revs(x,y,z) for(ll i=x;i<=y;i+=z)
//////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* FUNCTIONS */
#define f1(i,s,e) for(int i=s;i<e;i++)
#define ff1(i,s,e) for(long long int i=s;i>=e;--i)
#define For1(i,n) (long long int i=0;i<n+1;++i)
#define cf(i,s,e) for(int i=s;i<=e;i++)
#define FO(i,s,e) for(long long int i=1;i*i<=y;i++)
#define rf(i,e,s) for(long long int i=e-1;i>=s;i--)
#define pass(a) for(long long int i=n-1;i>=1;i-=2)
#define pb push_back
#define ppb pop_back
#define eb emplace_back
//////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ----------------------</BITWISE>--------------------------
/* a=target variable, b=bit number to act upon 0-n */
#define BIT_SET(a,b) ((a) |= (1ULL<<(b)))
#define BIT_CLEAR(a,b) ((a) &= ~(1ULL<<(b)))
#define BIT_FLIP(a,b) ((a) ^= (1ULL<<(b)))
// '!!' to make sure this returns 0 or 1
#define BIT_CHECK(a,b) (!!((a) & (1ULL<<(b))))
#define BITMASK_SET(x, mask) ((x) |= (mask))
#define BITMASK_CLEAR(x, mask) ((x) &= (~(mask)))
#define BITMASK_FLIP(x, mask) ((x) ^= (mask))
#define BITMASK_CHECK_ALL(x, mask) (!(~(x) & (mask)))
#define BITMASK_CHECK_ANY(x, mask) ((x) & (mask))
// ----------------------</BITWISE END>--------------------------
/****************** Prime Generator **********************/
// const int N=1e7+10; int prime[20000010];
// bool isprime[N]; int nprime;
// template <typename T> void Sieve(T a)
// {nprime = 0;memset(isprime,true,sizeof(isprime));
// isprime[1]=false;for(int i=2;i<N;i++){
// if(isprime[i]){prime[nprime++]=i;for(int j=2;i*j<N;j++)
// isprime[i*j]=false;}}}
// template <typename T> inline T PrimeFactors(T n)
// {ll cnt=0,sum=1;for(int i=0; prime[i]*prime[i]<=n
// and i<nprime;i++){cnt=0;while(n%prime[i]==0)
// {cnt++;n/=prime[i];}sum*=(cnt+1);}
// if(n>1)sum*=2;return sum;}
/**************** Prime Generator End ********************/
/****************** Geometry *****************/
template <typename T> inline T PointDistanceHorVer(T x1,T y1,T x2, T y2)
{return abs(x1-x2)+abs(y1-y2);}
template <typename T> inline T PointDistanceDiagonally(T x1,T y1,T x2, T y2)
{return sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));}
template <typename T> inline T PointDistanceMinimum(T x1,T y1,T x2, T y2)
{ T tmp1=abs(x1-x2); T tmp2=abs(y1-y2); T tmp3=abs(tmp1-tmp2);
T tmp4=min(tmp1, tmp2); return tmp3+tmp4; }
template <typename T> inline T PointDistance3D(T x1,T y1,T z1,T x2,T y2,T z2)
{return sqrt(square(x2-x1)+square(y2-y1)+square(z2-z1));}
template <typename T> inline T Cube(T a){return a*a*a;}
template <typename T> inline T RectengularPrism(T a,T b,T c)
{return a*b*c;}
template <typename T> inline T Pyramid(T base, T height)
{return (1/3)*base*height;}
template <typename T> inline T Ellipsoid(T r1,T r2,T r3)
{return (4/3)*PI*r1*r2*r3;}
template <typename T> inline T IrregualarPrism(T base, T height)
{return base*height;}
template <typename T> inline T Sphere(T radius)
{ return (4/3)*PI*radius*radius*radius;}
template <typename T> inline T CylinderB(T base, T height)
{return base*height;} // base and height
template <typename T> inline T CylinderR(T radius, T height)
{return PI*radius*radius*height;} // radius and height
template <typename T> inline T Cone (T radius,T base, T height)
{return (1/3)*PI*radius*radius*height;}
/****************** Geometry end *****************/
/******* Debugging Class Template *******/
#define debug(args...) (Debugger()) , args
class Debugger
{
public:
Debugger(const std::string& _separator = " - ") :
first(true), separator(_separator){}
template<typename ObjectType> Debugger& operator , (const ObjectType& v)
{
if(!first)
std:cerr << separator;
std::cerr << v;
first = false;
return *this;
}
~Debugger() { std:cerr << endl;}
private:
bool first;
std::string separator;
};
//////////////////////////////////////////////////////////////////////////////////////////////////////
/* PRINTS */
template <class T>
void print_v(vector<T> &v) { cout << "{"; for (auto x : v) cout << x << ","; cout << "\b}"; }
/* UTILS */
ll min(ll a,int b) { if (a<b) return a; return b; }
ll min(int a,ll b) { if (a<b) return a; return b; }
ll max(ll a,int b) { if (a>b) return a; return b; }
ll max(int a,ll b) { if (a>b) return a; return b; }
ll gcd(ll a,ll b) { if (b==0) return a; return gcd(b, a%b); }
ll lcm(ll a,ll b) { return a/gcd(a,b)*b; }
string to_upper(string a) { for (int i=0;i<(int)a.size();++i) if (a[i]>='a' and a[i]<='z') a[i]-='a'-'A'; return a; }
string to_lower(string a) { for (int i=0;i<(int)a.size();++i) if (a[i]>='A' and a[i]<='Z') a[i]+='a'-'A'; return a; }
void yes() { cout<<"YES\n"; }
void no() { cout<<"NO\n"; }
bool isSorted(vi &arr){for(int i=0;i<(int)arr.size()-1;i++) if(arr[i] >= arr[i+1]) return false;return true;}
/* All Required define Pre-Processors and typedef Constants */
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;
bool isprime(ll n)
{
ll x=sqrt(n);
for(int i=2;i<=x;i++)
{if(n%i==0)return true;}
return false;
}
bool cmp(pair<int,int>x,pair<int,int>y){
return x.second<y.second;
}
void two_D_PrifixSum()
{
//prefix sum in 2 D array
int n,m;cin>>n>>m;
int a[n][m],prefix[n][m];
f1(i,0,n)
{
f1(j,0,m)
{
cin>>a[i][j];
}
}
//create a prefix array
f1(i,0,n)
{
f1(j,0,m)
{
prefix[i][j]=a[i][j];
if(i-1>=0)prefix[i][j]+=prefix[i-1][j];
if(j-1>=0)prefix[i][j]+=prefix[i][j-1];
if(i-1>=0 and j-1>=0)
{
prefix[i][j]-=prefix[i-1][j-1];
}
}
}
int q;cin>>q;
while(q--)
{
int i1,i2,j1,j2;cin>>i1>>i2>>j1>>j2;
}
}
//Scanline algorithm
void scanlineAlgoritm()
{
int n;cin>>n;
int a[n];
f1(i,0,n)cin>>a[i];
int prefixSum[n+1];
int q;cin>>q;
while(q--)
{
int l,r,x;cin>>l>>r>>x;
prefixSum[l]+=x;
prefixSum[r+1]-=x;
}
int s=0;
f1(i,0,n)
{
s+=prefixSum[i];
a[i]+=s;
}
f1(i,0,n)
{
cout<<a[i]<<" ";
}
}
int findMin(vector<bool>&vis,VI distance)
{
int start=-1;
int n=vis.size();
f1(i,0,n)
{
if(start==-1 or distance[start]>distance[i])
start=i;
}
return start;
}
int dist(VPII vis,int n)
{
vector<bool>ar(n,false);
VI temp(n,INF);
ar[1]=0;
int val=findMin(ar,temp);
if(val==-1)return 0;
ar[val]=true;
cf(i,0,n)
{
return temp[n];
}
}
bool binarySearch(ll mid,ll k,ll x)
{
ll tot=0;
mid-=1;
if(mid<=k)
tot=(mid*(mid+1))/2;
else
{
tot=(k*(k+1))/2;
mid=2*k-1-mid;
tot+=((k*(k-1))/2-(mid*(mid+1))/2);
}
return tot<x;
}
int power(int b, int e)
{
int ret = 1;
for(int i=0; i<e; ++i) ret *= b;
return ret;
}
vi helper(int n)
{
vi v;
for (int i = 1; i <=sqrt(n); i++)
{
if (n % i == 0) {
if (n / i == i)
{
v.push_back(i);
}else
{
v.push_back(n/i);
}
}
}
return v;
}
ll dmod(ll x)
{
return ((x+1000000007)%1000000007);
}
ll take(ll x,ll y)
{
ll ans=1;
while(y)
{
if(y&1)ans=dmod(ans*x);
y/=2;
// x=dmod(sqrt(x));
x=dmod(x*x);
}
return ans;
}
ll help( long long x )
{
ll sum = 0 ;
while ( x )
{
sum += x % 10 ;
x /= 10 ;
}
return sum ;
}
bool c2(pair<string,pair<int,int>> &x,pair<string,pair<int,int>>&y)
{
if(x.second.first!=y.second.first)
return (x.second.first < y.second.first);
else
return (x.second.second < y.second.second);
}
bool c1(pair<string,pair<int,int>> &x,pair<string,pair<int,int>>&y)
{
if(x.first!=y.first)
return (x.first < y.first);
else
return (x.second.second < y.second.second);
}
int findGCD(vi& a,int n)
{
int tp=a[0];
if(n==a.size()-1)
{
return a[n];
}
// f1(i,0,n)
// {
// tp=gcd(a[i],tp);
// if(tp==true)
// {
// return true;
// }
// }
// return tp;
if(tp==0)
{
return false;
}
int x=a[n];
int y=findGCD(a,n+1);
int totalGCD=__gcd(x,y);
return totalGCD;
}
bool solve()
{
ll a,b;cin>>a>>b;
if(a==0)
{
cout<<0<<nl;
// return;
// continue;
}
// if(a>b)
// {
// if(a&1 and b&1)
// {
// cout<<1<<nl;
// }
// else if(a&1 and b%2==0)
// {
// cout<<2<<nl;
// }
// else if(a%2==0 and b&1)
// {
// cout<<2<<nl;
// }
// else
// {
// cout<<1<<nl;
// }
// }
// if(a<b)
// {
// if(a&1 and b%2==0)
// {
// cout<<1<<nl;
// }
// else if(a%2==0 and b&1)
// {
// cout<<1<<nl;
// }
// else if(a&1 and b&1)
// {
// cout<<2<<nl;
// }
// else
// {
// cout<<2<<nl;
// }
// }
if(a==b)
{
cout<<0<<nl;
return false;
}
int diff=abs(a-b);
diff%=2;
if(a<b and diff==1)
{
cout<<1<<nl;
return false;
}
if(a>b and diff==0)
{
cout<<1<<nl;
return false;
}
cout<<2<<nl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// #ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin); //file input.txt is opened in reading mode i.e "r"
// freopen("output.txt","w",stdout); //file output.txt is opened in writing mode i.e "w"
// #endif
int tc = 1;
cin >> tc;
for (int t = 1; t <= tc; t++) {
// //cout << "Case #" << t << ": ";
if(solve())
{
// cout<<"YES"<<nl;
}
else
{
// cout<<"NO"<<nl;
}
}
// solve();
// }
return 0;
}