# GCDDUEL - Editorial

**URL:** <https://discuss.codechef.com/t/gcdduel-editorial/126293>\
**Category:** editorial\
**Tags:** easy, editorial, math, start267\
**Created:** [December 2, 2026, 4:30pm UTC](https://discuss.codechef.com/t/gcdduel-editorial/126293 "2026-12-02T16:30:57Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![iceknight1093](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@iceknight1093](https://discuss.codechef.com/u/iceknight1093)\
**Post date:** [December 2, 2026, 4:30pm UTC](https://discuss.codechef.com/t/gcdduel-editorial/126293/1 "2026-12-02T16:30:57Z")

</div>

# PROBLEM LINK:

[Practice](https://www.codechef.com/problems/GCDDUEL)  
[Contest: Division 1](https://www.codechef.com/START267A/problems/GCDDUEL)  
[Contest: Division 2](https://www.codechef.com/START267B/problems/GCDDUEL)  
[Contest: Division 3](https://www.codechef.com/START267C/problems/GCDDUEL)  
[Contest: Division 4](https://www.codechef.com/START267D/problems/GCDDUEL)

_ **Author:** _ [HarraBoy13](https://www.codechef.com/users/HarraBoy13)  
_ **Tester:** _ [raysh07](https://www.codechef.com/users/raysh07)  
_ **Editorialist:** _ [iceknight1093](https://www.codechef.com/users/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)**
>
> ```python
> 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')
> 
> ```
