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.
Forget the divisors for a moment. Since , the condition is just . When does a product equal an LCM? Look prime-by-prime.
For a fixed prime , compare exponents:
while
A sum of nonnegative integers equals their maximum exactly when at most one of them is nonzero. So every prime can appear in the chosen vote of at most one voter. In other words, the selected must be pairwise coprime.
Now count independently per prime. If prime appears in with exponent , then can either be unused, or assigned to exactly one voter , with exponent .
So prime contributes
The final answer is
Just factor all , accumulate total exponents for each prime, multiply .
In the easy version, , so an ideal vote means
Look at one fixed prime . Let be the exponent of in . Then
but
Therefore we need
for every prime .
The values are nonnegative integers. A sum of nonnegative integers equals their maximum iff at most one of them is nonzero. Translation: no prime may appear in two different chosen numbers. Equivalently, the chosen numbers are pairwise coprime. That is the whole damn problem.
Fix a prime . Suppose contains with exponent
In a valid voting array, prime has only these possibilities:
So the number of choices for this prime is
Different primes are independent. Once every prime chooses its owner and exponent, each is determined as the product of prime powers assigned to voter , and automatically . Conversely, every valid array gives exactly one such set of per-prime decisions. Clean bijection, no gremlins hiding under the carpet.
Thus the answer is
where the product is over primes appearing in at least one .
Precompute the smallest prime factor for all .
For each test case:
If all , no prime is touched, and the empty product is , which is correct: everyone must vote .
Let . The sieve costs . Factoring all numbers costs in the simple bound, easily fine for .
Memory usage is .
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
const int MAXA = 500000;
const ll MOD = 1000000007LL;
vector<int> spf(MAXA + 1);
void build_spf() {
vector<int> primes;
for (int i = 2; i <= MAXA; ++i) {
if (spf[i] == 0) {
spf[i] = i;
primes.push_back(i);
}
for (int p : primes) {
ll v = 1LL * p * i;
if (v > MAXA || p > spf[i]) break;
spf[(int)v] = p;
}
}
}
int main() {
setIO();
build_spf();
int T;
cin >> T;
vector<int> cnt(MAXA + 1, 0);
while (T--) {
int n, x;
cin >> n >> x;
vector<int> touched;
touched.reserve(n * 3);
for (int i = 0; i < n; ++i) {
int a;
cin >> a;
while (a > 1) {
int p = spf[a];
int e = 0;
while (a % p == 0) {
a /= p;
++e;
}
if (cnt[p] == 0) touched.push_back(p);
cnt[p] += e;
}
}
ll ans = 1;
for (int p : touched) {
ans = ans * (cnt[p] + 1) % MOD;
cnt[p] = 0;
}
cout << ans << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
const int MAXA = 500000;
const ll MOD = 1000000007LL;
vector<int> spf(MAXA + 1);
void build_spf() {
vector<int> primes;
for (int i = 2; i <= MAXA; ++i) {
if (spf[i] == 0) {
spf[i] = i;
primes.push_back(i);
}
for (int p : primes) {
ll v = 1LL * p * i;
if (v > MAXA || p > spf[i]) break;
spf[(int)v] = p;
}
}
}
int main() {
setIO();
build_spf();
int T;
cin >> T;
vector<int> cnt(MAXA + 1, 0);
while (T--) {
int n, x;
cin >> n >> x;
vector<int> touched;
touched.reserve(n * 3);
for (int i = 0; i < n; ++i) {
int a;
cin >> a;
while (a > 1) {
int p = spf[a];
int e = 0;
while (a % p == 0) {
a /= p;
++e;
}
if (cnt[p] == 0) touched.push_back(p);
cnt[p] += e;
}
}
ll ans = 1;
for (int p : touched) {
ans = ans * (cnt[p] + 1) % MOD;
cnt[p] = 0;
}
cout << ans << '\n';
}
}