CodeForces - 260A - Adding Digits(思维)

探讨如何在数字a后添加n位数,确保每步结果都能被b整除,若成功则输出最终数字,否则返回-1。通过判断是否能整除决定添加0或直接返回结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值