Hints: Divisibility Game
Answer to Hint 1: Alice can remove $y$ iff at least one entry of $a$ divides $y$. Bob can remove $y$ iff at least one entry of $a$ does not divide $y$.
So for a fixed $y$, if every entry of $a$ divides $y$, only Alice can ever take that copy of $y$. If no entry of $a$ divides $y$, only Bob can take it. If some divide and some do not, both players might be able to take that $y$ (Alice picks a divisor from $a$, Bob picks a non-divisor).
For a candidate $y$, you need to know how many entries of $a$ (with multiplicity) have value dividing $y$. If that number is $n$, every entry of $a$ divides $y$. If it is $0$, none do. If it is strictly between $0$ and $n$, the situation is “mixed.”
How can you compute, for all $y$ up to $n+m$, the sum of $\mathrm{cnta}[d]$ over every divisor $d$ of $y$—efficiently in $O((n+m)\log(n+m))$ or similar?
Split all copies of numbers in $b$ into three groups using the count from Hint 6 (call it $\mathrm{cov}[y]$ for value $y$):
- Alice-only: $\mathrm{cov}[y] = n$ (every $a$-entry divides $y$).
- Bob-only: $\mathrm{cov}[y] = 0$ (no $a$-entry divides $y$).
- Either: $0 < \mathrm{cov}[y] < n$.
Let $C_A$ be the total number of $b$-elements (with multiplicity) in Alice-only values, $C_B$ for Bob-only, and $C_C$ for Either. How do these three totals relate to who wins?
Answer to Hint 7: Think strategically. Alice will eventually remove every Alice-only $b$-entry—Bob never can. Bob will eventually remove every Bob-only entry—Alice never can. The Either entries are the only ones where both players might have a legal move; play on those can “steal” turns and interact with who runs out of moves first.
Try to argue that optimal play reduces to comparing $C_A$ and $C_B$, with the parity of $C_C$ (even vs odd) flipping a tie case.
Answer to Hint 13: After even $C_C$, it is Alice’s turn again. With $C_A = C_B$ and only exclusive values left, simulate: Alice always shrinks $C_A$, Bob always shrinks $C_B$; with equal starting piles and Alice moving first, Alice empties her pile first and then cannot move on Bob’s remaining exclusive values—Bob wins the tie.
After odd $C_C$, it is Bob’s turn first on the exclusive race; the same symmetric argument swaps roles—Alice wins the tie when $C_A = C_B$.
When $C_A \ne C_B$, the strict inequality $C_A > C_B$ vs $C_A < C_B$ decides the winner regardless of parity (parity only matters when the exclusive counts would otherwise tie).
Implementation checklist: compute $\mathrm{cov}[y]$ for all $y \le n+m$ by divisor-sweep; accumulate $C_A$, $C_B$, $C_C$ using $\mathrm{cntb}[y]$; then:
- if $C_C$ is even: Alice iff $C_A > C_B$;
- if $C_C$ is odd: Alice iff $C_A \ge C_B$.
This is exactly the “exclusive race” plus parity of the Either phase: even $C_C$ leaves Alice first on exclusives (strict $>$ for her when $C_A = C_B$ she loses); odd $C_C$ leaves Bob first ($\ge$ flips the tie toward Alice).