PROBLEM LINK:
Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4
Author: souradeep1999
Tester and Editorialist: iceknight1093
DIFFICULTY:
1486
PREREQUISITES:
None
PROBLEM:
Given N and K, determine whether it’s possible to partition the integers \{1, 2, \ldots, N\} into exactly K groups such that:
- Each group contains \geq 2 integers.
- The sum of each group is odd.
EXPLANATION:
First off, note that we want to form K groups with \geq 2 integers each.
This means we need at least 2K integers in the first place; so if N \lt 2K the answer is immediately No
.
Now let’s analyze when N \geq 2K.
We have x = \left\lceil \frac{N}{2} \right\rceil odd integers and y = \left\lfloor \frac{N}{2} \right\rfloor even integers to work with.
Note that we’ll have x = y if N is even, and x = y+1 if N is odd.
Also, N \geq 2K means x, y \geq K.
The sum of each group must be odd, which means each group should contain an odd number of odd integers. The even integers don’t affect the parity of the sum, so they can be distributed however we like.
Since x, y \geq K, let’s first put one odd and one even integer into each group.
Now, each group has odd sum and \geq 2 elements; we just need to figure out whether the other elements can be distributed properly while maintaining this property.
We’re left with x-K odd integers, which need to be distributed among the K groups.
To keep the sum of each group odd, note that even group must receive an even number of these x-K integers.
In particular, this means x-K itself must be even; and it’s not hard to see that this condition is also sufficient (if x-K is even, give all x-K odd integers to the first group and we’re done).
So, when N \geq 2K, the answer is Yes
if x-K is even and No
otherwise.
TIME COMPLEXITY
\mathcal{O}(1) per test case.
CODE:
Editorialist's code (Python)
for _ in range(int(input())):
n, k = map(int, input().split())
if n < 2*k: print('No')
else: print('Yes' if ((n+1)//2 - k)%2 == 0 else 'No')