#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;
const int INF = 1e9;
vector spf(1e6 + 1, -1);
void solve() {
int n;
cin >> n;
map<int, int> mp;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
while (spf[x] != -1) {
mp[spf[x]]++;
x /=spf[x];
}
}
for (auto [k, v]: mp) {
if (v%n != 0) {
cout << "NO" << endl;
return;
}
}
cout << "YES" << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
cin >> t;
for (int i = 2; i*i <= 1e6; ++i) {
if (spf[i] == -1) {
for (int j = i; j <= 1e6; j+=i) {
// cout << j << endl;
if (spf[j] == -1) {
spf[j] = i;
}
}
}
}
while (t--)
solve();
return 0;
}
generate test cases that return wrong answer
Assistant
The issue in your code lies in the Sieve of Eratosthenes implementation for smallest prime factor (spf) and how you're handling prime numbers during factorization.
✅ Problem Summary:
You're trying to determine whether all elements in an array can be made into perfect n-th powers by multiplying or dividing. To do this, you're counting the total number of times each prime factor appears across all numbers, and checking if each count is divisible by n.
However, your smallest prime factor (spf) sieve is incorrect. You're initializing spf[i] = -1, but then using it like:
cpp
while (spf[x] != -1)
This leads to incorrect factorization for prime numbers, because their spf will remain -1. So, a prime number like 7 would not get factored at all, and its prime factor wouldn't be counted — causing wrong answers.
🔍 Root Cause
In this block:
cpp
while (spf[x] != -1) {
mp[spf[x]]++;
x /= spf[x];
}
If x is a prime number, spf[x] remains -1, so the loop never runs. That means primes are completely ignored, which is wrong.
✅ Fix: Correct Sieve and Factorization
Here's how to fix the sieve and factorization logic:
✅ Updated spf sieve (correct initialization and marking):