#P11059. [入门赛 #27] 数字 (Hard Ver.)

[入门赛 #27] 数字 (Hard Ver.)

Background

If I say I will not stop until I kiss you
Who can force me to settle
——Li Ronghao, "Bu Jiang Jiu"

In life, you cannot just “settle”. Whether you have made the best choice depends on each person.

Problem Description

You need to find an nn-digit number xx that satisfies the following two conditions:

    1. The remainder of (the sum of digits of xx) divided by pp is as small as possible.
    1. After condition 1 is satisfied, the value of xx is as small as possible.

Sum of digits: the total obtained by adding the digits in every position of a number. For example, the sum of digits of 123123 is 1+2+3=61+2+3=6.

Input Format

The input consists of one line with two integers n,pn, p.

Output Format

Output one integer, representing the answer to the problem above.

3 8
107
1 1
1
5 3
10002
2 7
16

Hint

Sample Explanation #1

Three-digit numbers include 100,101,,999100,101,\dots,999. Among them, the sum of digits of 107107 is 1+0+7=81+0+7=8, and the remainder of 88 divided by 88 is 00.

Constraints

For 30%30\% of the testdata, 1n181 \le n \le 18, 1p2001 \le p \le 200
For 100%100\% of the testdata, 1n1061 \le n \le 10^6, 1p1091 \le p \le 10^9

Translated by ChatGPT 5