PROBLEM LINK:
Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4
Author: HarraBoy13
Tester: raysh07
Editorialist: iceknight1093
DIFFICULTY:
Easy
PREREQUISITES:
GCD and LCM
PROBLEM:
Alice and Bob play a game on an array A, with Alice moving first.
In a move, the player chooses 1 \le k \le K and replaces every A_i by \gcd(A_i, k).
The move is legal only if exactly one element changes, and that element becomes 1.
The player who can’t move loses; find the winner.
EXPLANATION:
Elements equal to 1 never change, so they’re irrelevant; let’s ignore them and assume every element is \gt 1.
First, let’s understand when a valid move on index i (i.e. one that turns A_i into 1 and leaves everything else unchanged) is possible.
- Every other element must remain unchanged, i.e. \gcd(A_j, k) = A_j, which means A_j divides k.
So, k must be a multiple of \text{LCM}(A_1, A_2, \ldots, A_{i-1}, A_{i+1}, \ldots, A_N). - A_j must become 1, i.e. \gcd(A_i, k) = 1.
Clearly, if such a k does exist, choosing k = \text{LCM}(A_1, A_2, \ldots, A_{i-1}, A_{i+1}, \ldots, A_N) is optimal; since taking any multiples is just going to make both parts harder.
So, a valid move on index i is possible if and only if \text{LCM}(A_1, A_2, \ldots, A_{i-1}, A_{i+1}, \ldots, A_N) \le K and \gcd(A_i, \text{LCM}(A_1, A_2, \ldots, A_{i-1}, A_{i+1}, \ldots, A_N)) = 1.
The second condition (about the gcd) tells us that A_i shares no prime factor with any other element.
Let’s call such elements free, and the others stuck.
The main observation is that a stuck element can never have a valid move made on it.
This is because, if A_i is stuck,
- There exists some j and a prime p such that p divides both A_i and A_j.
- A_i cannot be made 1 before A_j, because doing so would require p to be present in the LCM (to preserve A_j) and also not be present in the LCM (to send A_i to 1.)
This is of course a contradiction. - A_j cannot be made 1 before A_i, for the same reason: p would have to both divide and not divide the LCM.
This means neither A_i nor A_j can ever change!
So, Alice and Bob can only ever removes free elements, one at a time.
What about the first condition, the LCM not exceeding K?
Suppose some move is possible, on the free element A_i.
After it’s made, every remaining element will surely be a divisor of the chosen k; which in turn means that their LCM will also be a divisor of this k (and hence, \le K).
This means that as long as the first move can be made, all the free elements can be removed for sure.
This tells us that the game is actually quite simple:
- If no move is possible at the start, Alice loses immediately.
- Otherwise, all free elements will be removed - and only the free elements will be removed.
So, the total number of moves equals the number of free elements; and Alice wins if and only if this count is odd.
All that remains is to implement this, i.e. find a valid first move (if it exists.)
Let B denote the LCM of the stuck elements, and P denote the product of the free elements (the free elements are pairwise coprime, so their LCM is just their product).
Then for a free element A_i, we have \text{LCM}(A_1, A_2, \ldots, A_{i-1}, A_{i+1}, \ldots, A_N) = \frac{B\cdot P}{A_i}.
We want this to be \le K, and because it doesn’t matter which element the first move is made on, clearly our best hope is to choose the largest one!
Finding all the free elements is trivial because the constraints allow for quadratic time.
Then, we need to check if the move on the largest free element is valid - i.e. the LCM of the remaining elements is \le K.
This can be done by just trying to compute the LCM of the remaining elements directly; and stopping immediately if said LCM ever exceeds K (since it’ll never fall below K after that) to avoid overflowing beyond what a 64-bit integer can hold.
It’s also possible to improve this to \mathcal{O}(N\log M) time by building prefix/suffix LCMs and capping them to K+1 to avoid overflow issues; since we can really skip the coprime check for any element whose related LCM exceeds K and if the LCM is \le K the LCM itself can be used for the coprime check; but this optimization wasn’t needed to get AC.
TIME COMPLEXITY:
\mathcal{O}(N^2\log M) per testcase, where M = 10^6.
CODE:
Editorialist's code (PyPy3)
from math import gcd
for _ in range(int(input())):
n, k = map(int, input().split())
a = []
for x in map(int, input().split()):
if x > 1: a.append(x)
n = len(a)
free = []
for i in range(n):
good = True
for j in range(n):
if i != j and gcd(a[i], a[j]) > 1: good = False
if good: free.append(i)
if len(free) == 0:
print('Bob')
continue
best = free[0]
for i in free:
if a[i] > a[best]: best = i
L = 1
for i in range(n):
if i == best: continue
L = L * a[i] // gcd(L, a[i])
if L > k: break
if L > k: print('Bob')
elif len(free)%2 == 1: print('Alice')
else: print('Bob')