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.
The first line contains three integers: a, b, n (1 ≤ a, b, n ≤ 105).
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.
5 4 5
524848
12 11 1
121
260 150 10
-1
——————————————————时间不够用的分割线——————————————————
思路:请注意题目的最后一句话:If there are multiple possible answers, print any of them.
对,没错。本着解决问题的原则,这一题想到这儿就足够了:第一次扩展如果成功,那么之后的扩展全部是0就能除得尽。进行第一次扩展之后,使之除得尽b即可。
另外,如果你天真地使用 %d完成数据处理,那么你就错了。看一下a = 10^5, n = 10^5的时候。数字有多少位?
代码如下:
#include <stdio.h>
int main(){
int a, b, n;
int i, j , k, flag = 0;
char ans[200010];
for(i = 0;;i++){
scanf("%c", ans+i);
if(i == 0) a = ans[i] - '0';
if(ans[i] == ' ') break;
else if (i != 0)
a = a * 10 + ans[i] - '0';
}
scanf("%d%d", &b, &n);
for(j = 0; j <= 9; j++){
if((a * 10 + j)%b == 0)
{flag = 1;break;}//找到使之除得尽的那个数字
}
if(flag){
ans[i] = j + '0';
for(k = i+1; k < i+n; k++)
ans[k] = '0';//循环补0
ans[k] = '\0';
puts(ans);
}
else
printf("-1\n");
return 0;
}