PROBLEM LINK:
Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4
Author: notsoloud
Tester: mexomerf
Editorialist: iceknight1093
DIFFICULTY:
cakewalk
PREREQUISITES:
None
PROBLEM:
There’s a deck with N cards numbered 1 to N.
Chef chooses card X.
How many cards Y from the remaining deck are such that (X+Y) is even?
EXPLANATION:
If X is even, Y should also be even; and if X is odd, Y should also be odd.
This is the only way (X+Y) can be even.
If X is even, the count of even numbers between 1 and N is \text{floor}\left(\frac{N}{2}\right).
Any of them can be chosen, except X itself (which is also even).
So, in this case, the answer is \text{floor}\left(\frac{N}{2}\right) - 1.
If X is odd, we instead count the odd numbers: there are \text{ceil}\left(\frac{N}{2}\right) of them between 1 and N.
Again, any of them is valid except X, so the answer is \text{ceil}\left(\frac{N}{2}\right) - 1.
TIME COMPLEXITY:
\mathcal{O}(1) per testcase.
CODE:
Editorialist's code (PyPy3)
for _ in range(int(input())):
n, x = map(int, input().split())
if x%2 == 0: print(n//2 - 1)
else: print((n+1)//2 - 1)