黑妹的游戏三
PS:枚举质因数很容易想到,比赛的时候想到枚举1e8范围内,果断写不出来。其实只需要枚举10000内的质因数就行了,因为对于a来说大于10000的质因数最多一个。枚举质因数后怎么确定能消去多少个呢?最容易想到的方法就是模拟了(我是这样YY的,最坏的情况:10000个 134217728。需要计算 (10000 * 1000 + 27 * 5000)次,10000内有1000多个素数),挺考验实现能力的。
#include<bits/stdc++.h> #include<algorithm> #define ll long long #define P pair<int, int> #define PP pair<int,pair<int, int>> #define pb push_back #define pp pop_back #define lson root << 1 #define INF (int)2e9 + 7 #define rson root << 1 | 1 #define LINF (unsigned long long int)1e18 #define mem(arry, in) memset(arry, in, sizeof(arry)) using namespace std; const int maxn = 20000; const int mod = 1000000007; int T, n; int a[maxn], prime[maxn]; vector<int> p; priority_queue<int> q; void Inite() { for(int i = 2; i <= 10000; i++) { if(prime[i]) continue; p.pb(i); for(int j = 2 * i; j <= 10000; j += i) prime[j] = 1; } } int main() { Inite(); cin >> T; while(T--) { cin >> n; for(int i = 1; i <= n; i++) cin >> a[i]; ll ans = 1; for(auto i : p) { for(int j = 1; j <= n; j++) { int t = 0; while(a[j] % i == 0) { a[j] /= i; t++; } if(t) q.push(t); } while(q.size() > 1) { int x = q.top(); q.pop(); int y = q.top(); q.pop(); --x; --y; if(x > 0) q.push(x); if(y > 0) q.push(y); } if(q.empty()) continue; int x = q.top(); q.pop(); for(int j = 0; j < x; j++) ans = (ans * i) % mod; } sort(a + 1, a + n + 1); for(int i = 1; i < n; i++) if(a[i] == a[i + 1]) a[i] = a[i + 1] = 1; for(int i = 1; i <= n; i++) ans = (ans * a[i]) % mod; cout << ans << endl; } return 0; }