Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Sorted arrays are a special case: the game ends before anyone moves, and Bob wins. So only think about arrays that already have a decrease.
A number with two different prime factors is dangerous. Alice can split off its smallest prime factor as , and because was not a prime power, the left part still contains some prime .
Prime powers behave nicely: splitting always creates two smaller powers of the same prime . So the important label of is just its base prime .
If every number is a prime power and these base primes are non-decreasing from left to right, Alice cannot create a real permanent decrease. Eventually everything becomes primes in non-decreasing order, which is Bob's happy ending.
So after checking whether the original array is already non-decreasing, reduce every to its prime-power base: , , and “mixed primes” bad. Bob wins exactly when all bases exist and are non-decreasing.
First, do not solve this by move parity. That is bait. Bob wins as soon as the array becomes non-decreasing, even if Alice was the one who made it happen. Alice only wins if the game gets stuck while the array is still decreasing somewhere.
The real structure is about prime factors.
Prime-Power Base
Define the base of a number like this:
Examples:
Now the answer is surprisingly clean:
If the array is already non-decreasing, Bob wins immediately.
Otherwise, Bob wins iff every number has a base and the base sequence is non-decreasing.
So for a non-sorted array, this is Bob-winning:
because the bases are
But this is Alice-winning:
because the bases are
and .
Why Prime Powers Are Safe For Bob
Suppose every number is a prime power and the bases are non-decreasing.
Splitting a prime power preserves its base:
Both new numbers are still powers of . So no matter how Alice or Bob splits things, the base sequence stays non-decreasing.
The game is finite, because each move breaks one composite into smaller factors. Eventually, if the game somehow has not ended earlier, every number is either or prime. At that point, the actual array equals its base sequence, which is non-decreasing.
That means Alice can never force an unsorted terminal position. Bob wins sooner or later. Not flashy, but brutally effective.
Why A Bad Number Lets Alice Win
Now suppose the array is not initially non-decreasing, and some number has at least two distinct prime factors.
Let be the smallest prime factor of .
Alice splits like this:
This is legal. Since is not a pure power of , the number still contains some prime factor greater than . In particular,
So Alice immediately creates a local decrease:
The right element is prime, so it can never be changed. Everything produced from will always remain before that .
For the array to ever become non-decreasing, the whole block coming from would need to fit before , meaning all its final pieces would have to be at most . But that is impossible, because contains a prime factor greater than .
So Bob can never make the array sorted. Eventually the game ends unsorted, and Alice wins. Clean kill.
Why A Decrease In Bases Lets Alice Win
Now suppose every number is a prime power, but the bases are not non-decreasing.
Then there are adjacent numbers with bases
So locally we have something like
If is already just (or it is ), then the bad boundary is already fatal: everything from the left block is at least , and .
If , Alice splits the right number as
Now the array contains
and since , we have an unavoidable decrease before the prime .
Bob cannot fix this. Splitting only creates more powers of , all still greater than . The prime sits there like a tiny wall Bob cannot move.
So again, sortedness is impossible forever, and Alice wins.
Algorithm
For each test case:
Bob.Alice.Alice.Bob.We precompute smallest prime factors up to , so checking whether a number is a prime power is fast.
Complexity
The sieve costs
for .
Each test case is linear in , so the total work is
Memory is .
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
const int MAXA = 1'000'000;
vector<int> spf(MAXA + 1);
for (int i = 2; i <= MAXA; i++) {
if (spf[i] == 0) {
for (int j = i; j <= MAXA; j += i) {
if (spf[j] == 0) spf[j] = i;
}
}
}
auto base = [&](int x) -> int {
if (x == 1) return 1;
int p = spf[x];
while (x % p == 0) x /= p;
return x == 1 ? p : -1;
};
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<int> a(n);
for (int &x : a) cin >> x;
bool sorted = true;
for (int i = 0; i + 1 < n; i++) {
if (a[i] > a[i + 1]) sorted = false;
}
if (sorted) {
cout << "Bob\n";
continue;
}
bool good = true;
int last = 0;
for (int x : a) {
int b = base(x);
if (b == -1 || b < last) {
good = false;
break;
}
last = b;
}
cout << (good ? "Bob" : "Alice") << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
const int MAXA = 1'000'000;
vector<int> spf(MAXA + 1);
for (int i = 2; i <= MAXA; i++) {
if (spf[i] == 0) {
for (int j = i; j <= MAXA; j += i) {
if (spf[j] == 0) spf[j] = i;
}
}
}
auto base = [&](int x) -> int {
if (x == 1) return 1;
int p = spf[x];
while (x % p == 0) x /= p;
return x == 1 ? p : -1;
};
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<int> a(n);
for (int &x : a) cin >> x;
bool sorted = true;
for (int i = 0; i + 1 < n; i++) {
if (a[i] > a[i + 1]) sorted = false;
}
if (sorted) {
cout << "Bob\n";
continue;
}
bool good = true;
int last = 0;
for (int x : a) {
int b = base(x);
if (b == -1 || b < last) {
good = false;
break;
}
last = b;
}
cout << (good ? "Bob" : "Alice") << '\n';
}
}