We need to find the maximum possible GCD of N positive integers (≥1) whose product is P. We are given N and P (both up to 1e12). The integers are not necessarily distinct; they are any positive integers.
We want to maximize d = gcd(a_1, a_2, ..., a_N) under the constraint that a_i >= 1 integers and product = P.
Let the common divisor be d. Then we can write a_i = d * b_i, where b_i are positive integers. Then product = d^N * (b_1 * ... * b_N) = P. Thus d^N must divide P. Since b_i are integers, we need d^N | P. Then d can be any integer such that d^N divides P. Also, we can always choose b_i to satisfy the product: after factoring out d^N, we need the remaining factor Q = P / d^N to be expressible as product of N positive integers (b_i). But since b_i can be any positive integers, any positive integer Q can be expressed as product of N positive integers? For N≥1, yes: we can set b_1 = Q, and the rest b_i = 1. However, note that we also need that gcd of the a_i is exactly d? Wait, we are asked to maximize the greatest common divisor. So we are looking for a set of numbers whose product is P and such that their gcd is as large as possible. The gcd is defined as the largest integer that divides all a_i. So if we aim for d to be the GCD, then we need to find a configuration where gcd = d. The condition for existence of numbers with product P and gcd at least d is: there exist a_i such that d divides each a_i, and product = P. Equivalently, we can set a_i = d * b_i with b_i integers >= 1, product d^N * prod b_i = P => d^N divides P, and then we need to assign b_i such that product of b_i = P / d^N. As argued, as long as P/d^N is a positive integer, we can always set b_1 = P/d^N, b_2 = ... = b_N = 1. Then a_1 = d * (P/d^N), a_2 = d, ..., a_N = d. Their gcd? gcd(a_1, a_2,..., a_N) = gcd(d * (P/d^N), d, d, ..., d) = d * gcd(P/d^N, 1, 1, ...) = d * gcd(P/d^N, 1) = d. Since d divides a_1 (obviously), and gcd of d and d*(something) is d, and all others are d, so overall gcd = d. But is it exactly d? Yes. So as long as d^N divides P, we can achieve gcd = d. However, could we achieve gcd larger than d? Possibly if we choose different b_i that cause a common factor greater than d? But we are trying to see if a configuration exists with gcd exactly d? Actually, we want the maximum possible gcd. So we need to find the largest d such that there exists some N-tuple (a_i) with product P and gcd = d. But if we can achieve d as a gcd, then the maximum d is the largest such that there exists some tuple. The necessary condition is d^N | P. The constructive method shows that condition is also sufficient for achieving gcd at least d (specifically, we get gcd exactly d). Wait: careful: In our construction, the gcd might be greater than d if d * gcd(b_i) > d. In our construction, we set b_1 = Q = P/d^N, b_2 =...=b_N = 1. Then gcd(b_1,...,b_N) = gcd(Q,1,1,...,1) = 1. So gcd(a_i) = d * 1 = d. So exactly d. So we can achieve exactly d. So the condition d^N | P is sufficient to achieve gcd exactly d. However, can we achieve a gcd larger than d even if d^N does not divide P? For a candidate D to be the gcd of some N numbers with product P, we need that D divides each a_i, so a_i = D * b_i, product = D^N * prod b_i = P, so D^N must divide P. So D^N | P is a necessary condition. Therefore the maximum possible GCD is the maximum integer D such that D^N divides P.
Thus the answer is the largest integer d such that d^N divides P. Since P up to 1e12 and N up to 1e12, we need to compute that efficiently.
We need to find the maximum d with d^N | P. This is equivalent to d = (product over prime factors) p^{ floor(e_p / N) }? Actually, if P has prime factorization P = ∏ p_i^{e_i}, then the condition d^N | P means for each prime p, the exponent of p in d, say f, must satisfy f * N ≤ e_i, i.e., f ≤ floor(e_i / N). So the largest such d is ∏ p_i^{ floor(e_i / N) }.
Thus answer = product over primes dividing P of p^{⌊e / N⌋}.
However, P up to 1e12, so we can factor P with trial division up to sqrt(P) (which is up to 1e6). That's feasible. But N is huge up to 1e12, but that only matters in exponent division. For each prime exponent e, floor(e/N) will be zero if N > e, else compute floor division. Since N can be huge, for most primes, floor(e/N) will be 0 unless e >= N. But e is at most log_2(P) ≈ 40 (since 2^40 ≈ 1e12). Actually, maximum exponent e for a prime factor in a number up to 1e12 is at most 39 (since 2^39 ≈ 5.5e11, 2^40≈1.1e12 so max exponent 40 for 2? 2^40 = 1,099,511,627,776 > 1e12? Actually 1e12 = 1,000,000,000,000, 2^40 is about 1.0995e12 > 1e12, so exponent 39 is max: 2^39 = 549,755,813,888 < 1e12. For 3, exponent max ~ 24 (3^24 ≈ 2.8e11), etc. So e is relatively small (< 40). N can be huge (up to 1e12). Therefore, for N > e, floor(e/N) = 0. So the answer is 1 unless N <= e for some prime. But wait, there is also possibility that d may be composite not prime power? But we are constructing d from primes. So basically answer > 1 only if there exists a prime factor of P with exponent e >= N. Since e is at most about 40, if N > 40, then for all primes, e < N, so floor(e/N) = 0, answer = 1. But is that always? N can be up to 1e12, but if N=1, answer = P. So we need to compute product of p^{⌊e/N⌋}. Since e is small, we can compute that easily.
But careful: The reasoning that d^N | P is necessary and sufficient for existence of numbers with product P and gcd at least d? Actually, we argued that if d^N divides P, we can construct numbers with gcd exactly d. So the maximum possible gcd is the maximum d satisfying that. So answer is as above.
Thus solution: Factor P into primes and exponents. For each exponent e, compute f = e // N (integer division). Multiply answer by p^f.
But wait: Could there be a scenario where the gcd is larger than the product of prime floor(e/N) because we could have a common divisor that is not a perfect N-th power dividing P? Actually, d^N must divide P. For example, suppose P = 12 = 2^2 * 3^1, N=2. The largest d such that d^2 divides 12? Check d=2: 2^2=4 divides 12 -> yes. d=3: 9 does not divide 12. d=6: 36 >12. So d=2. Our formula: floor(2/2)=1 for 2, floor(1/2)=0 for 3 => product =2 => correct.
Another example: P=36=2^23^2, N=2: d^2 must divide 36. d=6? 6^2=36 divides => d=6 possible. floor(2/2)=1 for each => product=23=6 => correct.
Now consider N=3, P=8=2^3. floor(3/3)=1 => answer=2. Construction: a_i = d * b_i, d=2, P/d^3 = 8/8=1, so set a_1=2, a_2=2, a_3=2? But product = 8? 222=8. gcd=2 => okay.
If N=3, P=12=2^2*3. floor(2/3)=0, floor(1/3)=0 => answer=1. Could we achieve gcd=2? 2^3=8 does not divide 12, so impossible. So answer=1.
Edge cases: N=1: Then condition is d^1 divides P, so d can be any divisor of P. The maximum is P. Our formula: floor(e/1)=e => product of p^e = P => correct.
N huge: answer=1.
But what about if P=0? Not, P>=1.
Thus the problem reduces to prime factorization of P up to 1e12.
We need to be careful: The factorization of numbers up to 1e12 can be done by trial division up to 1e6 (since sqrt(1e12)=1e6). That's fine. However, after dividing by all primes up to 1e6, the remaining factor could be a prime >1e6 (and at most 1e12) but its square would exceed 1e12? Actually if after trial division up to sqrt(original) but as we divide we reduce the number; however, better: after trial division up to sqrt(current) we stop. Since we are doing trial division up to 1e6, after that the remaining factor, if >1, could be a prime (since any composite would have a factor ≤ sqrt(remaining), but we have tested up to 1e6, and remaining ≤1e12; sqrt(remaining) might be >1e6 if remaining > 1e12? Wait, the original P ≤ 1e12, after dividing out all factors up to 1e6, the remaining factor m is either 1 or a prime (since if m is composite, its smallest prime factor ≤ sqrt(m) ≤ sqrt(original) ≤ 1e6, but we already tested all primes up to 1e6, so m would have been divided if it had a prime factor ≤1e6. However, there is a nuance: we only test up to floor(sqrt(current)) as we go? Typically, to factor a number up to 1e12, we loop i from 2 to sqrt(P) but as we divide, the sqrt reduces. But we can simply loop i from 2 to 1e6 (or sqrt(original)) and if i*i > current, break. After the loop, if remaining >1, it's a prime. Because if remaining >1, then it's either prime or product of two primes > 1e6, but the product of two such primes would be > (1e6)^2 = 1e12, which exceeds the original limit? Actually (1e6+1)^2 ≈ 1e12+2e6+1, which is > 1e12? (1,000,001)^2 = 1,000,002,000,001, which is > 1e12 (1,000,000,000,000). So a number ≤ 1e12 cannot be product of two primes both >1e6, because their product would exceed 1e12. Thus the remaining factor, if >1 after trial division up to 1e6, must be prime. Good.
So factorization is doable.
Now compute answer as product of p^{ floor(e/N) }. Since e and floor division are small numbers, we can compute power via fast exponent (or simply loop multiplication, but exponent can be up to e (max 40) so trivial). Answer can be up to P (when N=1) which is up to 1e12, fits in 64-bit.
But careful: N is up to 1e12, but we are using integer division e // N; if N > e, it's 0. So we need to compute e // N safely: e is small, so we can just check if N <= e, then floor division yields 1? Actually if N > e, floor division yields 0. If N <= e, then e // N is at least 1, but e//N could be >1 if N is small. For example, e=40, N=2 => 20. So exponent could be up to 40? Actually e//N can be up to 40 (if N=1). So answer could be up to P, which is ≤ 1e12. So we need to compute product possibly of p^{e/N}. But p^{20} might exceed 1e12? Possibly, but product overall cannot exceed P? Actually the answer is a divisor of P, because it's the product of primes with exponents less than or equal to those in P (since floor(e/N) ≤ e). So answer ≤ P ≤ 1e12. So no overflow beyond 64-bit if using 64-bit unsigned (or 128-bit if needed). But careful: while computing p^{f}, we should ensure not to overflow intermediate product? Since p^{f} ≤ p^e ≤ P, but we multiply multiple such contributions. The product of these contributions is at most P, so no overflow. However, if we compute p^{f} by multiplication in 64-bit, it's fine.
But note: For large primes p (maybe > 1e6) with exponent e=1, f = 1 if N=1 else 0. So only for N=1 we get answer=P which could be as high as 1e12. That's fine.
Thus algorithm:
- Read N, P (as 64-bit integers, maybe use long long in C++).
- Factorize P:
- Let ans = 1.
- For i from 2 to sqrt(P) (while ii <= P), but we need to go up to 1e6. Since P decreases, we can loop while ii <= P. i increments. For each i, if P % i == 0, then count exponent e while P % i ==0, P/=i, e++. Then compute f = e // N. If f > 0, ans *= i^f. After loop, if P > 1, that means remaining prime with exponent e=1. Then f = 1 // N, which is 1 if N==1 else 0. So if N==1, ans *= P (the remaining prime). Actually careful: After division, P becomes the remaining factor, which is either 1 or a prime. So we treat it as prime with exponent 1. So if P > 1, f = 1 // N. If f ==1 (i.e., N==1) then ans *= P.
But there is nuance: What if after dividing out all factors, P becomes a prime that we didn't treat? For example, P=9999999967 (a prime near 1e10). We'll loop i up to sqrt(original) which is about 100000. But after dividing out small primes, the loop condition ii <= P will become false when i gets larger than sqrt(current). Actually we should loop i while ii <= current P. So for a prime >1e6, ii <= P will be false at start because i starts at 2, ii <= P? Actually for a prime near 1e10, sqrt is about 100000. So i will go up to 100000, which is less than 1e6. We'll test all i up to 100000. That's okay because we don't need to test up to 1e6 if sqrt(P) is smaller. But to be safe, we can loop i from 2 to sqrt(P) (original or current). The typical algorithm:
long long temp = P;
for (long long i=2; i*i <= temp; i++) {
if (temp%i==0) { count exponent etc...}
}
After loop, if temp>1 then it's prime.
But note: if P is a product of two primes both > 1e6 but ≤ 1e12? As argued, product would exceed 1e12. Actually, if both primes > 1e6, their product > 1e12, so not possible. So after dividing out all factors up to sqrt(original), remaining must be prime or 1. So safe.
Now, N can be up to 1e12. But exponent e is at most about 40. So compute f = e / N? But we can't divide e by N because they are integers but N may be larger than e. So use if (N <= e) then f = e / N (integer division) else f=0. However, careful: In many languages, integer division of a small number by a large number yields 0. So e // N automatically yields 0 if N > e. That's fine.
But N may be huge and we may need to compute pow(i, f) which is fine.
Thus answer is straightforward.
But wait, is there any nuance about the condition d^N divides P? Let's double-check the necessity: Suppose we have numbers a_i with gcd = g. Then each a_i = g * b_i, and gcd(b_i) = 1? Not necessarily; it's possible that the gcd of the a_i is g, but the b_i might have a common factor >1? If they had a common factor h>1, then gcd(a_i) would be g*h, contradiction. So indeed gcd(b_i)=1. However, the condition d^N | P is necessary for existence of numbers with gcd at least d, not exactly d. For a candidate d to be achievable as the gcd, we need to find b_i such that gcd(b_i)=1 and product d^N * prod b_i = P. But our construction gave gcd(b_i)=1, so works. So indeed d^N | P is sufficient.
But what if we want the maximum possible gcd? That is the largest d with d^N | P.
But consider scenario where d^N divides P, but we can actually achieve a gcd larger than d? That's irrelevant; we are looking for max. So we need to check if there is any possibility that the gcd could be something that is not a divisor of P? No, gcd divides each a_i, so product of all a_i includes N copies of gcd, so gcd^N divides product, hence divides P. So any possible gcd must satisfy that condition. So the max is indeed the largest integer whose N-th power divides P.
Thus answer is as described.
Now check examples:
Example1: N=3, P=24=2^3 * 3^1. floor(3/3)=1 for 2, floor(1/3)=0 for 3 => ans=2 => matches.
Example2: N=5, P=1. P=1 (no prime factors). Then ans=1. Our factorization loop yields no factors, after loop, remaining P=1, ans stays 1 => correct.
Example3: N=1, P=111=3*37. floor(3 exponent? Actually 111=3^1 * 37^1. floor(1/1)=1 each => ans=111 => matches.
Example4: N=4, P=972439611840. Let's factor that? Probably answer 206. Let's test our approach: Compute prime factorization of that number? We'll trust that our answer matches 206.
So solution seems simple.
Now we need to implement factoring P up to 1e12. Since time limit is typical, we can just trial divide by i from 2 to sqrt(P). But sqrt(P) up to 1e6, which is fine, but if we loop i up to 1e6 without optimization, that's about 1e6 iterations, which is fine in most languages. However, we might need to consider that we also do division and mod operations each step; 1e6 is trivial. But we must also handle the case when P is prime and large: we will loop up to sqrt(P) ≈ 1e6, also fine.
But we can optimize by checking only odd numbers after 2, but not necessary.
However, there is a catch: N can be up to 1e12, but we are not directly iterating over N. So overall complexity is O(sqrt(P)) ~ 1e6.
But wait: The constraints say N ≤ 1e12, P ≤ 1e12. But factoring P via trial division up to 1e6 is fine.
But consider the possibility of P being a perfect square of a prime near 1e12? sqrt(P) ~ 1e6, so we still only need to loop up to 1e6. The number of iterations is at most 1e6, which is fine.
However, we might want to accelerate by checking primes only. But generating primes up to 1e6 via sieve could be done but not necessary.
But we need to be careful about integer overflow when computing ii. Use ii <= P, but if i is up to 1e6 and P up to 1e12, ii fits within 64-bit (1e12). But in languages like C++, using int for i might overflow if i is int and ii exceeds 2^31-1? 1e6^2 = 1e12 > 2^31-1, but if i is 32-bit signed, i*i would overflow before comparison. So we should use long long for i, or cast to 64-bit. Better to use long long throughout.
Alternatively, we can loop while (i <= P / i) to avoid overflow.
Thus implement:
long long p = P;
long long ans = 1;
for (long long i = 2; i * i <= p; i++) {
if (p % i == 0) {
int e = 0;
while (p % i == 0) {
p /= i;
e++;
}
long long f = e / N; // integer division (e < N gives 0)
if (f > 0) {
// multiply ans by i^f
for (int j=0; j<f; j++) ans *= i; // f small
}
}
}
if (p > 1) {
// prime with exponent 1
long long f = 1 / N; // 1 if N==1 else 0
if (N == 1) ans *= p;
}
But careful: f = 1 // N is 1 only when N=1. So condition: if (N == 1) ans *= p.
But we need to ensure that ans doesn't exceed 1e12? It won't exceed original P, but if N=1, ans becomes P, which is fine.
But what if p > 1 and N>1? Then we ignore this prime because exponent=1, 1//N = 0.
Now, this seems correct.
But is there any edge case where the answer could be >1 from composite contributions even if no single prime exponent meets N? No, because if floor(e/N) = 0 for all primes, then d has no prime factors, so d=1.
Thus answer computed.
Double-check with some cases:
Case: N=2, P=16 = 2^4. floor(4/2)=2 => ans=2^2=4. Is gcd=4 possible? Let's test: Need two numbers product 16 with gcd=4. Let a=4, b=4 gives product 16, gcd=4. So works.
Case: N=3, P=8 = 2^3. floor(3/3)=1 => ans=2. Could we get gcd=2? Yes: (2,2,2). Could we get gcd=4? 4^3=64 >8, impossible. So correct.
Case: N=2, P=9 = 3^2. floor(2/2)=1 => ans=3. Can we get gcd=3? (3,3) works. Good.
Case: N=4, P=8 = 2^3. floor(3/4)=0 => ans=1. Could we get gcd=2? 2^4=16 >8, so no. So correct.
Now what about a case where P=0? Not in constraints.
Now consider the scenario when N is huge but P is huge too? But P <= 1e12 and N up to 1e12. So answer will be 1 unless N=1.
But there is subtlety: Could the answer be something like d = product of p^{ floor(e / N) } but maybe also d could include a factor that is not a prime factor of P? For d^N to divide P, d must be composed only of primes dividing P, because any prime not dividing P would appear in d^N, but not in P, so can't divide. So only primes of P.
Now is there any scenario where the maximum gcd is not exactly that product? Could there be a larger d that is not a perfect N-th power factor? Wait, condition: d^N divides P. So if d has prime factor p with exponent f, then p^{fN} must be <= exponent in P, i.e., fN ≤ e. So maximum f is floor(e/N). So the maximal d is indeed product of p^{ floor(e/N) }. Because for each prime we can independently choose f up to floor(e/N), and any larger f would violate. So that's the max.
Thus answer is straightforward.
However, note that the answer is independent of the actual distribution of the remaining product among b_i; the construction shows achievability.
Now, let's implement.
But the constraints: N up to 1e12, but we are dividing e/N. In C++, if we do e / N with both being integers, and N is large, e/N = 0. That's fine. But we need to ensure that e is integer and N is long long, and division is integer division.
Potential issues: N might be 0? Constraints say N >= 1.
Now, the factorization loop: The number of iterations could be up to sqrt(P) ~ 1e6, which is fine.
But if we check i * i <= p each iteration, p decreases as we divide out factors. However, for efficiency, we can compute limit as sqrt of original P or just loop while (i * i <= p). Since p shrinks, the loop might terminate earlier. That's fine.
Edge: p could become 1 early, then loop ends.
But we need to consider that i could become large up to 1e6. In C++, a for loop with long long i=2; i*i <= p; i++ will be fine.
However, one might worry about performance if we iterate i up to 1e6 for each test case. This is a single test case problem, so that's fine.
Alternative: Use precomputed primes up to 1e6 via sieve to reduce number of divisions. But 1e6 divisions/mod operations are trivial (approx microseconds). So no need.
Now, need to output ans.
Check example 4: Let's quickly compute to verify: P=972439611840. Let's factor quickly? Probably yields 206.
But we can test: 972439611840 / 206? 206 = 2 * 103. 972439611840 / 2 = 486219805920, /103 = 4720000018.835? Not integer? Actually 486219805920 / 103 = 4720000018.835? Not exact. So maybe 206 is not a divisor? Actually need to check: 206 * something =? Might be that 206 is the answer (the gcd), not necessarily a divisor of P? But answer d must be divisor of P because d^N divides P, so d divides P? Actually if d^N divides P, then d divides P (since d^1 divides d^N divides P). So d is a divisor of P. So 206 should divide P. Let's check: 972439611840 / 206 =? 206 * 4720000018 = 2064,720,000,018 = 972,080,000,708? Not correct. Wait compute properly: 206 * 4,720,000,000 = 972,320,000,000? Actually let's compute: 206 * 4,720,000,000 = 206 * 4.72e9 = 972,320,000,000? Let's do exact: 4,720,000,000 * 200 = 944,000,000,000; 6 = 28,320,000,000; sum = 972,320,000,000. But the number is 972,439,611,840, which is larger by ~119,611,840. So maybe 206 doesn't divide exactly? Possibly my calculation is off. Let's check proper division: 972,439,611,840 ÷ 206. Compute 206 * 4,720,000,000 = as above 972,320,000,000. Subtract: remainder = 119,611,840. 206 * 580,000 = 119,480,000 (since 206580000= 2065810000 = 1194810000=119,480,000). Remainder 131,840. 206640 = 131,840 (since 206640= 2066410 = 1318410=131,840). So total multiplier = 4,720,000,000 + 580,000 + 640 = 4,720,580,640. Then product = 206 * 4,720,580,640 =? Let's compute: 4,720,580,640200 = 944,116,128,000; *6 = 28,323,483,840; sum = 972,439,611,840. Yes! So 206 divides P exactly. So d=206 is a divisor. And indeed answer is 206.
Now check our formula: Factor P: 972439611840. Let's factor quickly: It's known that 972439611840 = 2^? * 3? * 5? etc. 206 = 2 * 103. So likely floor exponents /4 yields 1 for 2 and 1 for 103? That would require exponent for 2 at least 4? Actually N=4. For d=206=2103, need 2^4 divides P? 2^4=16 must divide P, which it does. And 103^4 must divide P? 103^4 = (103^2)^2 = 10609^2 ≈ 112550881? Actually 103^4 = (103^2)^2 = 10609^2 = 112,550,881? Wait compute: 1060910609 =? 1060910000=106,090,000; 10609609=10609600=6,365,400, plus 106099=95,481 => 6,460,881; total = 112,550,881. That's about 1.125e8. P is ~9.7e11, so it's possible. So exponent of 103 in P must be at least 4. So P includes 103^4 as factor. So floor(e_103/4)≥1. Also maybe other primes? Actually answer 206 also has only primes 2 and 103, so likely floor exponents for other primes are zero. So that's consistent.
Thus algorithm is fine.
Now we need to consider potential pitfalls: Overflows when computing i*i. We'll use i <= p / i.
Also careful: When multiplying ans by i^f, we might exceed 64-bit if we use pow function that returns double? But we can just loop multiply ans by i f times, where f ≤ e ≤ ~40. So it's safe.
But note: ans can become 0? No, it starts at 1.
Edge: P could be 1. Then factorization loop does nothing, p ends as 1, not >1, ans=1, correct.
Edge: N=0? Not allowed.
Thus solution is ready.
Now, is there any more nuance? Could we possibly get a gcd larger than the product of floor(e/N) because the b_i could also contribute to a common factor? Wait, recall that the gcd is defined as the largest integer dividing all a_i. If we choose a_i = d * b_i, and the b_i may have a common factor, then the actual gcd would be d * gcd(b_i) > d. So if we aim to maximize the overall gcd, maybe we could choose the b_i not to be coprime, but then we would have effectively a larger d' = d * g, and then we need (d')^N to divide P. So the maximum possible gcd is indeed the largest integer D such that D^N divides P, independent of any decomposition. So our approach finds that D directly.
Now consider scenario: P=8, N=2. D=4? 4^2=16 >8, so D=2? Actually 2^2=4 divides 8? 8/4=2, remainder 0, yes 4 divides 8? Wait 4 divides 8? 8/4=2, integer. So 2^2=4 divides 8, so D=2. But could we achieve gcd=4? 4^2=16 does not divide 8, so no.
Thus consistent.
Thus solution is clear.
But wait: There is another interpretation: The problem says "maximum possible greatest common divisor of a_1, a_2, ..., a_N". That is, over all choices of N positive integers (a_i) such that product = P, we want to maximize their GCD. So yes.
Now, let's examine constraints again: N up to 1e12, P up to 1e12. We have to compute answer within these constraints. Our algorithm runs in O(sqrt(P)) which is up to 1e6. That's fine.
But what if we have multiple test cases? The problem appears to be a single test case.
Now, one nuance: The integers a_i are "not less than 1", i.e., positive integers. So all good.
Now, the solution seems straightforward.
But before finalizing, I'd like to double-check the constructive proof to ensure that for any d such that d^N divides P, we can actually achieve gcd exactly d, not just at least d. Because we need to be able to achieve the maximum d; but if we can achieve d exactly, then the maximum d is the max such d. However, sometimes one might worry that for a particular d, even though d^N divides P, the maximum gcd achievable might be larger than d if we choose different numbers. But we are looking for the maximum possible gcd over all tuples. So if there exists any tuple with gcd = D_max, then D_max is answer. Our constructive method shows that for any d that meets the condition, there exists a tuple with gcd = d. So the set of achievable gcd values includes all d such that d^N|P. So the maximum achievable gcd is the maximum such d. So correct.
Now, we should also verify that the condition is necessary: if a tuple has gcd = g, then g^N divides P. So g must satisfy condition. So the achievable gcds are a subset of these d. Since we have shown all such d are achievable, the set equals the set of all d with d^N|P. So max is max d with d^N|P.
Thus solution is solid.
Now, possible corner: d could be non-integer? No.
Now, we implement.
Implementation details:
- Use long long for variables.
- Factorize P.
Pseudo-code:
function solve():
read N, P
ans = 1
temp = P
for i = 2; i*i <= temp; i++:
if temp % i == 0:
cnt = 0
while temp % i == 0:
temp /= i
cnt++
// compute contribution
exp = cnt // N (integer division)
if exp > 0:
// multiply ans by i^exp
for j in 1..exp:
ans *= i
if temp > 1:
// remaining prime, exponent 1
if N == 1:
ans *= temp
print ans
But note: For the remaining prime case, if N=1, exp = 1//1 = 1, so multiply by temp. If N>1, exp=0.
But careful: When N is large, say N=10^12, the line exp = cnt // N will be 0 because cnt small. So ok.
Now, but cnt // N will be computed as integer division of two integers. In C++, if cnt and N are both integer types, cnt/N will truncate towards zero, which yields 0 if cnt < N. However, if N is long long and cnt is int, then promotion occurs to long long, division yields 0. That's fine.
Potential pitfalls: if we use int for cnt, but cnt can be up to about 40, so int is fine.
Now, but what about the loop variable i? i can go up to sqrt(temp) which is at most 1e6. So i fits in int, but ii might overflow if i is int and >46340? Actually int32 max ~ 2e9, 1e6^2 = 1e12, which exceeds 2e9, so overflow if i is int. So we need to use long long for i to avoid overflow when computing ii. Or we can use i <= temp / i.
Thus in C++:
for (long long i = 2; i * i <= temp; ++i) // but i*i may overflow if i is int. Use long long.
Or while (i <= temp / i) as condition.
Better:
for (long long i = 2; i <= temp / i; ++i)
This avoids overflow and is efficient.
Now, we should also handle the case where N=0? Not needed.
Testing with examples:
-
N=3, P=24: temp=24. i=2: temp%2=0, cnt: 24/2=12 cnt1, 12/2=6 cnt2, 6/2=3 cnt3, 3/2 not integer. So cnt=3. exp=3//3=1, ans=2. Continue i=3: temp now 3? Actually after dividing out 2's, temp=3. i=3: i<= temp/i? 3<=1? No, loop condition: i=3, temp=3, temp/i=1, i<=1 false, so loop ends. Then temp>1 -> temp=3, N=3>1 so no multiplication. ans=2 -> output 2.
-
N=5,P=1: temp=1. loop not entered (i<=temp/i? 2<=0 false). temp>1? false. ans=1. Output 1.
-
N=1,P=111: temp=111. i=2: 2<=111/2=55, 111%2 no. i=3: 3<=111/3=37, 111%3=0 -> cnt: 111/3=37 cnt1, 37%3 not 0. cnt=1. exp=1//1=1, ans=3. Continue i=... after division temp=37. i=4: 4<=37/4=9? 4<=9 true, 37%4 !=0. i=5: 5<=37/5=7 true, 37%5!=0. i=6: 6<=37/6=6? 6<=6 true, 37%6 !=0. i=7: 7<=37/7=5? 7<=5 false, loop ends. temp=37>1, N=1, so ans*=37 => ans=111. Output.
-
N=4, P=972439611840: Let's test with program maybe, but we trust.
Now, what about time? It's fine.
Thus answer.
But wait, there is one more nuance: The GCD is defined as greatest common divisor. If we have numbers like a_1 = d * Q, a_2 = d, ..., a_N = d, with Q possibly not integer? But it's integer. So that's fine.
Now, I'll produce final answer.
But before concluding, consider if there's any trick: Since N can be huge, maybe the answer can be computed as floor(P^(1/N))? Is that equivalent? Not exactly. For example, if P is 24, N=3, floor(24^(1/3)) = 2, matches. But if P=72=2^3*3^2, N=2, floor(sqrt(72))=8, but our formula: exponents: 2: floor(3/2)=1, 3: floor(2/2)=1, product=6. So floor(sqrt(72))=8, but answer is 6. So not simply integer N-th root. Because the N-th root might include primes with insufficient exponent across N copies. So factorization needed.
But we could compute by taking each prime factor, but that's what we do.
Now, the limits: P up to 1e12, but maybe there is a scenario where P is huge prime, and N is huge, answer=1. That's trivial.
Thus solution stands.
Now, let's think about possible optimization: If N > 40 (since max exponent of any prime is about 40), then answer = 1. But we can also early exit: if N > max_exponent_possible (like 60 maybe), but we don't know exponents before factorization. However, we can quickly check: if N > 60 (since 2^60 > 1e12? Actually 2^60 ~ 1e18 > 1e12, so max exponent e <= 39 for 2, less for others). So if N > 39, then for any prime, e < N, so floor(e/N)=0. Thus answer=1. So we can skip factorization entirely if N > 60? But careful: The exponent of 2 could be up to 39 (since 2^39 ≈ 5.5e11 < 1e12, 2^40≈1.1e12 > 1e12, so if P = exactly 2^39? 2^39 = 549,755,813,888 < 1e12, so exponent 39 possible. 2^40 is 1,099,511,627,776 > 1e12, but P max is 1e12, so exponent 40 is impossible because 2^40 > 1e12. However, could other primes have exponent > 39? For prime 3, exponent max ~ 25? 3^25 = 847,288,609,443? Actually 3^25 ≈ 8.47e11 < 1e12, 3^26 ≈ 2.54e12 > 1e12, so exponent max 25. For 5, exponent max 16? 5^16 = 152587890625 < 1e12, 5^17 = 762939453125? Actually 5^17 = 762939453125? Wait 5^17 = 762,939,453,125 < 1e12? 762B < 1e12, 5^18 = 3,814,697,265,625 > 1e12, so exponent 17 possible. For 7, exponent max ~ 14? 7^14 = 678223072849 < 1e12, 7^15 ≈ 4.7e12 > 1e12. So exponents are relatively small, <40. So indeed if N > 40, answer = 1. But N can be up to 1e12, so many cases will have answer 1. So we can do an early check: if N > 40 (or maybe > 60 for safety) then answer = 1. However, we need to be sure that no prime exponent can be >= N. Since max exponent is at most floor(log2(P)) ≈ 39, if N > 39, it's impossible. So we can output 1 immediately without factoring. But careful: Could there be a scenario where N=40 and P contains 2^40? 2^40 is >1e12, so cannot. So safe threshold is 39? But to be safe, we can compute max possible exponent: the exponent of a prime p in P is at most floor(log_p(P)). Since p >=2, max exponent is floor(log_2(P)) <= floor(log_2(1e12)) ≈ 39. So if N >= 40, answer=1. However, is it possible that N=39 and P=2^39? Then answer = 2^{floor(39/39)} = 2^1 = 2. So N=39 yields answer>1. So threshold is 40.
But we need to be careful about p=1? Not needed.
Thus we can add early exit:
if (N > 60) { print 1; return; } // 60 is safe upper bound > log2(1e12).
But careful: The actual max exponent may be slightly higher if P is 1? Actually 1 has no primes, exponent sum 0, but if N>0, answer=1 anyway. So early exit for N > 40 is valid.
But note: The factorization loop will still be efficient even if N large; but early exit could save time, but not necessary.
We should also consider N=0? Not allowed.
Now, there's one more nuance: The numbers a_i are "not less than 1", so they are >=1. Our construction uses b_i=1 for i>1, so a_i = d for those. That's fine.
Thus final answer.
Now, what about the possibility of multiple primes and large N? Already handled.
Now, we need to output as integer.
Implementation in C++:
#include
#include
using namespace std;
typedef long long ll;
int main() {
ll N, P;
cin >> N >> P;
ll ans = 1;
ll temp = P;
for (ll i = 2; i <= temp / i; ++i) {
if (temp % i == 0) {
int cnt = 0;
while (temp % i == 0) {
temp /= i;
++cnt;
}
ll exp = cnt / N; // integer division, cnt < N yields 0
if (exp > 0) {
for (int j = 0; j < exp; ++j) {
ans *= i;
}
}
}
}
if (temp > 1) {
// remaining prime factor with exponent 1
if (N == 1) {
ans *= temp;
}
}
cout << ans << endl;
return 0;
}
But careful: The exponent division cnt/N: if cnt is int and N is long long, cnt/N promotes cnt to long long, but integer division yields 0 if N>cnt. So fine.
Now, we need to ensure that ans doesn't overflow beyond 64-bit? ans <= P <= 1e12, so safe.
Now, test with provided examples.
Let's manually test with example 4 using code mental? Not needed.
Now, is there any scenario where our early loop condition i <= temp / i may skip a prime factor because after dividing out some factors, temp becomes a prime and i may become larger than sqrt(temp) but not equal? Our loop continues while i <= temp / i. When temp becomes a prime, say 37, and i starts at where it left off. In example 3, we had i=3 after processing i=3. Then we continue to i=4,5,6,7. For i=7, condition i <= temp/i: 7 <= 37/7=5? false, so loop stops. Then we handle remaining temp=37 as prime. So correct.
But we must be careful: In our loop, after we divide out factor i, we continue with the same i? Actually after we exit the while loop for that i, we then continue for next i (i++). But temp has been reduced. So condition i <= temp / i is recalculated each iteration. That's fine.
Now, one more nuance: When we have a prime factor i that appears multiple times, we count exponent, divide temp. After that, temp is no longer divisible by i. Then we continue with i+1. That's correct.
Now, note: The loop condition i <= temp / i is used. For i up to maybe 1e6, but if temp becomes 1 early, i <= 1 / i will be false for i>=2, so loop stops.
Now, we should consider the possibility that P=0? Not.
Thus final answer.
But wait, there is another subtlety: The statement says "There are N integers a_1, a_2, ..., a_N not less than 1. The values of a_1, a_2, ..., a_N are not known, but it is known that a_1 × a_2 × ... × a_N = P. Find the maximum possible greatest common divisor of a_1, a_2, ..., a_N." It's exactly as we think.
Now, as a sanity check, let's test a few more random cases mentally.
Case: N=2, P=36. Factorization: 2^2*3^2. floor(2/2)=1, floor(2/2)=1 => ans=6. Can we get gcd=6? (6,6) product 36, yes. Could we get gcd=12? 12^2=144>36, no. So max=6.
Case: N=2, P=48 = 2^4 * 3^1. floor(4/2)=2 for 2 => 2^2=4; floor(1/2)=0 for 3 => ans=4. Can we achieve gcd=4? Example: (12,4) product 48, gcd=4? Actually gcd(12,4)=4. Or (4,12). So yes. Could we do gcd=6? 6^2=36 divides 48? 48/36=1.333, no. So 4 is max.
Case: N=3, P=48 = 2^4*3^1. floor(4/3)=1, floor(1/3)=0 => ans=2. Can we achieve gcd=2? Example: (2,2,12) product 48, gcd=2. Could we do gcd=4? 4^3=64>48, no. So 2.
Case: N=4, P=48. floor(4/4)=1, floor(1/4)=0 => ans=2. Achievable? (2,2,2,6) product 48, gcd=2. Yes.
Seems consistent.
Now, what about P being a perfect N-th power? e.g., N=5, P=32=2^5. floor(5/5)=1 => ans=2. Can we have gcd=2? (2,2,2,2,2) product 32, yes. Could we have gcd=4? 4^5=1024>32, no.
All good.
Now, one more nuance: Could we have the GCD larger than floor(e/N) product because we might have a common factor that is not a prime power but maybe we could combine primes in a way that some a_i don't use all primes? Actually, for the gcd to be d, each a_i must be a multiple of d. So each a_i contains all prime factors of d. So the total exponent of each prime p across all a_i is at least N * exponent_in_d. Since product is P, the exponent of p in P must be at least N * exponent_in_d. So exponent_in_d ≤ floor(e_p / N). So indeed each prime individually limited. So the product of the primes to those exponents is the maximum.
Thus answer is product of p^{ floor(e_p / N) }.
Now, potential issue: Our construction used b_i such that only one b_i is >1, others are 1. That yields gcd exactly d. So fine.
Now, we should ensure that ans is computed correctly for large P where prime factors are large (like 999983, a prime near 1e6). That's okay: we will test i up to sqrt(temp). When temp is that prime, we'll test i up to 1e6. That's fine.
Now, performance: In worst-case P is a product of two primes ~1e6 (like 1,000,003 * 1,000,003? But 1e6^2 = 1e12. So P could be near 1e12 and prime factors near 1e6. Our loop will go up to i = 1e6. That's 1e6 iterations, each performing mod operation. That's fine for a single test case.
But note: if P is a large prime near 1e12, its sqrt is ~1e6, so we loop 1e6 times. That's okay.
However, in some languages like Python, 1e6 iterations is fine, but we need to be careful with time limit if using Python? Usually AtCoder problems (this appears from AtCoder) have time 2 sec and Python can handle 1e6 loops easily. But we need to write efficient code. However, the constraints up to 1e12, but typical solution in Python uses factorization by trial division up to int(sqrt(P)). sqrt(1e12)=1e6, so about 1,000,000 iterations. That's okay in Python if optimized (using while i*i <= P). But in Python, loops of 1 million are fine. However, we also have a while loop for dividing out factors, which overall won't add too many iterations. So Python should be okay.
But we could add early break if N > 40 to avoid factorization. But not necessary.
Now, we need to produce final answer.
Let's also test with large N:
N=100, P=2^30? Actually 2^30=1,073,741,824 which > 1e12? Actually 2^30=1,073,741,824 ≈ 1.07e9, less than 1e12. So P=2^30, N=100. exponent 30, floor(30/100)=0, ans=1. So answer 1.
Ok.
Now, I think we have the solution.
But wait, the problem may have multiple test cases? The input format shows only one line N P. So single case.
Now, we can code.
However, there is a subtle nuance: The numbers a_i are not necessarily distinct, but our construction works.
Thus final answer.
Now, I'll write a clear solution explanation and code.
Edge Cases:
- N=0? Not allowed.
- P=0? Not allowed.
- N=1: answer = P.
- P=1: answer = 1 regardless of N.
- Large N > 40: answer = 1.
But we don't need to treat specially, our code will output 1 anyway after factorization because no prime exponent >= N.
Now, one more check: In our code, for the remaining prime after loop, we check if N==1. But what if N > 1 but exponent 1 is enough? For N=1, it's handled. For N>1, floor(1/N)=0, so we do nothing. That's correct.
Now, let's test with a case where N=2 and P=4: factorization: 2^2. exponent 2, floor(2/2)=1 => ans=2. That's correct.
Now, case where P is a square of a prime: N=2, P=9=3^2 => ans=3.
Now, case where N=3, P=27=3^3 => ans=3^{floor(3/3)}=3. Achievable: (3,3,3) gcd=3.
Now, case where N=3, P= 8=2^3 => ans=2.
Now, case where N=3, P= 64=2^6 => floor(6/3)=2 => ans=4. Achievable: (4,4,4) product 64? 444=64 yes.
All good.
Now, let's also test with composite answer like example 4: 206.
Now, for the sake of completeness, we might consider if there is any alternative interpretation: Could the GCD be computed over the set of numbers but not necessarily that each a_i is multiple of the same d? No, that's the definition.
Now, final answer.
Thus we can produce final solution.
But the problem might be from AtCoder ABC 169? Actually it's AtCoder "Maximum GCD" (ABC 215? Not sure). But anyway.
Now, I'll implement.
Potential issues: In C++, integer division of positive numbers truncates towards zero, which is floor. So fine.
Now, in Python, we need to be careful about loops: for i in range(2, int(sqrt(P))+1) but that might be up to 1e6, which is okay. But we need to update P (temp) and break early if ii > temp. But simpler: while ii <= temp: but we need to increment i properly. Python code:
def solve():
import sys, math
N, P = map(int, sys.stdin.read().split())
ans = 1
temp = P
i = 2
while i * i <= temp:
if temp % i == 0:
cnt = 0
while temp % i == 0:
temp //= i
cnt += 1
exp = cnt // N
if exp:
ans *= i ** exp # i**exp safe as small
i += 1
if temp > 1:
if N == 1:
ans *= temp
print(ans)
But careful: i**exp might be large, but exponent small; ans up to 1e12, safe.
But we need to consider integer overflow? Python arbitrary precision, fine.
Now, edge: If N is huge, cnt//N is 0.
Now, we might optimize with early exit: if N > 60: print(1); return. But not required.
Now, test with P huge prime and N=1: loops up to sqrt(P) which is about 1e6, then after loop, temp>1 and N=1 so ans *= temp. That gives P. So fine.
Now, we should ensure we handle P=1: while condition i*i <= 1: i=2 => 4 <=1 false, loop not entered; temp=1, not >1; ans=1 printed.
Now, for large N > 1, answer 1.
Now, final answer.
Thus problem solved.
I'll now produce final answer with explanation. We are given N positive integers a_1,...,a_N with product P.
Let g = gcd(a_1,...,a_N). Then we can write a_i = g·b_i with integers b_i ≥ 1.
The product becomes g^N·(b_1·…·b_N) = P, so g^N must divide P.
Conversely, if an integer d satisfies d^N | P, we can construct a valid N‑tuple with gcd exactly d:
take b_1 = P / d^N, b_2 = … = b_N = 1, and set a_i = d·b_i.
Then ∏ a_i = d^N·(P/d^N) = P and gcd(a_i) = d.
Therefore the set of achievable gcds is exactly the set of integers d for which d^N divides P, and the maximum possible gcd is the largest such d.
If P has prime factorisation P = ∏ p_i^{e_i}, the condition d^N | P is equivalent to requiring that for each prime p_i, its exponent in d is at most ⌊e_i / N⌋.
The largest d is obtained by taking the maximum allowed exponent for each prime, i.e.
[
d_{\max} = \prod_{p_i | P} p_i^{\lfloor e_i / N \rfloor}.
]
Since P ≤ 10^{12}, its prime factors are at most 10^6, and the exponents are at most 39.
We can factor P by trial division up to √P (≤ 10^6) and compute the answer directly.
Complexity: O(√P) time, which is about 10^6 operations – easily within the limits.
Below are implementations in C++ and Python.
C++
#include <iostream>
using namespace std;
using ll = long long;
int main() {
ll N, P;
cin >> N >> P;
ll ans = 1, temp = P;
for (ll i = 2; i <= temp / i; ++i) {
if (temp % i == 0) {
int cnt = 0;
while (temp % i == 0) {
temp /= i;
++cnt;
}
ll exp = cnt / N; // integer division, 0 if N > cnt
if (exp) {
for (int j = 0; j < exp; ++j)
ans *= i;
}
}
}
// remaining prime factor (if any) has exponent 1
if (temp > 1 && N == 1) {
ans *= temp;
}
cout << ans << endl;
return 0;
}
Python
import sys
def main():
N, P = map(int, sys.stdin.read().split())
ans = 1
temp = P
i = 2
while i * i <= temp:
if temp % i == 0:
cnt = 0
while temp % i == 0:
temp //= i
cnt += 1
exp = cnt // N
if exp:
ans *= i ** exp
i += 1
if temp > 1 and N == 1:
ans *= temp
print(ans)
if __name__ == "__main__":
main()