Codeforces-260A_Adding Digits

**

A. Adding Digits

**
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

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 == 0 ;若不能得出结果,printf (“-1”);

刚开始我的思路是很简单的,即找到 a b 的最小公倍数然后往后加零就行了,能力不到,只能再想办法。

以下是ac代码思路:
首先我们可以知道,一个数的前x个非零数的 mod 等于整个数的 mod;(如 10380000 取 mod 可以直接从 1038 开始入手,因为k若对 1038 mod等于 0 ,则对 10380000 理所当然的 mod 等于 0);那么我们可以把思路稍微倒一下,找到 a 开头,且 mod 等于 0 的整数,这个整数后边无论跟多少个 0 ,均可以对 b mod 等于 0 ;

以下是ac代码实现:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cmath>
using namespace std;


int main()
{
    long long a,b,c;
    scanf ("%lld%lld%lld",&a,&b,&c);
    int flag=0;int i;//i放于for循环外是方便于下方判断打印的0的个数
    for (i=1;i<=c;i++)
    {
        a *= 10;
        if (a<1) {flag=0;break;}//此处特判防止运算爆出负数
        if (a % b != 0)
        {
            for (int j=1;j<=9;j++)
            {
                a++;
                if (j == 9 && a % b != 0) {a -= 9;break;}
                if (a % b == 0) {flag=1;break;}
            }
        }
        else flag=1;
        if (flag) break;
    }


    if (flag)
    {
        printf ("%lld",a);
        for (int j=1;j<=c-i;j++)
        {
            if (j==c-i) printf ("0\n");
            else printf ("0");
        }
    }
    else printf ("-1\n");
}

另附上CF 红名大神代码:

#include<stdio.h>
int a,b,n,j,R;
int main(){
scanf("%d%d%d",&a,&b,&n);
R=a%b;R*=10;
for(j=0;j<10;j++)if((R+j)%b==0)break;
printf("%d",j>9?-1:a*10+j);
if(j<=9)for(j=0;j<n-1;j++)printf("0");
}

//by ainta
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值