Vasya has got two number: a and b. However, Vasya finds number a too short. So he decided to repeat the operation of lengthening number a n times.
One operation of lengthening a number means adding exactly one digit to the number (in the decimal notation) to the right provided that the resulting number is divisible by Vasya's number b. If it is impossible to obtain the number which is divisible by b, then the lengthening operation cannot be performed.
Your task is to help Vasya and print the number he can get after applying the lengthening operation to number a n times.
Input
The first line contains three integers: a, b, n (1 ≤ a, b, n ≤ 105).
Output
In a single line print the integer without leading zeros, which Vasya can get when he applies the lengthening operations to number a n times. If no such number exists, then print number -1. If there are multiple possible answers, print any of them.
Examples
Input
5 4 5
Output
524848
Input
12 11 1
Output
121
Input
260 150 10
Output
-1
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to understand the problem: Given a, b, n. Start with number a (as a string? as integer? but a <= 10^5). We need to perform n times: append exactly one digit (0-9) to the right such that the resulting number is divisible by b. If at any step it's impossible (no digit works), then the whole fails, output -1. Else, after n steps, output the resulting number (as integer without leading zeros, but a might have leading zeros? Actually a is an integer, so no leading zeros. The appended digits can be 0-9, but the resulting number must be divisible by b. Also "print any of them" if multiple.
We need to construct a string representation of the number, appending digits.
Observations: This is similar to extending a number to make it divisible by b by appending digits. At each step, we have a current number x. We need to find a digit d (0-9) such that (10x + d) % b == 0. That is d ≡ (-10x) mod b, but modulo 10? Actually we need d between 0 and 9, so we need to check if there exists d in [0,9] satisfying (10x + d) % b == 0. That is d ≡ (-10x) mod b. Since d must be in 0..9, we can compute required remainder r = (b - (10x % b)) % b. Then we need 0 ≤ r ≤ 9. If r is in that range, then we can append digit r. However, is that the only digit? Because the congruence is modulo b, but d is a digit (0-9) but the equation is modulo b, which might have multiple solutions if b > 10? Actually the equation (10x + d) ≡ 0 mod b => d ≡ -10x mod b. But d is between 0 and 9, and modulo b there is exactly one residue class mod b, but d is an integer, not a residue class modulo b? Wait, d is an integer digit 0..9. The condition is (10x + d) % b == 0. This is equivalent to d ≡ -10x (mod b). So there could be many integers d that satisfy that congruence, but they differ by multiples of b. Since b can be up to 10^5, but d is limited to 0..9, so there can be at most one solution in that range (if b > 9 then any two numbers that differ by b cannot both be within 0..9 because b > 9 => the distance >= 10000? Actually b could be less than 10, e.g., b=2,3,5,7, etc. For b <= 9, there could be multiple digits satisfying because if b=2, then -10x mod 2 is either 0 or 1? Actually mod 2, -10x is even? 10x is always even? 10x is even if x integer? Actually 10 is even, so 10x is even, so -10*x mod 2 = 0. So d must be ≡0 mod2, so digits 0,2,4,6,8 are valid. So multiple digits. For b=1, any digit works. So we have multiple possible digits if b <= 9. But the problem says print any.
So algorithm: start with current integer a_mod = a % b? But careful: we need to construct the full number, but we can work mod b to decide digits. Actually we can simulate: let cur = a % b. For each step, we need to find a digit d (0-9) such that (10cur + d) % b == 0? Wait: If we have the current number x (as integer), then after appending digit d, the new number y = 10x + d. Then y mod b = (10*(x mod b) + d) mod b. So if we keep track of current remainder r = x mod b, then we need to find d in [0,9] such that (10r + d) % b == 0 => d ≡ -10r mod b. So the condition depends on r, not on full x.
Thus we can compute remainder r = a % b initially. Then for n steps: compute required digit: need = (-10r) mod b, which is (b - (10r % b)) % b. Check if need <= 9. If not, we could also consider adding multiples of b? Actually we need d in 0..9 that satisfies d ≡ need mod b. The general solution is d = need + kb for integer k. Since d must be in 0..9, we can check if there exists integer k such that 0 ≤ need + kb ≤ 9. Since b could be less than 10, there may be multiple k. For b>9, the only possible k is 0 because b >9 => need + b >= b >9 (if need >=0). But need might be >9? Actually need is between 0 and b-1. For b>9, if need <=9, then d = need works. If need >9, then d = need is not acceptable; could we try d = need - b? That would be negative because need < b and b >9, so need - b < 0. So no solution. For b <=9, need may be <=9 and also possibly need - b? Actually need is in [0,b-1]. For b <=9, need itself is ≤8 (since b-1 ≤8). So need always ≤8 which is ≤9, so need works. But also need + b may be ≤9? Since b ≤9, need + b ≤ (b-1)+b = 2b-1 ≤ 17. Could be ≤9 if b small? For b=9, need max 8, need+9=17 >9, so not. For b=8, need max 7, need+8=15 >9, so not. For b=7, need+7 max 13 >9. For b=6, need+6 max 11 >9. For b=5, need+5 max 9 (if need=4, 4+5=9). For b=4, need+4 max 7? Actually need max 3, 3+4=7 ≤9, so both need and need+4? But need+4 is also a valid digit? It would be between 5 and 7? Actually if need=0, need+4=4 ≤9. So possible. For b=3, need+3 and need+6 can be ≤9: need max 2, need+3 ≤5, need+6 ≤8. So multiple. For b=2, need max 1, need+2, need+4, need+6, need+8 all ≤9. For b=1, need=0, any digit works because any d satisfies? Actually if b=1, condition is always divisible, so d can be any 0-9. So multiple digits.
So to generalize: At each step, we need to find any d in [0,9] such that (10r + d) % b == 0. That is equivalent to d ≡ -10r (mod b). We can find the smallest nonnegative residue: need = ((-10r) % b + b) % b? Actually -10r mod b yields a value in 0..b-1. That is need = (b - (10r % b)) % b. Then we can try d = need, need + b, need + 2b, ... as long as ≤9. Also we could try negative multiples? d = need - b could be negative, not allowed. So we can simply check if need <= 9, then choose d = need. But for b <=9, need always ≤ b-1 ≤8 ≤9, so need <=9 always. However, what about b=10? b=10, need = (b - (10r % b)) % b. Since b=10, 10r %10 = 0, so need = 0. So need=0 <=9, works. So for b=10, also works. Actually if b>9, need might be >9? Let's test: b=11, r could be such that 10r %11 =? For example, a=12, b=11, n=1: initial remainder r=12%11=1. 10*r=10, 10%11=10, need = (11-10)%11=1, need=1 ≤9, works => digit 1 gives 121. That's example 2. For b=12, suppose r=1 => need = (12-10)%12=2 <=9 works. r=2 => need = (12-20%12)= (12-8)=4? Actually 20%12=8, need=4<=9. r=3 =>30%12=6, need=6 <=9. r=4 =>40%12=4, need=8 <=9. r=5 =>50%12=2, need=10? 12-2=10, need=10 >9. So if r=5, need=10 >9, and there is no other solution because need+b=22 >9. So for b=12 and r=5, no digit works. So indeed for b>9, need might be >9, leading to impossible step.
But wait, is need the only candidate? Suppose need=10 and b=12. Could there be d = need - b? need - b = -2 not allowed. So no. So we must check if there exists d in [0,9] such that d ≡ need (mod b). For b>9, the only possible d in [0,9] that is congruent mod b to need is d itself (if need <=9) because adding/subtracting b would go out of range. So condition: need <=9.
For b<=9, need is always <= b-1 <=8, so need <=9 always true. However, there might be more digits available, but we can just choose need. But caution: need could be 0? That's fine. Also note that the digit can be 0, that's allowed. So for b<=9, we can always find a digit? Actually is it always possible? Let's test: b=8, r= something: need = (8 - (10r %8)) %8. Since 10 mod 8 =2, so need = (8 - (2r mod8))%8. For any r, 2*r mod8 can be 0,2,4,6. Then need is 0,6,4,2 respectively, all <=7. So need in 0..7 always <=9, so we can append digit need. So always possible for b<=9. But what about b=1? need=0 works. So indeed for b <=9, any step is always possible? Let's check b=9: 10 mod9 =1, need = (9 - (r mod9))%9, which yields values 0..8, all <=8, so possible. b=7: 10 mod7=3, need = (7 - (3r mod7))%7, yields 0..6, all <=6, possible. b=5: 10 mod5=0, need=0 always, so always possible (digit 0). b=3: 10 mod3=1, need = (3 - (r mod3))%3 gives 0,2,1? Actually r mod3 can be 0,1,2; need = (3 - (r mod3))%3 = 0,2,1 respectively; all <=2, digit can be 0,1,2, possible. So for b <=9, at any step there is always a digit (need itself) that works. So we can always extend arbitrarily.
But is there any scenario with b<=9 where need >9? No because need <= b-1 <=8.
So the only cases where extension might be impossible are when b > 9 and at some step need >9.
But careful: When b >9, need is between 0 and b-1. It could be >9, causing impossibility. So if at any step we get such an r that need >9, we output -1.
Also note that after appending a digit, the new remainder becomes 0 because we require the new number to be divisible by b. Actually condition is that after appending a digit, the resulting number must be divisible by b. So we choose d such that (10x + d) % b == 0. That means after the step, the remainder becomes 0. But then the next step: we need to append another digit to this number which is divisible by b. However, if the current number is divisible by b, then its remainder r = 0. For the next step, we need to find d such that (100 + d) % b == 0 => d % b == 0. So d must be a multiple of b. Since d is a digit 0-9, the only possible d is 0 if b > 0, and if b <= 9? Actually d must be 0 modulo b. For b>9, the only digit that is 0 modulo b is 0 (since b>9, other multiples like b,2b are >9). So d=0 works. For b <=9, there may be other digits that are multiples of b: e.g., b=3, digits 0,3,6,9; b=5, digits 0,5; b=1, all digits. But at least d=0 is always a valid digit (0). So once we get a number divisible by b, we can always extend further by appending 0 repeatedly? Let's verify: if current number x is divisible by b (r=0), then we need d such that (100 + d) % b == 0 => d % b == 0. So d must be a multiple of b. If b>9, only d=0 works (since b>9, the next multiple b is at least 10, not allowed). So we can append 0. For b<=9, there are multiple digits, but 0 always works. So we can always extend by 0. However, we must be careful: Is it allowed that the digit we append is 0? Yes, it's a digit. The resulting number will have trailing zeros, but that's fine. So once we get a number divisible by b, we can extend it arbitrarily by adding zeros. That means if we can achieve a divisible number after the first digit append, then for the remaining steps we can just append zeros. But wait: The condition says we must apply the lengthening operation n times. So we need to perform n steps. At each step, we must append a digit such that the resulting number is divisible by b. So if we get to a divisible number earlier, the subsequent steps are constrained: the new number must also be divisible after each step. But as argued, if the current number is divisible, we can always append 0 to keep it divisible (since 0 % b = 0). However, is it always allowed? For r=0, we need d such that (100 + d) % b = d % b == 0. So d must be a multiple of b. For b > 0, d = 0 always works. So yes, we can append zeros. So after we achieve divisibility once, the rest is trivial.
But careful: The process requires that each operation, the number after appending must be divisible by b. Starting from a, which may not be divisible by b. After first step, we get number divisible by b. Then after second step, we need to append a digit such that the new number (which is (previous number * 10 + d)) is divisible by b. Since previous number is divisible by b, say x = kb. Then new number = 10kb + d = b(10*k) + d. For this to be divisible by b, we need d % b == 0. So d must be a multiple of b. As argued, d=0 works. So indeed we can just append zeros.
Thus a strategy: We need to find a single digit (maybe more than one step? Actually we need to append n digits. But we can think of the whole process: we need to find a sequence of n digits d1,d2,...,dn such that after appending them one by one, each intermediate number after i appends is divisible by b? Wait, the problem statement: "repeat the operation of lengthening number a n times. One operation of lengthening a number means adding exactly one digit to the number (in the decimal notation) to the right provided that the resulting number is divisible by Vasya's number b." So after each operation, the resulting number must be divisible by b. That means after the first digit appended, the number must be divisible by b. After the second digit appended (starting from that divisible number), the new number must be divisible by b, etc. So each step maintains divisibility. So indeed the process is: x0 = a. For i=1..n: choose digit di such that xi = 10*xi-1 + di is divisible by b.
Thus we need to find any such sequence.
Observation: Since after the first step we achieve divisibility, we can then just append zeros for the remaining steps. So the core is: can we find a single digit d (0-9) such that (10a + d) % b == 0? If yes, then we can do: first digit = that d, then n-1 digits = 0. However, this only works if after the first digit we get a number divisible by b, and then we can append zeros. But wait: Could it be that after first digit we get a divisible number, but then when we append zero, the new number might not be divisible if b>10? Let's check: Suppose we have x1 = 10a + d, which is divisible by b. Then x2 = 10x1 + 0 = 10x1. Since x1 divisible by b, 10x1 is divisible by b as well (b divides x1, so b divides 10x1). So yes. So zeros always work. So that's valid.
But is it necessary that we achieve divisibility in exactly one step? Could it be that no single digit works to make (10a + d) % b == 0, but we could use multiple steps to eventually achieve divisibility? For example, maybe a first digit can be chosen such that the resulting number is not divisible, but that would violate the condition because after the first operation the number must be divisible. So after each operation the number must be divisible. So after the first operation, it must be divisible. So the first digit must achieve divisibility. So the condition is: there must exist a digit d1 (0-9) such that (10a + d1) % b == 0. If not, then it's impossible to perform even the first operation, so answer -1. Because after the first step we need the number divisible by b. There's no way to postpone divisibility to later steps because after the first step the number must already be divisible. So the problem reduces to: Check if there exists a digit d (0-9) such that (10a + d) is divisible by b. If not, output -1. If yes, we can output the number formed by a followed by that digit and then n-1 zeros? Wait, careful: We need to perform n operations, so after first digit we have one digit appended. Then we need to append n-1 more digits, each chosen to keep divisibility. As argued, we can just append zeros. So the final number will be: a concatenated with d (the first digit) and then (n-1) zeros. However, is that always valid? Let's check: After the first step, number = 10a + d, divisible by b. After second step, we append 0, number = 10*(10a + d) + 0 = 100a + 10d, divisible by b. After third step, number = 1000a + 100*d, etc. Yes, all divisible. So that's a valid construction.
But wait: Is it allowed that the appended digits after the first can be zero? Yes, they are digits. The resulting number may have trailing zeros, but that's fine; it's an integer without leading zeros, but trailing zeros are okay. Also note that if we choose d as 0? Could d=0 be the first digit? That would mean (10*a) % b == 0. If that's true, then we can just append 0 first and then n-1 zeros, essentially n zeros. That's allowed.
Thus the solution seems simple: Try digits 0 through 9 to see if (10*a + d) % b == 0. If none works, output -1. Else, choose any such digit d, output a, then d, then n-1 zeros.
But is there any catch? Let's test with provided examples:
Example 1: a=5, b=4, n=5. Check digits: 10*5=50. 50%4=2. Need d such that (50+d)%4==0 => d%4 must be 2? Actually (50+d)%4 = (2+d)%4==0 => d%4=2. Digits with %4=2: 2,6. So d=2 or 6. Choose d=2. Then output 5 2 and then 4 zeros? Actually n=5, so we need total of 5 appended digits. So first digit =2, then 4 zeros => number: 5 2 0 0 0 0 => 520000. But example output is 524848. That's different. But our method yields a valid number? Let's check: 520000 %4 =? 520000 mod4 = 0 (since last two digits 00). So yes, it's divisible by 4. So our output would be 520000, which is a valid answer? The problem states: "If there are multiple possible answers, print any of them." So 520000 should be acceptable. The example output is 524848, but that's just one of many possible. So our solution would output a different number but still correct. However, we need to verify that after each step, the number is divisible. Let's simulate: Start 5. Append 2 => 52. 52%4=0, ok. Append 0 => 520. 520%4=0, ok. Append 0 => 5200%4=0, ok. Append 0 => 52000%4=0, ok. Append 0 => 520000%4=0, ok. So yes. So 520000 is acceptable. But perhaps the problem expects exactly the output format? They said print any. So our solution is valid.
But wait: Could there be a scenario where after first digit we get divisibility, but then appending zeros fails because we need to append exactly one digit each time and the resulting number must be divisible. As argued, it's fine. So that seems trivial.
But what about the scenario where b > 9 and the required digit need >9? Then no digit works, so we output -1. Example 3: a=260, b=150, n=10. Check digits: 10260=2600. 2600%150 =? 15017=2550, remainder 50. Need d such that (2600+d)%150==0 => d ≡ 100? Actually (2600 mod150 = 2600 - 150*17=2600-2550=50). To make divisible, we need d such that (50+d)%150==0 => d = 100 (since 50+100=150). 100 is not a digit. So no digit works. So output -1. Matches example.
But is it always that after first digit we achieve divisibility, and zeros work for rest? The reasoning seems solid.
However, I recall a known problem from Codeforces: "Vasya and Digital Root" maybe? Actually this is CF problem 260A? I think it's "Adding Digits" or something. The typical solution is: try digits 0-9 to find first digit, then output a, then that digit, then n-1 zeros. Yes, I've seen this problem. So it's straightforward.
But wait: There is a nuance: The problem statement: "Your task is to help Vasya and print the number he can get after applying the lengthening operation to number a n times." It doesn't say we have to output the smallest or anything. So any valid number works.
Thus algorithm:
Read a, b, n.
Initialize a_str = str(a) (though we can just output using print).
Loop d from 0 to 9:
if (10*a + d) % b == 0:
output a, then d, then '0' * (n-1) (as string)
exit.
If no d found, print -1.
But is a up to 10^5, n up to 10^5, so the resulting number length = len(str(a)) + n, which could be up to 6 (since a <= 100000, that's at most 6 digits) + 100000 = up to 100006 digits. That's huge but we can output as a string. So we need to output a string of length len(a_str)+n. That's up to 100006 characters, which is fine.
Now, note: a could be large (but up to 100000, so at most 6 digits). So we can compute (10a + d) mod b using integers (64-bit). Since a up to 1e5, 10a up to 1e6, fine.
But what about if we choose d and then output n-1 zeros? That yields a number that is divisible by b after each step? Let's double-check the condition for the second step: after first step, number x1 = 10a + d, which is divisible by b. For second step, we choose digit 0: x2 = 10x1 + 0 = 10x1. Since x1 divisible by b, 10x1 is also divisible by b. So condition holds. So yes.
Thus solution is trivial.
But wait: There is a subtlety: After we append the first digit, the number is divisible by b. After that, we append zeros, but what if b is not relatively prime to 10? That's okay. There is no issue.
So the answer is simple.
But is there any scenario where (10a + d) % b == 0 for some digit d, but then after appending zeros, at some later step, the condition fails? Let's examine: After k zeros, number = (10a + d) * 10^{k}. Since (10*a + d) is divisible by b, the whole product is divisible by b. So it's fine.
Thus the answer is as described.
Now, what about the case when b <= 9? As argued, there will always be at least one digit d that works, because need = (-10a) mod b is in [0,b-1] <=8, so d = need works. So we will always find a d. So never output -1 for b<=9? Actually check: If b <= 9, then we can always find a digit? Let's test a=1, b=9, n=... 101=10, 10%9=1, need = (9-1)%9=8, d=8 yields 18%9=0. Works. So yes.
But is there any edge case: a can be up to 1e5, but b can be up to 1e5, but b <=9 trivial.
Now, consider a=0? But a >=1 per constraints (1 ≤ a). So a is at least 1.
Now, implement.
But wait: The problem statement says: "If it is impossible to obtain the number which is divisible by b, then the lengthening operation cannot be performed." That suggests that if at any step it's impossible, the whole process fails.
So we only need to check the first step? But what about the possibility that after first digit we get divisibility, but then for some later step, appending zeros might not be allowed because maybe zeros are not considered digits? Actually zero is a digit. So fine.
Thus answer.
But let's confirm with another example: Input: a=2, b=20, n=3. 10*a=20, (20+d)%20 = d%20. Need d such that d%20==0 => d=0 only. So d=0 works. Then output 2, then 0, then n-1=2 zeros => 2000. Check: 20 is divisible by 20? Actually 2 with appended 0: 20, divisible. Then 200, divisible? 200%20=0. 2000%20=0. Good.
But note: b=20 >9, but d=0 works. So need=0 <=9.
Now, what about a=10, b=20, n=1? 1010=100, (100+d)%20 = (0+d)%20? Actually 100%20=0, so need d%20==0 => d=0. So output 100? Actually a=10, append d=0 => 100, divisible by 20? 100%20=0? 205=100, yes. Good.
Now, what about a=11, b=20, n=1? 110+d, 110%20=10, need d=10, but 10>9? Actually d must be digit 0-9, so 10 not allowed. So no digit. So -1. Indeed 11 cannot be extended with one digit to become divisible by 20 because 110-119 range: 110%20=10, 111%20=11, ... 119%20=19, none 0. So -1.
Now, what about a=12, b=20, n=1? 120%20=0, d=0 works => 120.
So overall, algorithm holds.
But is there any scenario where the first digit works but then we need to ensure that the appended zeros keep the number divisible? That's fine.
Thus solution.
But wait, what about a case where b is not necessarily a divisor of 10? That doesn't matter.
Thus answer.
But I recall a known Codeforces problem: "Vasya and Digital Root" maybe? Actually it's Codeforces Round #323 (Div. 2) problem A? Or problem "Adding Digits"? The typical solution is to try digits 0-9, if found, print a, digit, and n zeros? Actually they printed n zeros? Let's check typical solution: For Codeforces problem 260A "Adding Digits", the solution is exactly: try d 0..9, if (10*a + d) % b == 0, then print a, d, and then n-1 zeros. Yes, that is standard.
But let's double-check problem statement: "Vasya has got two number: a and b. However, Vasya finds number a too short. So he decided to repeat the operation of lengthening number a n times. One operation of lengthening a number means adding exactly one digit to the number (in the decimal notation) to the right provided that the resulting number is divisible by Vasya's number b. If it is impossible to obtain the number which is divisible by b, then the lengthening operation cannot be performed. Your task is to help Vasya and print the number he can get after applying the lengthening operation to number a n times. If no such number exists, then print number -1. If there are multiple possible answers, print any of them." That matches.
Now, note: The example 1 output is 524848. That corresponds to a=5, b=4, n=5. How did they get 524848? That is: start 5, append digits: 5 2? Actually 524848: digits: 5, then 2,4,8,4,8? That's 5 digits appended: 2,4,8,4,8. But we got 5 2 0 0 0 0. Both are valid. So any.
Thus solution: find d in 0..9 such that (a*10 + d) % b == 0. If none, print -1. Else print a, d, and then (n-1) times '0'.
But is there a corner case: n=0? The constraints say n ≥ 1. So n>=1.
Now, what about a with leading zeros? a is integer, so no leading zeros. So output starts with a's decimal representation.
Now, ensure we output as integer without leading zeros. If we output a, then d, then zeros, there will be no leading zeros because a has no leading zeros. However, what if a=0? But a>=1, so no.
Thus code is simple.
But wait: Let's test with a=1, b=7, n=10. 10%7=3, need = (7-3)%7=4, d=4 works. Then output: "1"+"4"+"0"9 = 14000000000? That's 1 followed by 4 then 9 zeros = 11 digits. Is this divisible by 7? 14 is divisible by 7, then zeros keep divisibility? 140 is divisible by 7? 720=140 yes. So okay.
Now, what about b=0? But b is at least 1 per constraints. So ignore.
Thus solution is fine.
But wait: Is there any scenario where after first digit we achieve divisibility but then appending zeros leads to a number that may have leading zeros? No, leading zeros not introduced.
Thus I think solution is correct.
Now, we must be careful about integer overflow? a up to 10^5, 10*a up to 10^6, plus d up to 9, so fits in int.
But when we output the number as a string, we don't compute the large integer.
Implementation:
Read a, b, n as integers.
For d in range(10):
if (10*a + d) % b == 0:
result = str(a) + str(d) + '0' * (n-1)
print(result)
return
print(-1)
But careful: n-1 zeros means we need to append n-1 times '0'. That is correct because we already appended one digit (d) as part of first operation. So total appended digits = n.
Edge Cases:
n=1: then we only need one digit. So we output a and d only (since n-1=0 zeros). So that works.
If we found d, we output. If not, -1.
But wait: There is also the possibility that after the first digit, we might need to append more digits but zeros might not be valid if b has some factor that requires something else? As argued, it's valid. However, consider b=125 and first digit yields a number divisible by 125. Then appending zeros yields numbers like 10 each time. 125 divides a number ending with zeros? For example, start a=?, b=125. After first digit, say x1 divisible by 125. Then x2 = x1 * 10. Since 125 divides x1, 125 divides x110? Not necessarily if 125 and 10 are not coprime? Actually if 125 divides x1, then x1 = 125k. Then x2 = 125k10 = 1250k, which is still divisible by 125 because 1250 = 12510, so yes, 125 divides 1250k. So it's fine. In general, if b divides N, then b divides N10^m because multiplying by 10 doesn't cancel divisibility? Actually careful: If b divides N, then N = b * q. Then N * 10 = b * (q10). So b divides N*10. So yes, always.
Thus zeros always work.
But is there any scenario where b has factor 2 or 5 and we need to consider something about trailing zeros? No.
Thus solution stands.
But wait: I recall that in some Codeforces editorial, they mentioned that after finding the first digit, they simply output n-1 zeros. Yes.
Now, we must also consider a nuance: The initial number a itself might already be divisible by b. Could we then just append zeros without any additional digit? The operation requires that each lengthening operation adds exactly one digit and the resulting number must be divisible by b. So if a is already divisible by b, can we start by appending a digit? The first operation still needs to be performed. Even if a is divisible by b, we still need to add a digit such that the new number is divisible. So we cannot skip adding a digit. But we could choose d=0 as the first digit because (10a + 0) = 10a, which is divisible by b? Not necessarily. If a is divisible by b, then a = bk. Then 10a = 10bk, which is divisible by b. So d=0 works. So we can still follow the same method. However, our loop from 0 to 9 will include d=0. So it's fine. But what if a is divisible by b and we might also have other digits? We can choose any.
So method works.
Now, confirm with a scenario where a divisible by b but the first digit found might be something else, but we can still choose 0. Our loop will check d=0 first perhaps, so we will pick d=0 if (10a)%b==0. Since a divisible by b implies 10a divisible by b, so (10*a+0)%b==0. So yes.
Now, consider a=3, b=6, n=1. 30%6=0, so d=0 works => 30. That's divisible by 6. Output 30. If we didn't find 0? Actually 30%6=0, so works.
Now, what about a=3, b=9, n=1. 30%9=3, need d=6, 36%9=0, so d=6 works.
All good.
Thus solution is trivial.
But wait: There is a nuance: The problem statement: "If it is impossible to obtain the number which is divisible by b, then the lengthening operation cannot be performed." That means for a particular step, if no digit works, the operation cannot be performed, and thus the whole process fails. So we only need to check the first step because subsequent steps we can always use 0 if we already have divisibility. However, is it guaranteed that after we get a divisible number, we can always continue for the rest n-1 steps? As argued, yes by appending 0. So the only potential failure point is the first step. So if we can find a digit for the first step, we can always complete the process.
But is it possible that after first step we get divisibility, but then appending zeros might not be allowed because the resulting number after each step must be divisible, and zeros are digits, so it's allowed. So no issue.
Thus answer: try d 0..9.
But wait, there is a scenario: b might be 0? Not allowed.
Now, about constraints: a, b, n up to 10^5. n up to 100000, output length up to 100000 + length of a (max 6). That's okay.
Now, we just implement.
But maybe we need to consider that the integer could be huge, but we output as string.
Now, some might argue that we should also consider the case where n>1 and after first digit we get divisibility, but then we need to ensure that the intermediate numbers after appending zeros are divisible, which they are. So fine.
Now, I'll provide solution.
Implementation details:
Read a, b, n from input.
For d in range(10):
if (a * 10 + d) % b == 0:
ans = str(a) + str(d) + '0' * (n - 1)
print(ans)
return
print(-1)
That's it.
But we must be careful: if we use Python, str(a) and then concatenation and then multiplication of string '0' is efficient enough for n up to 100000.
Now, test on provided examples:
Example1: a=5,b=4,n=5.
Loop d=0: (50)%4=2 !=0; d=1:51%4=3; d=2:52%4=0 => found. ans = "5"+"2"+"0"*4 = "520000"? Actually 4 zeros because n-1=4 => "5"+"2"+"0000" = "520000". That's 6 digits? Actually a=5 is 1 digit, plus d=1 digit, plus 4 zeros = total 6 digits, which matches n=5 appended digits? Wait n is number of appended digits. We appended one digit (d) and then n-1 zeros, total appended digits = n. So total digits = len(str(a)) + n. a=5 (1 digit) + n=5 => 6 digits, yes "520000" is 6 digits. So output 520000. That's valid but example gave 524848. Different but allowed.
Example3: a=260,b=150,n=10. Check d 0-9: (2600+d)%150? 2600%150=50, so need d=100, none. Print -1. Good.
Now, test edge: a=100000,b=99999,n=100000. 10*a = 1000000. 1000000%99999 =? Might be something. Could there be a digit? Possibly? But algorithm will loop 10 times, fine.
Now, time complexity O(10) = constant, memory O(L) for output string.
Thus solution is done.
But wait, is there any scenario where the first digit works but then the zeros may cause the number to have leading zeros? No, because the number starts with a which is non-zero (since a>=1). So no.
Now, confirm with a=0? Not allowed.
Thus final answer.
However, I recall in some Codeforces problem "Vasya and Digital Root" maybe there is a nuance: the number must be without leading zeros. Our output has a at front, which has no leading zeros. So fine.
Now, I'll produce final answer with explanation.
But before finalizing, let's double-check the reasoning that after we get a number divisible by b, we can always extend by zeros. Suppose b=20, a=2, d=0 gives 20 divisible by 20. Then append 0 => 200, 200%20=0, fine. So yes.
Now, is there any scenario where after first step, the resulting number is divisible by b, but then zeros are not allowed because the problem might require that the resulting number after each operation is divisible by b, but does not require that the number remains divisible after subsequent operations? No, it requires that each operation's result is divisible. So after second step, the number must be divisible; we must ensure that the second step's result is divisible. With zero it's fine.
Thus final.
But wait: Could there be a scenario where we need to consider that after the first step we might get divisibility, but then we need to perform n-1 more steps, and we can use zeros. But what if zeros are not allowed because the problem statement says "adding exactly one digit to the number (in the decimal notation) to the right"? Zero is a digit, so allowed.
Thus answer stands.
Now, I'll produce the final message: a Python solution.
One more nuance: reading input as three integers possibly separated by spaces. We'll use input().split().
Implementation:
def main():
import sys
data = sys.stdin.read().strip().split()
if not data: return
a, b, n = map(int, data)
for d in range(10):
if (a * 10 + d) % b == 0:
print(str(a) + str(d) + '0' * (n - 1))
return
print(-1)
if name == 'main':
main()
This solves.
But think about large n: '0' * (n-1) could be huge (100000 characters), that's fine.
Now, confirm with a=100, b=3, n=100000. 1000%3=1? Actually 10*a = 1000, 1000%3=1, need d=2 (since 1+2=3) => 1002 divisible by 3. Then output 1002 + 99999 zeros? Actually a=100, d=2, then n-1 zeros => string "1002" + '0'*99999, total length = 4+99999 = 100003. That's okay.
Now, output might be too big for some languages but Python can handle.
Thus final.
I'll also add a note that we could precompute the digit.
I think that's all.
Now produce final answer.
```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
a, b, n = map(int, data)
code
# try to find a digit d such that (10*a + d) is divisible by b
for d in range(10):
if (10 * a + d) % b == 0:
# once we have one correct digit, the rest can be zeros
result = str(a) + str(d) + '0' * (n - 1)
print(result)
return
# no suitable digit found
print(-1)