We need to compute A^B mod MOD where MOD = 1e9+7, a prime. Input A and B are huge numbers (up to 100k digits each). T up to 10.
We need to compute exponentiation with exponent B that can be huge. Since MOD is prime, we can use Fermat's little theorem: For any integer a not divisible by MOD, a^(MOD-1) ≡ 1 mod MOD. So we can reduce exponent B modulo (MOD-1). But we need to consider if A mod MOD == 0? If A is multiple of MOD, then A^B mod MOD is 0 unless B=0? Actually B>=1. If A mod MOD == 0 then result is 0. However careful: If A is divisible by MOD, then A mod MOD = 0, so 0^B = 0 for B>0. That's fine.
But if A mod MOD != 0, we can reduce exponent modulo (MOD-1). However there is nuance: The reduction works when exponent is used for a base that is coprime with modulus. But if base is not coprime (i.e., divisible by modulus) we already handled separately. Actually if base mod MOD = 0, then result is 0 for any positive exponent. So we need to compute:
Let a = A mod MOD.
Let b = B mod (MOD-1) but we also need to consider if B >= MOD-1? Actually we compute exponent modulo (MOD-1) because a^(phi(MOD)) = 1 mod MOD when gcd(a,MOD)=1. Since MOD is prime, phi(MOD)=MOD-1. So if a != 0, then a^(B) ≡ a^(b) mod MOD, where b = B mod (MOD-1). However careful: If a=0, then result is 0 for B>0. But also if B=0? But constraints: B>=1, so fine.
Thus approach: For each test case:
-
Read A and B as strings (since huge).
-
Compute a = large number A modulo MOD.
-
Compute b = large number B modulo (MOD-1) (MOD-1 = 1000000006). But caution: Actually if B == 0 then exponent 0? Not possible (B>=1). However if we reduce modulo MOD-1 and get b = 0, then exponent is multiple of MOD-1, so a^(B) ≡ a^0 = 1 mod MOD, provided a != 0. That's correct.
-
Then compute pow_mod(a, b, MOD) using fast exponentiation. But careful: If a == 0, then we need to output 0 (since B>=1). However what if a == 0 but b is computed from B mod (MOD-1) = 0? But if a == 0, we should output 0 anyway regardless of exponent (since 0^positive = 0). So we can treat separately.
Edge cases: MOD = 1e9+7, MOD-1 = 1000000006.
But note: B could be huge (100k digits). So we need to compute B mod (MOD-1) efficiently by iterating over digits.
Similarly compute A mod MOD.
Complexities: O(len(A)+len(B)) per test.
Now check the example:
- 3 2 -> a=3, b=2 mod 1000000006=2 -> 3^2=9.
- 4 5 -> 4^5=1024. mod check works.
- 7 4 -> 7^4=2401.
- 34534985349875439875439875349875 93475349759384754395743975349573495 -> The answer 735851262.
- etc.
Thus solution should be fine.
Implementation details: Use Python or C++ with big integer? Input up to 100k digits, cannot use built-in big integers? Python can handle arbitrarily long integers, but reading 100k digits and converting to int might be heavy but possible? Actually Python's int can handle 100k digits; conversion from string to int is O(n) essentially. But we have T up to 10, so total digits up to 210100k = 2 million digits. Python should handle that, but converting huge string to int for each test may be memory heavy but still okay. However computing pow with huge exponent using built-in pow might cause performance issues because exponent is huge (like 100k digits). But we can use pow with three arguments: pow(a, b, MOD) where b is int. But if we convert B to int, it becomes an integer with 100k digits, which is huge but Python's pow can handle modular exponentiation using exponentiation by squaring, which takes time O(log b) where log b is number of bits of b. That's about O(len(B)*log(10)) ~ 300k bits, which is huge (around 332k bits? Actually number of bits ~ digits * log2(10) ≈ 100k * 3.3219 ≈ 332k bits). pow modular in Python is implemented in C and can handle exponent with many bits; it iterates over bits, so number of iterations equals number of bits of exponent. That's about 332k loops, which is okay. For T=10 that's 3.3M loops, still fine. So actually we could simply read A and B as Python ints and compute pow(A % MOD, B, MOD) directly. But careful: pow(A, B, MOD) with B huge might be slower? But it should still be fine because Python's modular exponentiation uses exponent bits; with exponent of 332k bits, that's 332k multiplications modulo MOD, each multiplication is cheap (64-bit numbers because MOD fits in 64-bit). That's fine. So simplest solution: Read strings, convert to int, then pow(A % MOD, B, MOD) if A % MOD != 0 else 0. However we need to watch out: converting to int from a 100k-digit string: Python can do it but memory/time? It will allocate a large integer object of size about 100k digits = ~ 332k bits ~ 41 KB per int? Actually Python's int uses 30-bit limbs, so number of limbs = ceil(332k/30) ≈ 11067 limbs, each limb 4 bytes? Actually Python's int uses 30-bit digits in 32-bit, but 64-bit builds use 30-bit as well; each digit is 30 bits stored in a 32-bit word. So 11067 limbs ~ 44KB. That's not huge. T=10 gives about 880KB. That's okay. But conversion from string to int is O(n^2)? Actually Python's int conversion from decimal string uses O(n^2) algorithm? I'm not sure; in CPython, int from a decimal string uses a base-conversion algorithm that is roughly O(n^2)? I think it uses a recursive splitting approach that is O(n log n) or O(n^2) depending. For 100k digits, it might be slow. Actually reading a huge string and converting to int could be costly. There are known issues: Converting a 100k-digit decimal string to int in Python can be relatively slow because it's O(n^2) in worst-case? Let's recall: Python's int constructor for decimal strings uses an algorithm that processes digits in groups to build an integer. I think it's O(n^2) if naive, but they use something like divide and conquer that is O(n log n). For 100k digits, it's likely fine (maybe a few hundred ms). But total for T=10, might be a couple of seconds, but likely acceptable given typical contest time limits? However to be safe, we can compute modular reduction without full conversion to int. That is more efficient and less memory.
Thus I'll implement manual modulo of A and B with MOD and MOD-1 respectively. That way we don't need to hold the entire integer.
But note: We also need to compute pow(a, b, MOD) which uses small ints. So that's efficient.
Hence algorithm:
MOD = 1000000007
PHI = MOD - 1
For each test case:
- read line with two strings A_str and B_str (split)
- compute a = 0; for each char c in A_str: a = (a * 10 + int(c)) % MOD
- compute b = 0; for each char c in B_str: b = (b * 10 + int(c)) % PHI
- if a == 0: result = 0 (since B>=1, but also check if B==0? Not needed)
- else: result = pow(a, b, MOD)
- print result
But caution: When a is not 0, we used b = B mod phi. However, there is subtlety: What if a mod MOD == 0? Already handled. But also if a mod MOD != 0, we reduce exponent mod phi. However note: If a mod MOD == 0 and B == 0, then 0^0 is undefined, but B>=1, so fine.
Thus answer.
But check: Example 4: Output 735851262. Let's test quickly mentally? We can trust.
Thus solution.
But consider that B could be 0? Constraints say B >= 1, so okay.
Now handle input reading: T lines. Each line contains two large integers separated by space. The integers can have up to 100k digits each, so each line could be up to 200k+1 characters. Use sys.stdin.read() to read all at once? Or line by line using sys.stdin.readline? That should be fine.
Implementation in Python:
import sys
MOD = 1000000007
PHI = MOD - 1
def mod_string(s, mod):
res = 0
for ch in s:
res = (res * 10 + (ord(ch) - 48)) % mod
return res
def solve():
data = sys.stdin.read().strip().split()
# After reading, first token is T, then pairs of A and B.
if not data:
return
t = int(data[0])
out = []
idx = 1
for _ in range(t):
a_str = data[idx]; b_str = data[idx+1]; idx += 2
a = mod_string(a_str, MOD)
b = mod_string(b_str, PHI)
if a == 0:
out.append('0')
else:
out.append(str(pow(a, b, MOD)))
sys.stdout.write('\n'.join(out))
if name == "main":
solve()
But note: The exponent reduction using phi is valid only when base is coprime with modulus. If base mod MOD != 0, it is coprime since MOD is prime, so yes.
Edge case: Suppose a != 0 and b computed as B mod phi, but what if B is huge and b = 0? Then pow(a,0) = 1, correct.
Also need to consider possibility that a mod MOD != 0 but B may be 0? Not allowed.
Thus this is correct.
But wait: There's nuance: Actually the exponent reduction using Fermat's theorem is: a^(k * (MOD-1) + r) ≡ a^r mod MOD, provided a is not divisible by MOD. That's correct.
Now test with example:
Case 4:
A=34534985349875439875439875349875
We can compute a mod MOD manually maybe not needed.
Let's test with Python quickly (conceptually) but trust.
Now check constraints: A,B up to 10^100000 meaning they can have up to 100000 digits, maybe even more? Actually "maximum of 100000 digits". So our mod_string loops up to 100k per number, which is fine.
Now time complexity: O(total digits) ~ 2100k10 = 2M operations, trivial.
Thus solution is efficient.
We should ensure we handle leading zeros? The integers are positive, so leading zeros maybe not present, but even if they are, mod_string works.
Now ensure we treat when a != 0 but maybe b is computed as 0 from B mod phi. Then pow(a,0,MOD)=1. That's fine.
Potential issue: When B is 0? Not allowed.
Thus done.
Now provide final answer: code.
But note: There's also a nuance: When a is divisible by MOD, and B=0, we would have 0^0, but not happening.
Now final code.
We'll output as per required.
Thus solution.
But also note: The problem statement says: "Your task is to ask the Linkin Park team on the Bth day." So cost on day B is A^B? Actually reading: On day 1 cost is A dollars. After each day, cost becomes A times the price on the previous day. So cost on day n = A * (previous day cost) = A * (A^(n-1))? Actually day1: A. Day2: A * A = A^2. So day B: cost = A^B? Wait if B days after day1? They need B days to get approvals before making the offer. So they ask on the Bth day. If day1 is today, they need B days to get approvals, so they ask on day (1 + B?) Actually careful: They need B days to get approvals before making the band an offer. So they will make the offer after B days. The cost on day1 is A. After each day, cost multiplies by A. So after B days, the cost becomes A^(B) ? Let's think: Starting day1: price = A. After 1 day (day2): price = A * A = A^2. After B-1 days? Actually if they need B days to get approvals, they will make the offer on day (B+1)?? The problem says: "Your task is to ask the Linkin Park team on the Bth day." So they ask on the Bth day. So day index: Day1 cost A, day2 cost A^2, day3 cost A^3, ..., day B cost A^B. So correct.
Thus exponent is B.
Now final solution.
Now we need to produce answer as python code.
One more nuance: In Python, the pow function with mod works with exponent as int. We'll compute b as an int from mod_string, which is small (< MOD-1). So pow(a, b, MOD) is fine.
Also we need to treat a == 0 and B > 0, output 0.
Now produce final answer.
But wait: The problem says: "Both integers will have a maximum of 100000 digits." That means A and B each up to 10^100000, but not negative. So our reading using split is fine.
Now the answer.
Potential pitfalls: When reading with sys.stdin.read().split(), if there are newline characters, it's okay. But the numbers are huge, so using split() may load entire input into memory as list of strings, but each up to 100k characters, and T up to 10, so total size ~ 2 million characters plus overhead, which is fine.
Alternative: Use sys.stdin.readline per test case to avoid loading all at once. But both are fine.
We'll use read().split() for simplicity.
Now let's double-check with examples:
Input:
5
3 2
4 5
7 4
34534985349875439875439875349875 93475349759384754395743975349573495
34543987529435983745230948023948 3498573497543987543985743989120393097595572309482304
Compute using code? I'd like to test manually with Python but can't run here. But we trust.
Thus final answer.
Now provide explanation.
But one more nuance: If a mod MOD != 0, we use b = B mod (MOD-1). But what about case when B is huge and we need to consider exponent 0? Already handled. However, there is another subtlety: In modular exponent reduction, the exponent is taken modulo φ(m) only if the exponent is used in a context where a and m are coprime. That's correct.
But wait: What if a mod MOD is not zero but is a multiple of MOD? That's impossible because mod MOD yields 0 if multiple. So fine.
Now final answer.
But maybe there is also need to handle B being extremely large and we want to compute exponent modulo φ(m) but need to consider that exponent could be 0 after reduction. That's fine.
Now I'm ready to write the final solution.
We'll produce a clean solution.
Also note: For safety, we might consider using fast exponentiation manually, but pow is fine.
Now writing final solution.
In the final answer, we can include comments.
Thus final. ```python
import sys
MOD = 1_000_000_007 # prime modulus
PHI = MOD - 1 # φ(MOD) because MOD is prime
def mod_string(s: str, m: int) -> int:
"""Return the integer represented by decimal string s modulo m."""
res = 0
for ch in s:
res = (res * 10 + (ord(ch) - 48)) % m
return res
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
t = int(data[0])
out = []
idx = 1
for _ in range(t):
a_str = data[idx]
b_str = data[idx + 1]
idx += 2
a = mod_string(a_str, MOD) # A mod MOD
b = mod_string(b_str, PHI) # B mod (MOD-1)
if a == 0:
# A is a multiple of MOD, and B >= 1 → result is 0
out.append('0')
else:
# a and MOD are coprime, use Fermat to reduce exponent
out.append(str(pow(a, b, MOD)))
sys.stdout.write('\n'.join(out))
if name == 'main':
solve()