D. Large Refrigerator
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya wants to buy a new refrigerator. He believes that a refrigerator should be a rectangular parallelepiped with integer edge lengths. Vasya calculated that for daily use he will need a refrigerator with volume of at least V. Moreover, Vasya is a minimalist by nature, so the volume should be no more than V, either — why take up extra space in the apartment? Having made up his mind about the volume of the refrigerator, Vasya faced a new challenge — for a fixed volume of V the refrigerator must have the minimum surface area so that it is easier to clean.

The volume and the surface area of a refrigerator with edges a, b, c are equal to V = abc and S = 2(ab + bc + ca), correspondingly.

Given the volume V, help Vasya find the integer lengths for the refrigerator’s edges a, b, c so that the refrigerator’s volume equals V and its surface area S is minimized.
Input

The first line contains a single integer t (1 ≤ t ≤ 500) — the number of data sets.

The description of t data sets follows. Each set consists of a single integer V (2 ≤ V ≤ 1018), given by its factorization as follows.

Let V = p1a1p2a2… pkak, where pi are different prime numbers and ai are positive integer powers.

Then the first line describing a data set contains a single positive integer k — the number of different prime divisors of V. Next k lines contain prime numbers pi and their powers ai, separated by spaces. All pi are different, all ai > 0.
Output

Print t lines, on the i-th line print the answer to the i-th data set as four space-separated integers: the minimum possible surface area S and the corresponding edge lengths a, b, c. If there are multiple variants of the lengths of edges that give the minimum area, you are allowed to print any of them. You can print the lengths of the fridge’s edges in any order.
Note

In the first data set of the sample the fridge’s volume V = 23 = 8, and the minimum surface area will be produced by the edges of equal length.

In the second data set the volume V = 17, and it can be produced by only one set of integer lengths.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
using namespace std;
const int maxn = 127;
typedef long long LL;
LL t, w[maxn], n, c[maxn];
LL v = 1, s = 3e18, A, B, C;
const long double eps = 1e-6;
void dfs0(int p, LL a);
void dfs1(int p, LL a, LL b);
long long alpha(long long a) {
return 2 * sqrt(a) * sqrt(v) + v / a;
}
int main() {
cin >> t;
while (t--) {
v = 1;
s = 3e18;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> w[i] >> c[i];
for (int j = 0; j < c[i]; j++)
v *= w[i];
}
dfs0(0, 1);
cout << (s << 1) << " " << A << " " << B << " " << C << endl;
}
return 0;
}
void dfs0(int p, LL a) {
if ((long double) a * a * a - eps > v)
return;
if (p == n) {
if (alpha(a) < s)
dfs1(0, a, 1);
return;
}
LL cm = 1;
for (int i = 0; i < c[p]; i++)
cm *= w[p];
for (int i = c[p]; i >= 1; i--) {
c[p] -= i;
dfs0(p + 1, a * cm);
c[p] += i;
cm /= w[p];
}
dfs0(p + 1, a);
}
void dfs1(int p, LL a, LL b) {
if ((long double) b * b * a - eps > v)
return;
if (p == n) {
if (a * b + v / a + v / b < s)
s = a * b + v / a + v / b, A = a, B = b, C = v / a / b;
return;
}
LL cm = 1, sav = c[p];
for (int i = 0; i < c[p]; i++)
cm *= w[p];
for (int i = c[p]; i >= 1; i--) {
c[p] -= i;
dfs1(p + 1, a, b * cm);
c[p] += i;
cm /= w[p];
}
dfs1(p + 1, a, b);
}