1. 程式人生 > >CodeForces - 260A - Adding Digits(思維)

CodeForces - 260A - Adding Digits(思維)

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
題目連結

  • 題目大意:給出a,b,n三個整數,要求在a後面新增n位,每新增一位都要能夠被b整除,否則不存在,如果一直添加了n位都能夠被b整除的話,輸出最終新增完成的數字a。如果不存在輸出-1。

  • 我們首先判斷新增一位的時候,只有兩種情況,能被整除和不能。
    1.如果能的話,那麼我們直接在這個後面再新增n-1位0就可以了,因為我們做除法的那個式子,大家寫一下就明白了,前面如果除盡了,那麼就可以只看後面了,0是所有整數的倍數,那無疑是符合題目條件的。
    2.如果不能整除,那就直接返回-1。

#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1e5 + 5;
int a, b, n;

int main()
{
    while(~scanf("%d%d%d", &a, &b, &n))
    {
        int flag = 0, add;
        for(int i = 0; i < 10; i++)
        {
            int ans = a * 10 + i;
            if(ans % b == 0)
            {
                flag = 1;
                add = i;
                break ;
            }
        }
        if(flag)
        {
            printf("%d%d", a, add);
            for(int i = 1; i < n; i++)
                printf("0");
            printf("\n");
        }
        else
            printf("-1\n");
    }
    return 0;
}