実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 150 点
問題文
あなたはコワスギ銀行の通帳を持っています。コワスギ銀行の預金通帳には、引き出し額に応じて手数料が変わるという怖すぎる性質があります。
コワスギ銀行の通帳からお金を引き出すには、1\,000 円単位で引き出し額を指定したうえで、引き出し額 1\,000 円あたり C 円の手数料を残高から別途支払う必要があります。銀行の残高が 0 円未満になる引き出しを行うことはできません。
あなたが持っているコワスギ銀行の通帳の残高が X 円のとき、そこから最大で何円を引き出せますか?
制約
- 1 \leq X \leq 10^7
- 1 \leq C \leq 999
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
X C
出力
答えを出力せよ。
入力例 1
650000 8
出力例 1
644000
644\,000 円を引き出すために必要な手数料は 644\,000 \times \displaystyle\frac{8}{1000} = 5\,152 円になるので、644\,000 + 5\,152 \leq 650\,000 より、644\,000 円は引き出すことができます。
一方で、645\,000 円引き出すために必要な手数料は 645\,000 \times \displaystyle\frac{8}{1000} = 5\,160 円になるので、645\,000 + 5\,160 > 650\,000 より、645\,000 円は引き出すことができません。
入力例 2
1003 4
出力例 2
0
まったくお金が引き出せないこともありえます。
入力例 3
10000000 24
出力例 3
9765000
Score : 150 points
Problem Statement
You have a bankbook from The Terrifying Bank. The deposit passbook of the bank has a terrifyingly scary property that the commission fee changes according to the withdrawal amount.
To withdraw money from the passbook, you need to specify the withdrawal amount in units of 1\,000 yen, and pay a commission fee of C yen per 1\,000 yen of withdrawal amount separately from the balance. Withdrawals are not allowed if they would leave the balance below 0 yen.
When the balance of your passbook from the bank is X yen, what is the maximum amount of money you can withdraw from it?
Constraints
- 1 \leq X \leq 10^7
- 1 \leq C \leq 999
- All input values are integers.
Input
The input is given from Standard Input in the following format:
X C
Output
Output the answer.
Sample Input 1
650000 8
Sample Output 1
644000
The commission fee required to withdraw 644\,000 yen is 644\,000 \times \displaystyle\frac{8}{1000} = 5\,152 yen, so since 644\,000 + 5\,152 \leq 650\,000, you can withdraw 644\,000 yen.
On the other hand, the commission fee required to withdraw 645\,000 yen is 645\,000 \times \displaystyle\frac{8}{1000} = 5\,160 yen, so since 645\,000 + 5\,160 > 650\,000, you cannot withdraw 645\,000 yen.
Sample Input 2
1003 4
Sample Output 2
0
It is possible that no money can be withdrawn at all.
Sample Input 3
10000000 24
Sample Output 3
9765000
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
N + 1 個の部屋が一列に並んでおり、順に 0, 1, \ldots, N の番号が付けられています。
部屋の間には N 個のドアがあり、1, 2, \ldots, N の番号が付けられています。ドア i は部屋 i - 1 と部屋 i の間にあります。
各ドアについて鍵の状態を表す値 L_i が与えられ、L_i = 0 のときドア i の鍵は開いており、L_i = 1 のときドア i の鍵は閉まっています。
2 人の人がおり、1 人は部屋 0 に、もう 1 人は部屋 N にいます。それぞれの人は、ドア i の鍵が開いているときに限り、部屋 i - 1 と部屋 i の間を移動することができます。
このとき、2 人のいずれも到達できない部屋の個数を求めてください。
制約
- 2 \leq N \leq 100
- L_i \in \lbrace 0, 1 \rbrace
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N L_1 L_2 \ldots L_N
出力
答えを出力せよ。
入力例 1
5 0 1 0 0 1
出力例 1
3
2 人のいずれも到達できない部屋は部屋 2, 3, 4 の 3 つです。
入力例 2
3 1 0 1
出力例 2
2
入力例 3
8 0 0 1 1 0 1 0 0
出力例 3
3
Score : 200 points
Problem Statement
There are N + 1 rooms arranged in a line, numbered 0, 1, \ldots, N in order.
Between the rooms, there are N doors numbered 1, 2, \ldots, N. Door i is between rooms i - 1 and i.
For each door, a value L_i representing the lock state is given. When L_i = 0, door i is unlocked, and when L_i = 1, door i is locked.
There are two people, one in room 0 and the other in room N. Each person can move between rooms i - 1 and i only when door i is unlocked.
Find the number of rooms that neither of the two people can reach.
Constraints
- 2 \leq N \leq 100
- L_i \in \lbrace 0, 1 \rbrace
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N L_1 L_2 \ldots L_N
Output
Output the answer.
Sample Input 1
5 0 1 0 0 1
Sample Output 1
3
The rooms that neither of the two people can reach are rooms 2, 3, 4, which is 3 rooms.
Sample Input 2
3 1 0 1
Sample Output 2
2
Sample Input 3
8 0 0 1 1 0 1 0 0
Sample Output 3
3
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
N + 1 個の部屋が一列に並んでおり、順に 0, 1, \ldots, N の番号が付けられています。
部屋の間には N 個のドアがあり、1, 2, \ldots, N の番号が付けられています。ドア i は部屋 i - 1 と部屋 i の間にあります。
各ドアについて鍵の状態を表す値 L_i が与えられ、L_i = 0 のときドア i の鍵は開いており、L_i = 1 のときドア i の鍵は閉まっています。
高橋君ははじめ部屋 R におり、ドア i の鍵が開いているときに限り、部屋 i - 1 と部屋 i の間を移動することができます。また、高橋君は部屋 i - 1 または部屋 i にいるときに限り、ドア i の鍵に対して 開閉操作 を行うことができます。ドア i の鍵に対して開閉操作を行ったとき、その鍵が開いているときは閉まり、閉まっているときは開きます。
すべてのドアの鍵が閉まった状態にするために行う鍵の開閉操作の回数として考えられる最小値を求めてください。
制約
- 2 \leq N \leq 2 \times 10^5
- 0 \leq R \leq N
- L_i \in \lbrace 0, 1 \rbrace
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N R L_1 L_2 \ldots L_N
出力
答えを出力せよ。
入力例 1
6 3 1 0 0 1 0 0
出力例 1
6
高橋君は以下のように行動することで 6 回の開閉操作ですべてのドアの鍵が閉まった状態にすることができます。
- 部屋 2 に移動する。
- ドア 2 に対して開閉操作を行い、ドア 2 の鍵を閉める。
- 部屋 3 に移動する。
- ドア 4 に対して開閉操作を行い、ドア 4 の鍵を開ける。
- ドア 3 に対して開閉操作を行い、ドア 3 の鍵を閉める。
- 部屋 4 に移動する。
- ドア 4 に対して開閉操作を行い、ドア 4 の鍵を閉める。
- 部屋 5 に移動する。
- ドア 5 に対して開閉操作を行い、ドア 5 の鍵を閉める。
- ドア 6 に対して開閉操作を行い、ドア 6 の鍵を閉める。
入力例 2
2 1 0 0
出力例 2
2
入力例 3
8 2 0 1 0 0 1 0 1 1
出力例 3
8
Score : 300 points
Problem Statement
There are N + 1 rooms arranged in a line, numbered 0, 1, \ldots, N in order.
Between the rooms, there are N doors numbered 1, 2, \ldots, N. Door i is between rooms i - 1 and i.
For each door, a value L_i representing the lock state is given. When L_i = 0, door i is unlocked, and when L_i = 1, door i is locked.
Takahashi is initially in room R, and can move between rooms i - 1 and i only when door i is unlocked. Also, he can perform a switching operation on door i only when he is in room i - 1 or room i. When a switching operation is performed on door i, if the door is unlocked, it becomes locked, and if it is locked, it becomes unlocked.
Find the minimum number of switching operations needed to make all doors locked.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 0 \leq R \leq N
- L_i \in \lbrace 0, 1 \rbrace
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N R L_1 L_2 \ldots L_N
Output
Output the answer.
Sample Input 1
6 3 1 0 0 1 0 0
Sample Output 1
6
Takahashi can make all doors locked with six switching operations by acting as follows:
- Move to room 2.
- Perform a switching operation on door 2 to lock door 2.
- Move to room 3.
- Perform a switching operation on door 4 to unlock door 4.
- Perform a switching operation on door 3 to lock door 3.
- Move to room 4.
- Perform a switching operation on door 4 to lock door 4.
- Move to room 5.
- Perform a switching operation on door 5 to lock door 5.
- Perform a switching operation on door 6 to lock door 6.
Sample Input 2
2 1 0 0
Sample Output 2
2
Sample Input 3
8 2 0 1 0 0 1 0 1 1
Sample Output 3
8
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
同時に最大 K 人の客を入れられるレストラン店があります。この店の前には脇道があり、ここで 1 つの待ち行列を管理しています。
時刻 0 の時点で店内に客はおらず、待ち行列も空です。
今日は N 個の団体客が来る予定であり、来訪が早い順に 1 から N までの番号が付けられています。団体客 i の人数は C_i 人で、時刻 A_i に待ち行列の末尾に加わります。また、この団体客は入店してから B_i 単位時間後に退店します。
それぞれの団体客は、以下の 2 条件が同時に満たされた最も早い時刻に、待ち行列を離れて入店します。
- その団体客は、待ち行列の先頭にいる。つまりその団体客は、その時点で待ち行列にいる団体客のうち、最も早く待ち行列に加わっている。
- その団体客と、店内にいるすべての団体客 (ちょうどその時刻に入店したぶんを含み、退店したぶんを除く) の人数を合算すると、K 人以下になる。
それぞれの団体客が入店する時刻を求めてください。
制約
- 1 \leq N \leq 3 \times 10^5
- 1 \leq K \leq 10^7
- 1 \leq A_i, B_i \leq 10^7 (1 \leq i \leq N)
- A_1 < \dots < A_N
- 1 \leq C_i \leq K (1 \leq i \leq N)
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 B_1 C_1 \vdots A_N B_N C_N
出力
N 行出力せよ。i 行目 (1 \leq i \leq N) には、団体客 i が入店する時刻を整数で出力せよ。
入力例 1
4 10 30 300 3 60 45 4 90 45 5 120 45 2
出力例 1
30 60 105 120
各団体客の入退店は以下の流れで進みます。
- 時刻 30 に、団体客 1 が待ち行列に加わった直後にそのまま入店し、店内の客は 3 名になる。
- 時刻 60 に、団体客 2 が待ち行列に加わった直後にそのまま入店し、店内の客は 7 名になる。
- 時刻 90 に、団体客 3 が待ち行列に加わる。
- 時刻 105 に、団体客 2 が退店し、店内の客は 3 名になる。その直後、団体客 3 が入店し、店内の客は 8 名になる。
- 時刻 120 に、団体客 4 が待ち行列に加わった直後にそのまま入店し、店内の客は 10 名になる。
- 時刻 150 に、団体客 3 が退店し、店内の客は 5 名になる。
- 時刻 165 に、団体客 4 が退店し、店内の客は 3 名になる。
- 時刻 330 に、団体客 1 が退店し、店内の客は 0 名になる。
入力例 2
4 10 30 300 10 60 45 2 90 45 3 120 45 4
出力例 2
30 330 330 330
各団体客の入退店は以下の流れで進みます。
- 時刻 30 に、団体客 1 が待ち行列に加わった直後にそのまま入店し、店内の客は 10 名になる。
- 時刻 60 に、団体客 2 が待ち行列に加わる。
- 時刻 90 に、団体客 3 が待ち行列に加わる。
- 時刻 120 に、団体客 4 が待ち行列に加わる。
- 時刻 330 に、団体客 1 が退店し、店内の客は 0 名になる。その直後、団体客 2,3,4 が入店し、店内の客は 9 名になる。
- 時刻 375 に、団体客 2,3,4 が退店し、店内の客は 0 名になる。
入力例 3
10 24 279290 9485601 1 1094410 8022270 4 1314176 7214745 5 1897674 5924694 10 1921802 5769841 4 2506394 2765234 2 2558629 2727489 9 2681289 4061363 5 3022540 2291905 3 4407692 1313036 8
出力例 3
279290 1094410 1314176 1897674 1921802 7691643 7822368 8528921 8528921 10549857
Score : 400 points
Problem Statement
There is a restaurant that can accommodate at most K customers simultaneously. In front of this restaurant, there is a side street where one queue is managed.
At time 0, there are no customers in the restaurant, and the queue is also empty.
Today, N groups of customers are scheduled to come, and they are numbered from 1 to N in the order of their arrival. Group i consists of C_i people, joins the end of the queue at time A_i, and leaves the restaurant B_i time units after entering.
Each group enters the restaurant by leaving the queue at the earliest time when both of the following two conditions are satisfied simultaneously:
- The group is at the front of the queue. In other words, the group is the earliest to have joined among those still in the queue at that point.
- When the number of people in that group and all groups currently in the restaurant (including those entering at exactly that time and excluding those leaving) are combined, there are K or fewer people.
Find the time when each group enters the restaurant.
Constraints
- 1 \leq N \leq 3 \times 10^5
- 1 \leq K \leq 10^7
- 1 \leq A_i, B_i \leq 10^7 (1 \leq i \leq N)
- A_1 < \dots < A_N
- 1 \leq C_i \leq K (1 \leq i \leq N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K A_1 B_1 C_1 \vdots A_N B_N C_N
Output
Output N lines. The i-th line (1 \leq i \leq N) should contain the time when group i enters the restaurant as an integer.
Sample Input 1
4 10 30 300 3 60 45 4 90 45 5 120 45 2
Sample Output 1
30 60 105 120
The entry and exit of each group proceed as follows:
- At time 30, group 1 joins the queue and immediately enters, making the number of customers in the restaurant 3.
- At time 60, group 2 joins the queue and immediately enters, making the number of customers in the restaurant 7.
- At time 90, group 3 joins the queue.
- At time 105, group 2 leaves, making the number of customers in the restaurant 3. Immediately after, group 3 enters, making the number of customers in the restaurant 8.
- At time 120, group 4 joins the queue and immediately enters, making the number of customers in the restaurant 10.
- At time 150, group 3 leaves, making the number of customers in the restaurant 5.
- At time 165, group 4 leaves, making the number of customers in the restaurant 3.
- At time 330, group 1 leaves, making the number of customers in the restaurant 0.
Sample Input 2
4 10 30 300 10 60 45 2 90 45 3 120 45 4
Sample Output 2
30 330 330 330
The entry and exit of each group proceed as follows:
- At time 30, group 1 joins the queue and immediately enters, making the number of customers in the restaurant 10.
- At time 60, group 2 joins the queue.
- At time 90, group 3 joins the queue.
- At time 120, group 4 joins the queue.
- At time 330, group 1 leaves, making the number of customers in the restaurant 0. Immediately after, groups 2,3,4 enter, making the number of customers in the restaurant 9.
- At time 375, groups 2,3,4 leave, making the number of customers in the restaurant 0.
Sample Input 3
10 24 279290 9485601 1 1094410 8022270 4 1314176 7214745 5 1897674 5924694 10 1921802 5769841 4 2506394 2765234 2 2558629 2727489 9 2681289 4061363 5 3022540 2291905 3 4407692 1313036 8
Sample Output 3
279290 1094410 1314176 1897674 1921802 7691643 7822368 8528921 8528921 10549857
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 475 点
問題文
長さ N の整数列 A = (A_1, A_2, \ldots, A_N) が与えられます。
Q 個のクエリが与えられるので、それぞれについて答えを求めてください。
i 番目のクエリでは、整数 L_i, R_i が与えられるので、 \displaystyle\sum_{l = L_i}^{R_i}\sum_{r = l}^{R_i}\sum_{j = l}^{r} A_j の値を答えとして求めてください。
制約
- 1 \leq N, Q \leq 3 \times 10^5
- 1 \leq A_i \leq 100
- 1 \leq L_i \leq R_i \leq N
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N Q A_1 A_2 \ldots A_N L_1 R_1 L_2 R_2 \vdots L_Q R_Q
出力
Q 行出力せよ。i 行目には、i 番目のクエリに対する答えを出力せよ。
入力例 1
5 4 2 1 3 3 1 2 4 1 4 1 5 3 3
出力例 1
24 44 74 3
1 番目のクエリについて説明します。
求めるべき値は \displaystyle\sum_{l = 2}^{4}\sum_{r = l}^{4}\sum_{j = l}^{r} A_j です。
-
l = 2, r = 2 のとき \displaystyle\sum_{j = l}^{r} A_j = 1 です。
-
l = 2, r = 3 のとき \displaystyle\sum_{j = l}^{r} A_j = 4 です。
-
l = 2, r = 4 のとき \displaystyle\sum_{j = l}^{r} A_j = 7 です。
-
l = 3, r = 3 のとき \displaystyle\sum_{j = l}^{r} A_j = 3 です。
-
l = 3, r = 4 のとき \displaystyle\sum_{j = l}^{r} A_j = 6 です。
-
l = 4, r = 4 のとき \displaystyle\sum_{j = l}^{r} A_j = 3 です。
以上より、求めるべき値は (1 + 4 + 7) + (3 + 6) + 3 = 24 です。
Score : 475 points
Problem Statement
You are given an integer sequence A = (A_1, A_2, \ldots, A_N) of length N.
Q queries are given, so find the answer for each.
In the i-th query, integers L_i and R_i are given, so find \displaystyle\sum_{l = L_i}^{R_i}\sum_{r = l}^{R_i}\sum_{j = l}^{r} A_j as the answer.
Constraints
- 1 \leq N, Q \leq 3 \times 10^5
- 1 \leq A_i \leq 100
- 1 \leq L_i \leq R_i \leq N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N Q A_1 A_2 \ldots A_N L_1 R_1 L_2 R_2 \vdots L_Q R_Q
Output
Output Q lines. The i-th line should contain the answer to the i-th query.
Sample Input 1
5 4 2 1 3 3 1 2 4 1 4 1 5 3 3
Sample Output 1
24 44 74 3
We explain the first query.
The value to be calculated is \displaystyle\sum_{l = 2}^{4}\sum_{r = l}^{4}\sum_{j = l}^{r} A_j.
-
When l = 2, r = 2, \displaystyle\sum_{j = l}^{r} A_j = 1.
-
When l = 2, r = 3, \displaystyle\sum_{j = l}^{r} A_j = 4.
-
When l = 2, r = 4, \displaystyle\sum_{j = l}^{r} A_j = 7.
-
When l = 3, r = 3, \displaystyle\sum_{j = l}^{r} A_j = 3.
-
When l = 3, r = 4, \displaystyle\sum_{j = l}^{r} A_j = 6.
-
When l = 4, r = 4, \displaystyle\sum_{j = l}^{r} A_j = 3.
From the above, the value to be calculated is (1 + 4 + 7) + (3 + 6) + 3 = 24.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 525 点
問題文
AtCoder 島には N 種類のセミが生息しています。種類 i のセミ (1 \leq i \leq N) は A_i の倍数の年にのみ大量発生します。
1 年から Y 年までの Y 年間のうち、ちょうど M 種類のセミが大量発生する年が何回あるかを求めてください。
制約
- 1 \leq M \leq N \leq 20
- 1 \leq Y \leq 10^{18}
- 1 \leq A_i \leq 10^{18} (1 \leq i \leq N)
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M Y A_1 \cdots A_N
出力
答えを出力せよ。
入力例 1
3 2 16 4 2 3
出力例 1
4
1 年から 16 年までのうち、各種類のセミが大量発生するのは以下の年です。
- 種類 1 のセミは 4,8,12,16 年に大量発生する。
- 種類 2 のセミは 2,4,6,8,10,12,14,16 年に大量発生する。
- 種類 3 のセミは 3,6,9,12,15 年に大量発生する。
1 年から 16 年までのうち、ちょうど 2 種類のセミが大量発生するのは 4,6,8,16 年の 4 回です。
入力例 2
2 1 122333444422333 1429 73651
出力例 2
87266392324
答えが 32bit 整数に収まらないことがあります。
入力例 3
20 3 832725971730072237 19639596380058 49098990950145 32732660633430 114564312217005 68738587330203 45825724886802 252041486877411 180029633483865 108017780090319 72011853393546 468077047058049 297867211764213 212762294117295 127657376470377 85104917646918 723391799998803 612100753845141 389518661537817 278227615384155 166936569230493
出力例 3
24231
入力される値が 32bit 整数に収まらないことがあります。
Score : 525 points
Problem Statement
AtCoder Island has N species of cicadas. Cicadas of species i (1 \leq i \leq N) have mass outbreaks only in years that are multiples of A_i.
Among the Y years from year 1 to year Y, find how many years have mass outbreaks of exactly M species of cicadas.
Constraints
- 1 \leq M \leq N \leq 20
- 1 \leq Y \leq 10^{18}
- 1 \leq A_i \leq 10^{18} (1 \leq i \leq N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M Y A_1 \cdots A_N
Output
Output the answer.
Sample Input 1
3 2 16 4 2 3
Sample Output 1
4
From years 1 to 16, each species of cicada has mass outbreaks in the following years:
- Species 1 cicadas have mass outbreaks in years 4,8,12,16.
- Species 2 cicadas have mass outbreaks in years 2,4,6,8,10,12,14,16.
- Species 3 cicadas have mass outbreaks in years 3,6,9,12,15.
From years 1 to 16, exactly two species of cicadas have mass outbreaks four times in years 4,6,8,16.
Sample Input 2
2 1 122333444422333 1429 73651
Sample Output 2
87266392324
The answer may not fit in a 32-bit integer.
Sample Input 3
20 3 832725971730072237 19639596380058 49098990950145 32732660633430 114564312217005 68738587330203 45825724886802 252041486877411 180029633483865 108017780090319 72011853393546 468077047058049 297867211764213 212762294117295 127657376470377 85104917646918 723391799998803 612100753845141 389518661537817 278227615384155 166936569230493
Sample Output 3
24231
Input values may not fit in a 32-bit integer.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 600 点
問題文
以下の 2 つの条件を満たす正の整数 n としてあり得る最小値を求めてください。
- n は K の倍数である。
- n の十進法での表記には、部分文字列として S が含まれる。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
部分文字列とは
ある文字列 S の部分文字列とは、S の先頭から 0 文字以上、末尾から 0 文字以上削除して得られる文字列のことをいいます。 例えば、ab
や b
は abc
の部分文字列ですが、ac
や cba
は abc
の部分文字列ではありません。
制約
- T は整数。
- 1 \leq T \leq 200
- K は整数。
- 1 \leq K \leq 10^9
- S は数字 (
0
-9
) からなる文字列。 - S の先頭の文字は
0
でない。 - 1 \leq |S| \leq 5 \times 10^5
- すべてのテストケースの |S| の総和は 5 \times 10^5 以下である。
入力
入力は以下の形式で標準入力から与えられる。
T \textrm{case}_1 \textrm{case}_2 \vdots \textrm{case}_T
\textrm{case}_i は i 番目のテストケースを表す。各テストケースは以下の形式で与えられる。
K S
出力
T 行出力せよ。i 行目 (1 \leq i \leq T) には i 番目のテストケースに対する答えを出力せよ。
入力例 1
4 271 414 15 23 155521 1000 920950937 999999999999999999999
出力例 1
34146 1230 100000003 1000000999999999999999999999
1 番目のテストケースにおいて、271 の倍数である正の整数のうち、十進法での表記に部分文字列として 414
を含むものの最小値は 34146 です。
2 番目のテストケースにおいて、15 の倍数である正の整数のうち、十進法での表記に部分文字列として 23
を含むものの最小値は 1230 です。
3 番目のテストケースにおいて、155521 の倍数である正の整数のうち、十進法での表記に部分文字列として 1000
を含むものの最小値は 100000003 です。
4 番目のテストケースにおいて、920950937 の倍数である正の整数のうち、十進法での表記に部分文字列として 999999999999999999999
を含むものの最小値は 1000000999999999999999999999 です。
Score : 600 points
Problem Statement
Find the minimum possible value of a positive integer n that satisfies the following two conditions:
- n is a multiple of K.
- The decimal representation of n contains S as a substring.
T test cases are given, so find the answer for each.
What is a substring?
A substring of a string S is a string obtained by deleting zero or more characters from the beginning and zero or more characters from the end of S. For example,ab
and b
are substrings of abc
, but ac
and cba
are not substrings of abc
.
Constraints
- T is an integer.
- 1 \leq T \leq 200
- K is an integer.
- 1 \leq K \leq 10^9
- S is a string consisting of digits (
0
-9
). - The first character of S is not
0
. - 1 \leq |S| \leq 5 \times 10^5
- For each input file, the sum of |S| over all test cases is at most 5 \times 10^5.
Input
The input is given from Standard Input in the following format:
T \textrm{case}_1 \textrm{case}_2 \vdots \textrm{case}_T
\textrm{case}_i represents the i-th test case. Each test case is given in the following format:
K S
Output
Output T lines. The i-th line (1 \leq i \leq T) should contain the answer to the i-th test case.
Sample Input 1
4 271 414 15 23 155521 1000 920950937 999999999999999999999
Sample Output 1
34146 1230 100000003 1000000999999999999999999999
In the first test case, among positive integers that are multiples of 271, the minimum one whose decimal representation contains 414
as a substring is 34146.
In the second test case, among positive integers that are multiples of 15, the minimum one whose decimal representation contains 23
as a substring is 1230.
In the third test case, among positive integers that are multiples of 155521, the minimum one whose decimal representation contains 1000
as a substring is 100000003.
In the fourth test case, among positive integers that are multiples of 920950937, the minimum one whose decimal representation contains 999999999999999999999
as a substring is 1000000999999999999999999999.