Here is the contest link.
A1. Non-alternating Deck (easy version)
Approach 1
Instead of thinking about “alternating every two steps”, define four explicit turns:
- Alice1 (current is Alice, next is also Alice)
- Alice2 (current is Alice, but next is Bob)
- Bob1 (current is Bob, next is also Bob)
- Bob2 (current is Bob, but next is Alice)
These repeat in a cycle.
If we encode these states as numbers:
- Alice1 = 0
- Alice2 = 1
- Bob1 = 2
- Bob2 = 3
Then after each step: turn = (turn + 1) % 4
This means if turn < 2, it is Alice's turn else it is Bob's turn.
We simulate distributing cards one by one.
We maintain:
- cards (how many cards will be given in this turn)
- cards_given (how many cards we’ve given in this turn)
- turn (whose turn it is (from 0 to 3))
We start from turn = 1 # Alice2
For each card:
- if it is Alice's turn, we increment Alice's card count by one
- if it is Bob's turn, we increment Bob's card count by one
When we finish a turn (cards_given == cards):
- Increase cards
- Reset cards_given
- Move to next turn with (turn + 1) % 4
Time Complexity: O(n) per test case.
t = int(input())
for _ in range(t):
n = int(input())
alice = 0
bob = 0
cards = 1
cards_given = 0
turn = 1
for _ in range(n):
if turn < 2:
alice += 1
else:
bob += 1
cards_given += 1
if cards_given == cards:
cards += 1
cards_given = 0
turn = (turn + 1) % 4
print(alice, bob)
Approach 2
In Approach 1, we distribute cards one by one.
Can we avoid iterating over every single card?
During a turn, the same player receives all cards cards.
So instead of incrementing one by one, we can directly add the whole amount at once.
At a given turn:
- The number of cards to give is cards.
- These cards all go to the same player in this turn.
- But if the remaining cards are fewer than cards, we give only the remaining cards and stop.
So instead of looping n times, we can:
- Give min(cards, remaining)
- Decrease remaining
- Move to the next turn
How many turns can there be?
Since: 1 + 2 + 3 + ... + k ≈ k² / 2
To reach n cards, k is approximately √(2n)
Time Complexity: O(√n) per test case.
t = int(input())
for _ in range(t):
n = int(input())
alice = 0
bob = 0
cards = 1
turn = 1
remaining = n
while remaining > 0:
give = min(cards, remaining)
if turn < 2:
alice += give
else:
bob += give
remaining -= give
cards += 1
turn = (turn + 1) % 4
print(alice, bob)
A2. Alternating Deck (hard version)
Approach 1
A1 — Approach 1
The deck is:
W B W B W B W B ...
So:
- Position 1 → White
- Position 2 → Black
- Position 3 → White
- Position 4 → Black
- Position 5 → White ...
Notice a pattern? Odd positions are White and Even positions are Black.
We simulate distributing cards one by one.
We maintain:
- cards (how many cards will be given in this turn)
- cards_given (how many cards we’ve given in this turn)
- turn → (whose turn it is (from 0 to 3))
We start from turn = 1 # Alice2
For each card:
- if it is Alice's turn and if the current card position is odd, we increment Alice's White card count by one
- if it is Alice's turn and if the current card position is eve, we increment Alice's Black card Count by one
- if it is Bob's turn and if the current card position is odd, we increment Bob's White card count by one
- if it is Bob's turn and if the current card position is eve, we increment Bob's Black card Count by one
When we finish a turn (cards_given == cards):
- Increase cards
- Reset cards_given
- Move to next turn with (turn + 1) % 4
Time Complexity: O(n) per test case.
t = int(input())
for _ in range(t):
n = int(input())
white_alice = 0
black_alice = 0
white_bob = 0
black_bob = 0
cards = 1
cards_given = 0
turn = 1
for pos in range(1, n + 1):
if turn < 2:
if pos % 2 == 1:
white_alice += 1
else:
black_alice += 1
else:
if pos % 2 == 1:
white_bob += 1
else:
black_bob += 1
cards_given += 1
if cards_given == cards:
cards += 1
cards_given = 0
turn = (turn + 1) % 4
print(white_alice, black_alice, white_bob, black_bob)
Approach 2
A1 – Approach 2 and A2 – Approach 1
In A1 – Approach 2, instead of distributing cards one by one, we distribute an entire turn at once.
In A2 – Approach 1, we determined that:
- Odd positions → White
- Even positions → Black
Can we combine both ideas?
Suppose we are giving k consecutive cards starting from position pos.
We want to count how many of those k positions are even.
There are two cases:
Case 1: pos is even The sequence looks like: Even, Odd, Even, Odd, ...
In this case:
- Number of even positions = (k + 1) // 2
- Number of odd positions = k // 2
Case 2: pos is odd The sequence looks like: Odd, Even, Odd, Even, ...
In this case:
- Number of even positions = k // 2
- Number of odd positions = (k + 1) // 2
Since:
- Even positions → Black
- Odd positions → White
We can compute white and black cards in O(1).
We combine the optimized turn simulation from A1 – Approach 2 with the color counting observation.
We maintain:
- cards → size of current turn
- turn → from 0 to 3 (start from 1)
- remaining → cards left
- pos → starting position of current block (initially 1)
At each turn:
- Compute give = min(cards, remaining)
- Compute how many whites and blacks are inside this block
- Assign these counts to Alice's and Bob's White and Black cards count depending on turn
- Update the variables
Time Complexity: O(√n) per test case.
t = int(input())
for _ in range(t):
n = int(input())
white_alice = 0
black_alice = 0
white_bob = 0
black_bob = 0
cards = 1
turn = 1
remaining = n
pos = 1
while remaining > 0:
give = min(cards, remaining)
if pos % 2 == 1:
white = (give + 1) // 2
black = give // 2
else:
white = give // 2
black = (give + 1) // 2
if turn < 2:
white_alice += white
black_alice += black
else:
white_bob += white
black_bob += black
remaining -= give
pos += give
cards += 1
turn = (turn + 1) % 4
print(white_alice, black_alice, white_bob, black_bob)



