Codeforces: 260A. Adding Digits


The problem is from codeforces: http://www.codeforces.com/problemset/problem/260/A
260A Codeforces: 260A. Adding Digits algorithms beginner brute force codeforces implementation math programming languages python tricks

This problem looks difficult at the first glance because of the input ranges are large for the given three integers. Using bruteforce to print all possibilities/combination is difficult, but the problem states that only print one possibility is enough.

Therefore, it is only needed to loop ten possibilities (0 to 9) on the right of the number to check if it is divisible by number b. If yes, we just need to print a, followed by this digit and n – 1 times of zeros because this number is also divisible by b.

If none combination of the number a * 10 + x where is from 0 to 9, divisible by b, we just print -1.

#!/usr/bin/env python

a, b, n = map(int, raw_input().split())

a *= 10
for _ in xrange(10):
    if (a + _) % b == 0:
        print str(a + _) + "0" * (n - 1)
        exit()

print -1

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
248 words
Last Post: New site and Three pages
Next Post: Base62

The Permanent URL is: Codeforces: 260A. Adding Digits

Leave a Reply