All submissions for this problem are available.
Read problems statements in Mandarin Chinese and Russian.
You are given a array A of N positive integers, and you can perform the following operation on the array
1) Pick any two indices i and j in the array
2) Divide A[i] and A[j] by some common factor of A[i] and A[j]
You can perform the above operation as many number of times as you want, and the aim is to minimize the product of the resulting array. Find this minimum product. Since the answer can be a large number, print the product modulo 1000000007 (109+7).
INPUT:
First line contains T, number of testcases. Each testcase contains 2 lines.
First line of each testcase contains single integer N, size of the array
Second line of each testcase contains N space separated integers, the array A
OUTPUT:
For each testcase, output single line indicating the answer to that testcase
CONSTRAINTS:
1<=T<=10
30 points : 1<=N<=2000, 1<=A[i]<=106
70 points : 1<=N<=20000, 1<=A[i]<=108
SAMPLE INPUT:
1
3
2 3 6
SAMPLE OUTPUT:
1
EXPLANATION:
First divide first and third numbers by 2, then the second and third by 3. This makes all numbers equal to 1, hence the product is 1.
1) Pick any two indices i and j in the array
2) Divide A[i] and A[j] by some common factor of A[i] and A[j]
You can perform the above operation as many number of times as you want, and the aim is to minimize the product of the resulting array. Find this minimum product. Since the answer can be a large number, print the product modulo 1000000007 (109+7).
INPUT:
First line contains T, number of testcases. Each testcase contains 2 lines.
First line of each testcase contains single integer N, size of the array
Second line of each testcase contains N space separated integers, the array A
OUTPUT:
For each testcase, output single line indicating the answer to that testcase
CONSTRAINTS:
1<=T<=10
30 points : 1<=N<=2000, 1<=A[i]<=106
70 points : 1<=N<=20000, 1<=A[i]<=108
SAMPLE INPUT:
1
3
2 3 6
SAMPLE OUTPUT:
1
EXPLANATION:
First divide first and third numbers by 2, then the second and third by 3. This makes all numbers equal to 1, hence the product is 1.
CODE IS AS FOLLOWS
//Coder: Balajiganapathi #define TRACE #define DEBUG #include <algorithm> #include <bitset> #include <deque> #include <cassert> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int,int> pi; typedef vector<string> vs; // Basic macros #define st first #define se second #define all(x) (x).begin(), (x).end() #define ini(a, v) memset(a, v, sizeof(a)) #define re(i,s,n) for(int i=s;i<(n);++i) #define rep(i,s,n) for(int i=s;i<=(n);++i) #define fr(i,n) re(i,0,n) #define repv(i,f,t) for(int i = f; i >= t; --i) #define rev(i,f,t) repv(i,f - 1,t) #define frv(i,n) rev(i,n,0) #define pu push_back #define mp make_pair #define sz(x) (int)(x.size()) const int oo = 2000000009; const double eps = 1e-9; #ifdef TRACE #define trace1(x) cerr << #x << ": " << x << endl; #define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl; #define trace3(x, y, z) cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl; #define trace4(a, b, c, d) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl; #define trace5(a, b, c, d, e) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl; #define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl; #else #define trace1(x) #define trace2(x, y) #define trace3(x, y, z) #define trace4(a, b, c, d) #define trace5(a, b, c, d, e) #define trace6(a, b, c, d, e, f) #endif const int mx = 20004, mod = 1000000007; int a[mx], n; set<int> primes; map<int, vector<pi> > pr; // Key is prime and the value is a vector of number of times the prime divides a[i] //Decompose each number into its prime factors and populate the above map void getPrimes() { primes.clear(); pr.clear(); fr(i, n) { int m = a[i]; for(int p = 2; p <= m / p; ++p) { int cnt = 0; while(m % p == 0) { ++cnt; m /= p; } if(cnt > 0) { primes.insert(p); pr[p].pu(mp(cnt, i)); //trace3(i, p, cnt); } } if(m > 1) { primes.insert(m); pr[m].pu(mp(1, i)); } } } int main() { int t; cin >> t; assert(1 <= t && t <= 10); while(t--) { cin >> n; assert(1 <= n && n <= 20000); fr(i, n) cin >> a[i]; fr(i, n) assert(1 <= a[i] && a[i] <= 100000000); getPrimes(); vi v(all(primes)); //Solve for each prime separately using a greedy strategy: Take the two elements with the largest no. of the prime currently and divide them by the prime. Decrement the count and update them. fr(j, sz(v)) { int p = v[j]; //trace1(p); priority_queue<pi> q; fr(j, sz(pr[p])) { q.push(pr[p][j]); //trace3(j, pr[p][j].st, pr[p][j].se); } while(sz(q) > 1) { //Atleast two elements needed pi p1 = q.top(); q.pop(); pi p2 = q.top(); q.pop(); a[p1.se] /= p; a[p2.se] /= p; if(p1.st > 1) q.push(mp(p1.st - 1, p1.se)); if(p2.st > 1) q.push(mp(p2.st - 1, p2.se)); } } ll ans = 1; fr(i, n) { ans *= a[i]; ans %= mod; } cout << ans << endl; } return 0; }
No comments:
Post a Comment