We need to solve a problem: given two positive integers A and B, compute prime factorization of A^B. The input consists of multiple lines? The description: "Each line contains 2 space-separated integers A and B." That suggests there could be multiple test cases? The sample input shows only one line "10 1". But the problem might be from some online judge where there is only one test case per file? Or maybe multiple lines until EOF. Typically such problems: read until EOF, for each line compute factorization of A^B. The output format: "Consider A^B to have N prime factors. Print N lines." That is per test case? But if multiple test cases, we need to separate outputs? Usually they'd either have one test case or separate outputs with each test case's output printed sequentially. The description doesn't mention multiple test cases explicitly, but "Each line contains 2 ..." hints multiple. Let's examine typical SPOJ problem "FACTCG2" or others. Actually the problem might be "Prime Factorization 2" from some contest. I recall a problem: given A and B, print prime factorization of A^B. Possibly multiple test cases, each on a separate line, and output for each test case should be the factorization lines, and there might be a blank line between test cases? The sample only shows one. Let's think: if multiple test cases, printing all factorizations consecutively could cause confusion as you wouldn't know where one ends and next begins. Usually they'd require a blank line between outputs. But the problem statement says: "Print N lines. For every prime factor, print the base and the exponent separated by a single space." No mention of blank lines. So maybe it's a single test case. But they'd typically say "The first line contains T number of test cases." So I'm leaning that there is only one line input. However, the constraints: A up to 10^12, B up to 1000. So A^B is huge, cannot compute directly. But we can factorize A, then multiply exponents by B. So algorithm: factor A into primes (A can be up to 10^12), which is manageable with trial division up to sqrt(A) which is up to 10^6. 10^6 trial divisions is okay if we do efficient primes. But 10^12 sqrt is 1e6, which is fine. But careful: if A is prime near 10^12, we might need to test divisibility up to 1e6, which is 1 million divisions; that's fine. However, we also need to handle possible large prime factor > 1e6 (since after dividing out all factors <= sqrt, the remaining is prime). So algorithm: read A, B. Factor A by trial division from 2 up to sqrt(A). For each divisor found, count exponent in A. Then multiply exponent by B. Output each prime factor base and exponent (base ascending). Complexity: O(sqrt(A)) per test case, but if multiple test cases, each could be up to 1e6 operations. But if there are many test cases, it could be heavy. However, from constraints maybe just one. If multiple, we might need to precompute primes up to 1e6 (since max sqrt(A) = 1e6). That would speed up. But even if many test cases, we can precompute primes up to 10^6 using sieve. But we need to see if input ends at EOF. So let's design to handle multiple test cases: read all lines from stdin, for each line, compute factorization. For each test case, output its factorization lines. But we need to separate outputs? Possibly they expect outputs one after another, and the judge checks exactly: for each test case, output the factorization lines. Since each test case's output is exactly N lines, and the input lines are processed sequentially, the judge can compare line by line. So if there are multiple test cases, the output would be a concatenation of factorizations. No blank line required. That would be okay because each test case's output is variable number of lines, and the judge knows the input and expects corresponding output. So it's safe to just output factorizations one after another. However, we must ensure we don't print extra blank lines. The sample output has two lines. So we'll implement that way.
But we must also consider that A could be 1. Then A^B = 1. What is prime factorization of 1? Usually it's considered to have no prime factors. So we should output nothing? But maybe constraints: A positive integer, but could be 1? Input says positive integers, so A≥1. For A=1, A^B=1, which has no prime factors. So N=0, print nothing. Should we print a blank line? Not specified. Probably nothing. But we need to check typical problems. Usually they'd define that A>1? But says positive integers, so includes 1. So handle A=1: factorization empty. So output nothing (maybe a newline? but no). So if multiple test cases and one is 1, output nothing and then next test case's output. That's fine.
Now, B up to 1000, so exponent multiplication may be large: exponent in A^B = exponent_in_A * B. This could be up to (max exponent in A) * 1000. Exponent in A can be up to log2(1e12) ~ 40, so product up to 40k, fits in int. But careful: if A has repeated prime factors, like 2^40? Actually 2^40 is about 1e12, so exponent up to 40. So multiplied by B up to 1000 gives exponent up to 40000. That's fine for printing.
Implementation details: We'll precompute primes up to 1,000,000 using sieve of Eratosthenes. sqrt(1e12) = 1,000,000 exactly? 1,000,000^2 = 1e12. So the maximum factor we need to test is floor(sqrt(A)). So we need primes up to 1e6. Using sieve to generate list of primes up to 1e6. Then for each test case: read A and B. Initialize vector<pair<long long, int>> factors. For each prime p in primes while p * p <= A: count exponent c = 0; while A % p == 0: A /= p; c++; if c>0: factors.emplace_back(p, c * B). After loop, if A > 1: then A is prime > sqrt(original A), so factor with exponent 1 * B (since it's prime). Actually careful: original A might have been reduced. So we treat remaining A as prime factor. Then output factors sorted by base. Since we go in increasing prime order, they will be sorted. But we must also handle the case when B=0? Constraint says B≥1, so fine.
Edge case: A=0? Not possible, positive integer.
Now, input reading: We need to read until EOF. Use while (cin >> A >> B) { ... }. That works.
But note: A can be up to 10^12, so use long long (64-bit). In C++ long long is enough (up to 9e18). sqrt(A) may be computed as long long. Use while (p * p <= A). Since p is int (up to 1e6), p*p fits in 64-bit? 1e6^2 = 1e12, fits in 64-bit (max ~9e18). So safe.
We'll use vector primes. Sieve size 1000000+1 maybe 1000000 or 1000000+1. The sqrt of max A (10^12) is 1,000,000. So we need primes up to 1,000,000 inclusive. So sieve limit = 1000000. But note: 1000000 * 1000000 = 10^12 exactly. So if A = 10^12, sqrt = 1000000. So we need to check up to 1000000. For A=10^12, its prime factorization: 10^12 = (25)^12 = 2^12 * 5^12, so primes are 2 and 5, both less than 1e6. So fine. But if A is prime greater than 1e6, e.g., 1,000,003 (which is prime? maybe), sqrt(1,000,003) ≈ 1000.001? Actually sqrt(1,000,003) is about 1000 (since 1000^2=1e6). Wait 1,000,003 > 1e6, its sqrt is ~1000.00015? No: 1000^2 = 1,000,000; 1001^2 = 1,002,001; so sqrt(1,000,003) ~ 1000.0015. So actually if A is around 1e6, sqrt is around 1000. So if A is a prime > 1e6 but less than 1e12, its sqrt is > 1e6? Wait careful: For a number up to 1e12, its square root is up to 1e6. So if A is a prime > 1e6, it could be as high as 1e12, but its sqrt is <= 1e6. So we only need to trial divide up to sqrt(A). If A is prime > 1e6, then after trial dividing up to sqrt(A) (which is <= 1e6) we won't find a factor, and then the remaining A > 1 will be that prime. So our loop using primes up to 1e6 is sufficient: we need to test primes up to sqrt(A). sqrt(A) might be less than 1e6 for many numbers, but we can just test primes up to 1e6, but we stop when pp > A. So we only need primes up to 1e6 because if A is near 1e12, sqrt is 1e6, we may need primes up to 1e6. So generating primes up to 1e6 covers the maximum needed prime divisor test. Good.
But is it necessary to precompute primes? Could just trial divide by integers from 2 up to sqrt(A). But that would be slower if many test cases; but since A up to 1e12, sqrt up to 1e6, doing trial division by all integers would be up to 1e6 divisions per test case. If there are many test cases, that could be too slow. However, the problem might have only one test case, but still precomputing primes is cheap and makes code robust.
Memory: sieve of size 1,000,001 is fine.
Implementation steps:
-
Determine MAX = 1000000 (maybe 1000000). Actually for safety, use 1000000 or 1000000? sqrt(1e12) = 1000000 exactly? 1000000^2 = 10^12. So if A = 10^12, sqrt = 1000000. So we need to check up to 1000000 inclusive. So our sieve should include 1000000. So define const int LIMIT = 1000000.
-
Sieve: vector isPrime(LIMIT+1, true); isPrime[0]=isPrime[1]=false; for i from 2 to sqrt(LIMIT): if isPrime[i] then for j=i*i; j<=LIMIT; j+=i: isPrime[j]=false. Then collect primes into vector.
Alternatively, we could use a bitset for speed.
- Read input: while (cin >> A >> B) { factorize(A, B); }
Factorize function:
long long n = A;
vector<pair<long long, int>> fac;
for (int p : primes) {
if (p * p > n) break;
if (n % p == 0) {
int cnt = 0;
while (n % p == 0) {
n /= p;
cnt++;
}
fac.emplace_back(p, cnt * B);
}
}
if (n > 1) {
fac.emplace_back(n, 1 * B);
}
// Output
for (auto &[prime, exp] : fac) {
cout << prime << " " << exp << "\n";
}
But careful: In the final if (n > 1), we need to ensure n is the prime base. But original A may have been reduced to 1? Actually if n > 1 then it's a prime factor. However, if B=0? Not allowed. So fine.
Now, consider multiple test cases: output for each test case will be printed sequentially. However, if we print nothing for A=1, that's fine. But we should ensure that if there are multiple test cases, we do not print an extra newline between them? Typically judges expect exactly the output lines. If we print a newline after each factor line, that's fine; after the last factor line of a test case, the next line (if any) will be the first factor line of next test case. That's okay. But what about the case where test case outputs nothing? That would cause two test cases' outputs to be adjacent without any separator. For example, test case 1: A=1 -> output nothing; test case 2: A=10 B=1 -> output lines "2 1", "5 1". The concatenated output would be "2 1\n5 1\n". That's fine; there is no extra blank line. But does judge expect something like a blank line between test cases? Usually not. So it's okay.
But what about if there are multiple test cases and some output nothing, the concatenation might be ambiguous? But the judge compares the output exactly. As long as we produce the exact expected output, it's fine. Usually problem statements that have multiple test cases will specify that each test case's output should be separated by a blank line or not. Since not specified, we assume no blank line.
However, there is nuance: The statement says "Print N lines. For every prime factor, print the base and the exponent separated by a single space." It doesn't say "for each test case". If there are multiple test cases, maybe the intention is that each line of input yields its output, but they may want each test case's output to be separated by a newline? Actually, typical SPOJ problem "FACT0" or "FACT1" have multiple test cases and output each factorization, sometimes with a blank line. But the wording "Each line contains 2 space-separated integers A and B." suggests reading until EOF, and for each line produce output lines. I've seen similar problems: e.g., "DIV" etc. So I'll implement as described.
Potential pitfalls: large A: need to use long long. The prime factors base may be large (up to 10^12) if A has a large prime factor > 1e6. So we store base as long long. Exponents up to 40000, store as int.
Now, about sieving: The limit 1000000, but the number of primes up to 1e6 is about 78,498. That's fine.
But we must consider that we are reading multiple test cases and factoring each. The sieve precomputation is done once.
Edge cases: A might be a perfect square of a prime > 1e6? For example, A = (10^6+3)^2 ≈ 1e12+... Actually (10^6+3)^2 = 1e12 + 6e6 +9 = 1,000,006,000,009? That's > 1e12? Let's compute: 1,000,003^2 = 1,000,006,000,009, which is > 1e12? 1e12 = 1,000,000,000,000. So 1,000,003^2 = 1,000,006,000,009 which is > 1e12. So maximum prime whose square <= 1e12 is <= 1,000,000 because 1,000,000^2 = 1e12 exactly. So primes > 1,000,000 have squares > 1e12, so they cannot be factors of A if A <= 1e12? Actually if A <= 1e12, a prime p > 1e6 could still be a factor if p <= A, but if p > sqrt(A) then p cannot appear with exponent 1 unless A itself is that prime. So for A up to 1e12, any prime factor > sqrt(A) must be the remaining A after dividing out smaller factors; but that prime could be larger than 1e6, but its square is > A, so it's fine. So our sieve only needs to include primes up to sqrt(max_A) i.e., 1e6. So we don't need primes above that.
Now, careful: In the trial division loop, we use condition p * p <= n. But n decreases as we divide out factors. So p*p <= n may become false earlier. That's fine.
After loop, if n > 1, that's a prime factor (could be 1? no). So we add it.
Now, exponent multiplication: The exponent for that remaining prime factor is 1 * B.
Now, we should also consider that A may be 0? Not allowed.
Now, output: For each factor, print base and exponent. Ensure we print as integers. We should also sort, but we are already iterating primes in increasing order, and the leftover n is > all previous primes (since we divided out all smaller primes, n is either 1 or a prime > sqrt(original) which is > all tested primes). So it will be in increasing order.
Now, reading input: The input may have trailing spaces or newline. Use while(cin >> A >> B) loop. This will read until EOF or failure.
Now, check sample: Input: "10 1" => factors: 2^1,5^1 => output two lines: "2 1", "5 1". That matches.
Now, also consider a test case: A=12 B=2 => A=12=2^2 * 3^1. So A^B = 12^2 = 144 = 2^4 * 3^2. Output: "2 4", "3 2". Good.
Now, consider A=1 B=5 => output nothing.
Now, is there any need to handle negative numbers? No.
Now, possible performance: For each test case, we iterate through primes up to sqrt(A). In worst-case, sqrt(A)=1e6, number of primes up to 1e6 is ~78k. For each prime we do a modulo operation, but we break early when pp > n. In worst-case, if A is prime near 1e12, we will test all primes up to sqrt(A)=1e6, that's 78k modulo operations, which is fine. If there are many test cases, say 1000, that's 78 million mod ops, still okay in C++ within time (maybe 1 sec? but 78 million is fine). However, we can optimize by breaking early if n becomes 1. But we still need to test primes until pp > n, but if n==1, then p*p > 1 immediately, but we need to break out of loop. So we can add check: if (n == 1) break; inside loop or after finding factor. So that would early exit. For prime A, n never becomes 1 until the very end when we add leftover. So we test all primes anyway.
But we should be careful: For A=1e12, we will test primes up to 1e6, but many will not divide. That's okay.
Now, regarding precomputation: we could also compute primes on the fly using simple trial division by odd numbers, but sieve is better.
Now, code skeleton:
#include
#include
#include
using namespace std;
const int LIMIT = 1000000;
vector primes;
void sieve() {
vector isPrime(LIMIT+1, true);
isPrime[0] = isPrime[1] = false;
for (int i = 2; i * i <= LIMIT; ++i) {
if (isPrime[i]) {
for (int j = i * i; j <= LIMIT; j += i)
isPrime[j] = false;
}
}
for (int i = 2; i <= LIMIT; ++i)
if (isPrime[i])
primes.push_back(i);
}
void factorize(long long A, int B) {
vector<pair<long long, int>> fac;
long long n = A;
for (int p : primes) {
if ((long long)p * p > n) break;
if (n % p == 0) {
int cnt = 0;
while (n % p == 0) {
n /= p;
cnt++;
}
fac.emplace_back(p, cnt * B);
}
}
if (n > 1) {
fac.emplace_back(n, B); // exponent 1*B
}
for (auto &pr : fac) {
cout << pr.first << " " << pr.second << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
sieve();
long long A;
int B;
while (cin >> A >> B) {
factorize(A, B);
}
return 0;
}
But we need to ensure that the output for each test case is printed. The problem may require printing exactly the factorization lines without extra spaces or lines. The above code prints a newline after each factor line. That's fine.
Now, potential pitfalls: Input may contain multiple spaces and newlines, but cin handles.
Now, about handling large exponent multiplication: using int for exponent, but cnt * B could be up to ~401000=40000, fits in int. However, cnt could be larger if A is a power of a small prime? For A up to 10^12, max exponent for prime 2 is floor(log2(1e12)) ≈ 39. So 391000=39000. So int is fine.
Now, we should consider that A may be up to 10^12, but we are storing A in long long. But after division, n may become 1. All good.
Now, check for A=0? Not needed.
Now, test with some examples:
Input:
10 1
12 2
Expected output:
2 1
5 1
2 4
3 2
Our code would output:
2 1
5 1
2 4
3 2
That matches.
Now, but if there is only one test case, it's fine.
Now, about the phrase "Consider A^B to have N prime factors." Usually prime factorization includes only distinct primes. So N is number of distinct prime factors. So we output each distinct prime base and its exponent. That's what we do.
Now, consider if B=0? Not allowed.
Now, think about memory: primes vector of ~78k ints.
Now, but maybe there is a more efficient way: because we have multiple test cases, we could factor A and multiply exponents by B. This is straightforward.
Now, check for A=1: our factorize function: n=1. In loop, condition pp > n: p=2, 22 >1 so break. Then if (n>1) is false, so fac empty, we print nothing. That's fine.
But note: the loop will break immediately because pp > n. Actually we need to be careful: in the for loop, we have condition if ((long long)p * p > n) break; at the start. For n=1, pp > 1 is true for p=2, so break before any iteration. So fine.
Now, but what about A being a large prime > 1e6? n = that prime > 1e6, but pp > n? For p=2, 4 > n? No, n is maybe 1e12, 4 is not > n. So we'll iterate over primes. But eventually when p > sqrt(n), pp > n, break. Since sqrt(n) could be up to 1e6. So we'll test all primes up to sqrt(n). That's fine.
Now, one more nuance: In some languages, reading until EOF may cause issues if there is trailing newline. But our while loop will exit.
Now, we also need to consider the possibility that there are blank lines in input. cin >> skips whitespace, so it's fine.
Now, check potential overflow in p * p: p is int up to 1e6, p*p up to 1e12 fits in 64-bit, but we cast to long long to be safe. In condition if ((long long)p * p > n) we already do that.
Alternatively, we could use p <= sqrt(n) but sqrt is slower.
Now, is it necessary to include prime 2? Yes.
Now, test with A=1000000000000 (10^12) B=1: factorization: 10^12 = 2^12 * 5^12? Wait 10^12 = (25)^12 = 2^12 5^12. So output: 2 12 and 5 12. Our code will find factors: trial division by primes. 2 divides, count exponent 12, n becomes 1 after dividing by 2? Actually after dividing by 2 repeatedly, n becomes something like after dividing 2^12, n becomes 5^12? Let's simulate: A=10^12 = 2^12 * 5^12. Start n=10^12. p=2: n%2==0, while divide by 2: count exponent. After 12 divisions, n becomes 5^12. Then p=3: pp=9, but n=5^12=244140625? Actually 5^12 = 244140625. p=3: 33=9 <= n, check n%3? 244140625 % 3 !=0. Continue. Eventually p=5: will find exponent 12. After that n becomes 1. Then we break when pp > n? Actually after dividing out all factors, n becomes 1. In loop, after n becomes 1, we will continue to next prime? But we have condition if (p * p > n) break; at start of each iteration. For n=1, p=7: 77=49 >1 so break. So we break early, good. So output: 2 12, 5 12. Sorted.
Now, edge: if B=1000, exponents become 12000 and 12000, printed as ints.
Now, final code.
But wait: The problem statement says "Xenny has 2 positive integers A and B. He wants to find the prime factorization of the integer A^B." It may be a single test case. However, many online judges require reading until EOF. I'd better handle both: read first line? If there is exactly one test case, while(cin >> A >> B) will read that and then attempt to read more and fail, that's okay. So using while is safe.
But sometimes there is a T at beginning? Not indicated.
Thus I'll keep while loop.
Now, possible performance: using endl might cause flushing, better use "\n".
Now, verify for A=1 B=1: output nothing. Some judges might expect nothing, maybe a newline? The problem says "Print N lines." So if N=0, print nothing. So okay.
Now, maybe there is a requirement that each test case's output should be separated by a blank line? I should check typical problem "Prime Factorization" from SPOJ (FCTRL?). Actually there is a problem "PRIMEFACT" from SPOJ? Not sure. But I recall a problem "FACT0" or "FACT1": "Integer Factorization (15 digits)" where input multiple lines, output factorization with a newline after each test case (maybe blank line). For "FACT0", the output format: "For each line of input, output a line with the factorization in the format shown. Print a blank line after each test case." However, in that problem, each input line ends with "0". Here we don't have terminator. So maybe not.
I'll check the problem statement from maybe CodeChef: "Prime Factorization of A^B" might be from some contest. The sample input and output suggests only one test case. However, the phrase "Each line contains 2 space-separated integers A and B." hints multiple.
Let's search memory: There is a problem on Codeforces? Not typical.
Given ambiguity, it's safer to not print an extra blank line unless required. But if we need to be safe, maybe we could print a blank line after each test case except the last. But that's tricky without knowing when last is. Some solutions output a blank line after each test case, and judges accept because they ignore extra whitespace? But many judges are strict. I'd rather not add extra blank lines.
However, some judges accept if there is a trailing newline after last line, which we already have because each factor line ends with newline and we don't add extra after last test case. That's fine.
If the problem expects a blank line between test cases, they would have specified. So I'll skip.
Now, implement final solution.
Now, testing with multiple test cases in local environment: provide input:
10 1
12 2
1 5
Expected output:
2 1
5 1
2 4
3 2
(nothing for third test case). So output exactly as above. That seems plausible.
Now, consider A=999999937 (a prime near 1e9) B=10. Should output: 999999937 10. Our code will test primes up to sqrt(999999937) ~31622. So it will iterate primes up to 31622 (about 3401 primes), which is fast.
Now, consider A=999999000001? Actually 10^12=1000000000000. A prime near that is 9999999967? Actually there are primes near 1e12. But sqrt is 1e6, so we test many primes.
Now, ensure that we don't overflow when computing p * p. Use (long long)p * p.
Now, think about potential bug: In some cases, after dividing out factors, n may become 1 but we still have condition break at start of loop. That's fine.
Now, one more thing: The prime factorization of A^B includes primes that appear in A. If A has exponent e, then A^B has exponent e*B. So correct.
Now, check if we need to handle B=0? not required.
Now, do we need to handle negative numbers? Not.
Now, consider A=0? Not allowed.
Now, there's also possibility of A being a perfect square of a prime > 1e6? But as argued, such prime would be > 1e6, but its square > 1e12, so not allowed because A <= 1e12. So maximum prime whose square <= 1e12 is 1,000,000. So no prime > 1,000,000 can appear with exponent >=2. So the only time we encounter a prime > 1e6 is as a leftover after division, and it appears with exponent 1. So our factorization is correct.
Now, potential improvements: Instead of precomputing primes up to 1e6, we could simply test divisibility by 2 then by odd numbers from 3 to sqrt(n). That would be simpler and maybe faster for a single test case? But we can still precompute.
Now, memory: using vector may be slower but okay.
Alternatively, we could use bitset.
I'll go with sieve.
Now, final code:
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000000;
vector primes;
void gen_primes() {
vector is_prime(MAX + 1, true);
is_prime[0] = is_prime[1] = false;
for (int i = 2; i * i <= MAX; ++i) {
if (is_prime[i]) {
for (int j = i * i; j <= MAX; j += i)
is_prime[j] = false;
}
}
for (int i = 2; i <= MAX; ++i) {
if (is_prime[i])
primes.push_back(i);
}
}
void solve(long long A, int B) {
long long n = A;
vector<pair<long long, int>> factors;
for (int p : primes) {
if ((long long)p * p > n) break;
if (n % p == 0) {
int cnt = 0;
while (n % p == 0) {
n /= p;
++cnt;
}
factors.emplace_back(p, cnt * B);
}
}
if (n > 1) {
factors.emplace_back(n, B);
}
for (auto &[base, exp] : factors) {
cout << base << " " << exp << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
gen_primes();
long long A;
int B;
while (cin >> A >> B) {
solve(A, B);
}
return 0;
}
But we used C++17 structured binding: auto &[base, exp]. That is okay if compiler supports C++17. Many judges support C++14/17. But if not, we can use .first, .second.
Now, alternative: we might need to consider that A might be up to 10^12 and B up to 1000, but exponent multiplication might overflow int? int max is 2.14e9, so 40*1000 = 40000 is fine. But could exponent be larger? If A contains a prime factor with exponent > 40? For A up to 10^12, maximum exponent for prime 2 is floor(log2(1e12)) = 39 (since 2^39 ~ 5.5e11, 2^40 ~ 1.1e12, so exponent can be 40 if A=2^40=1,099,511,627,776 which is > 10^12? Actually 2^40 = 1,099,511,627,776 ≈ 1.1e12, which is > 10^12. So max exponent for prime 2 is 39? Let's compute exactly: 2^39 = 549,755,813,888 < 1e12; 2^40 = 1,099,511,627,776 > 1e12. So max exponent is 39. For prime 3: 3^25 = 8.47e11? 3^24 = 2.8e11, 3^25 = 8.5e11, 3^26 = 2.5e12 > 1e12, so exponent up to 25. So max exponent is at most 39. Multiply by B max 1000 => 39,000. So int is safe.
But what about A=10^12 = 2^12 * 5^12, exponent 12. So fine.
Now, if A is 1, no factors.
Now, confirm that we handle A=0? Not needed.
Now, what about A being a product of two large primes near 1e12? Actually each prime must be <= A, but if both are > sqrt(A) then product > A? If A = p * q with p,q > sqrt(A), then p*q > A, so not possible. So at most one prime factor > sqrt(A). So our leftover is at most one.
Now, concerning multiple test cases: Suppose input has many lines. The output will be concatenated factorizations. The judge might compare line by line. If a test case outputs nothing, that may cause missing lines? But if the judge's expected output also has nothing for that test case, it's fine. But we don't know if they include test cases with A=1. Possibly not.
Now, another nuance: The problem might require printing factors in increasing order. We already do.
Now, test with a case where A has repeated prime factor, e.g., A=16 B=3 => 16=2^4, A^B=2^12 => output "2 12". Our code: n=16, p=2: cnt=4, n becomes 1, factors: (2,12). Then loop break because n=1? Actually after p=2, n=1. Next prime p=3: condition if p*p > n? p=3, 9>1 => break. So fine.
Now, test with A=49 B=2 => 49=7^2 => A^B=7^4 => output "7 4". p=2,3,5: none divide. p=7: pp=49 <= n? Initially n=49, so condition pp <= n true, divides, cnt=2, n=1. Then next prime break. So output (7,4). Good.
Now, test with A=2 B=1000 => output "2 1000". p=2: cnt=1, n=1, exponent=1000. Good.
Now, test with A=1000000000000 B=1000 => output "2 12000", "5 12000". Exponents 12*1000=12000.
Now, ensure that the prime 1000000? Actually 1000000 is not prime, but we have primes up to 1e6. So if A contains a factor that is 1e6+? Wait 1e6 is composite (2^6*5^6). So no issue.
Now, final code.
But wait: The limit for A is 10^12. But sqrt(10^12) is exactly 10^6. However, if we set MAX = 1000000, we must include 1000000 in the sieve? 1000000 is composite, but we only need primes up to 1000000. So generating primes up to 1000000 includes primes up to 999983 (largest prime less than 1e6). That's fine. But consider A = 10^12 = (10^6)^2, but 10^6 is not prime, so the prime factors are 2 and 5, which are less. So fine.
Now, consider A = (prime)^2 where prime = 999983? 999983^2 ≈ 9.99966e11 < 1e12. So A could be 999983^2. That's < 1e12, and prime 999983 is less than 1e6. So our sieve includes it. So we can factor it. Good.
Now, consider A = (prime)^2 where prime = 1000003? That prime > 1e6, its square > 1e12, so not allowed.
Thus sieve limit sufficient.
Now, finalize.
But maybe we should add some optimization: after dividing by 2 separately, then only odd primes. But sieve already gives primes.
Now, implement.
One more thing: In the sieve, we set LIMIT = 1000000. But if we allocate vector of size 1000001, that's fine. However, some judges have memory constraints but 1e6 booleans is fine.
Now, I'll produce final answer.
But the problem might be from some contest where they want immediate output per test case and they might have many test cases, like up to 10^5? But constraints not given for number of test cases. However, A up to 10^12 and B up to 1000, but number of test cases not bounded. Could be up to 10^5? In that case factoring each by trial division up to sqrt(A) would be too slow. But given typical problem statements, they often have a single test case or few. The phrase "Each line contains 2 space-separated integers A and B." could indicate that there could be many lines. But it's ambiguous. However, we can attempt to optimize by caching factorization? Not necessary.
We can also use Pollard's Rho for large A, but A is only up to 10^12, so trial division up to 1e6 is acceptable even for many test cases if number of test cases is moderate (like up to 1000). For 10^5 test cases, 10^5 * 78k = 7.8e9 operations, too many. So maybe the problem only has one test case per file. Let's see typical SPOJ: "FACT0" has many test cases and expects output per line. But "FACT0" numbers up to 2^63-1 (19 digits). That's much larger, and they use Pollard Rho. But here numbers only up to 10^12, which is easier. In "FACT0", they have many test cases, but each number up to 10^18? Actually "FACT0" is for 15 digits (10^15). But they use trial division up to sqrt? sqrt(10^15) = 3e7, too large. So they use Pollard. But here sqrt is only 1e6, so many test cases could be okay if number of test cases is not huge.
Given the constraints, I think the intended solution is trial division with precomputed primes. And the input likely has multiple test cases but not extremely many. The sample input shows only one line. So I'll proceed.
But just to be safe, we can implement a fast factorization using only trial division by primes.
Now, code final.
One more nuance: In the output, they say "Print N lines." and then "The prime factors must be sorted by base." So we must sort. Our method yields sorted.
Now, what about when B=0? Not allowed.
Now, confirm that we are reading input correctly: The sample input shows "10 1". Our code would read A=10, B=1, output "2 1\n5 1\n". That's exactly sample output. Good.
Now, consider that the problem might require the output to be like "2^1" but they said separated by a single space, so fine.
Now, finalize solution.
Potential issues: For large A and B, exponent multiplication may overflow int if we use int, but we used int for exponent (cnt*B). cnt is int (small), B up to 1000, product up to 40k fits. But we should store exponent as long long to be safe? However, the problem might have A up to 10^12 but B up to 10^9? Actually B up to 1000. So safe. But if B were larger, could overflow. But constraints say B ≤ 1000, so fine.
Now, final answer.
Now, I will produce final output code.
But note: The instructions: "You are ChatGPT, a large language model. Provide the final answer." So I need to output the code.
I'll write a clear explanation and then the code. But as per typical "final answer", I'll just provide the code.
Given the context, they likely want just the code.
Thus final answer: C++ solution with sieve and factorization.
I'll present code.
Also, I should include comments? Probably not necessary, but can include.
Now, I'll provide final code. ```cpp
#include
#include
#include
using namespace std;
const int MAX = 1000000; // sqrt(10^12) = 1e6
vector primes;
// Sieve of Eratosthenes to generate all primes up to MAX
void generate_primes() {
vector is_prime(MAX + 1, true);
is_prime[0] = is_prime[1] = false;
for (int i = 2; i * i <= MAX; ++i) {
if (is_prime[i]) {
for (int j = i * i; j <= MAX; j += i) {
is_prime[j] = false;
}
}
}
for (int i = 2; i <= MAX; ++i) {
if (is_prime[i]) {
primes.push_back(i);
}
}
}
// Factorize A^B and print the result
void factorize(long long A, int B) {
vector<pair<long long, int>> factors;
long long n = A;
for (int p : primes) {
if ((long long)p * p > n) break; // no larger prime factor possible
if (n % p == 0) {
int cnt = 0;
while (n % p == 0) {
n /= p;
++cnt;
}
factors.emplace_back(p, cnt * B);
}
if (n == 1) break; // early exit
}
// If a prime factor larger than sqrt(original A) remains
if (n > 1) {
factors.emplace_back(n, B);
}
// Output as required (already sorted because primes are increasing)
for (auto &[base, exp] : factors) {
cout << base << " " << exp << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
generate_primes();
long long A;
int B;
while (cin >> A >> B) {
factorize(A, B);
}
return 0;
}