Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
この問題は G 問題の簡易版です。
3 個のリールからなるスロットがあります。
i 番目のリールの配列は文字列 S_i によって表されます。ここで、S_i は数字のみからなる長さ M の文字列です。
それぞれのリールには対応するボタンがついています。高橋君は各非負整数 t について、スロットが回り始めてからちょうど t 秒後にボタンを 1 つ選んで押す、または何もしないことができます。
スロットが回り始めてから t 秒後に i 番目のリールに対応するボタンを押すと、i 番目のリールは S_i の (t \bmod M)+1 文字目を表示して止まります。
ただし、t \bmod M で t を M で割ったあまりを表します。
高橋君は全てのリールを止めた上で、表示されている文字が全て同じであるようにしたいです。
高橋君が目標を達成できるように全てのリールを止めるまでに、スロットが回り始めてから最小で何秒かかるかを求めてください。
そのようなことが不可能であればそのことを報告してください。
制約
- 1 \leq M \leq 100
- M は整数
- S_i は数字のみからなる長さ M の文字列
入力
入力は以下の形式で標準入力から与えられる。
M S_1 S_2 S_3
出力
全てのリールを止めた上で、表示されている文字が全て同じであるようにすることができないなら -1 を出力せよ。
できるなら、スロットが回り始めてからそのような状態にするまでに最小で何秒かかるか出力せよ。
入力例 1
10 1937458062 8124690357 2385760149
出力例 1
6
高橋君は次のようにそれぞれのリールを止めることでスロットが回り始めてから 6 秒後にリールに表示される文字を 8 で揃えることができます。
- スロットの回転開始から 0 秒後に 2 番目のリールに対応するボタンを押します。2 番目のリールは S_2 の (0 \bmod 10)+1=1 文字目である
8を表示して止まります。 - スロットの回転開始から 2 秒後に 3 番目のリールに対応するボタンを押します。3 番目のリールは S_3 の (2 \bmod 10)+1=3 文字目である
8を表示して止まります。 - スロットの回転開始から 6 秒後に 1 番目のリールに対応するボタンを押します。1 番目のリールは S_1 の (6 \bmod 10)+1=7 文字目である
8を表示して止まります。
5 秒以下で全てのリールに表示されている文字を揃える方法はないため、6 を出力します。
入力例 2
20 01234567890123456789 01234567890123456789 01234567890123456789
出力例 2
20
全てのリールを止めた上で、表示されている文字を揃える必要がある事に注意してください。
入力例 3
5 11111 22222 33333
出力例 3
-1
表示されている文字が全て同じであるようにリールを止めることはできません。
このとき -1 を出力してください。
Score : 300 points
Problem Statement
This problem is an easier version of Problem G.
There is a slot machine with three reels.
The arrangement of symbols on the i-th reel is represented by the string S_i. Here, S_i is a string of length M consisting of digits.
Each reel has a corresponding button. For each non-negative integer t, Takahashi can either choose and press one button or do nothing exactly t seconds after the reels start spinning.
If he presses the button corresponding to the i-th reel exactly t seconds after the reels start spinning, the i-th reel will stop and display the ((t \bmod M)+1)-th character of S_i.
Here, t \bmod M denotes the remainder when t is divided by M.
Takahashi wants to stop all the reels so that all the displayed characters are the same.
Find the minimum possible number of seconds from the start of the spin until all the reels are stopped so that his goal is achieved.
If this is impossible, report that fact.
Constraints
- 1 \leq M \leq 100
- M is an integer.
- S_i is a string of length M consisting of digits.
Input
The input is given from Standard Input in the following format:
M S_1 S_2 S_3
Output
If it is impossible to stop all the reels so that all the displayed characters are the same, print -1.
Otherwise, print the minimum possible number of seconds from the start of the spin until such a state is achieved.
Sample Input 1
10 1937458062 8124690357 2385760149
Sample Output 1
6
Takahashi can stop each reel as follows so that 6 seconds after the reels start spinning, all the reels display 8.
- Press the button corresponding to the second reel 0 seconds after the reels start spinning. The second reel stops and displays
8, the ((0 \bmod 10)+1=1)-st character of S_2. - Press the button corresponding to the third reel 2 seconds after the reels start spinning. The third reel stops and displays
8, the ((2 \bmod 10)+1=3)-rd character of S_3. - Press the button corresponding to the first reel 6 seconds after the reels start spinning. The first reel stops and displays
8, the ((6 \bmod 10)+1=7)-th character of S_1.
There is no way to make the reels display the same character in 5 or fewer seconds, so print 6.
Sample Input 2
20 01234567890123456789 01234567890123456789 01234567890123456789
Sample Output 2
20
Note that he must stop all the reels and make them display the same character.
Sample Input 3
5 11111 22222 33333
Sample Output 3
-1
It is impossible to stop the reels so that all the displayed characters are the same.
In this case, print -1.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
N 個の商品があります。i = 1, 2, \ldots, N について、i 番目の商品の値段は A_i 円です。
高橋君は K 枚のクーポンを持っています。
1 枚のクーポンは 1 つの商品に対して使用することができ、1 つの商品に対してはクーポンを何枚でも( 0 枚でもよい)使用することができます。
値段が a 円の商品に対して k 枚のクーポンを使用すると、その商品を \max\lbrace a - kX, 0\rbrace 円で買うことができます。
高橋君がすべての商品を買うために支払う合計金額の最小値を出力してください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K, X \leq 10^9
- 1 \leq A_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K X A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
5 4 7 8 3 10 5 13
出力例 1
12
1 番目の商品に対してクーポン 1 枚、3 番目の商品に対してクーポン 1 枚、5 番目の商品に対してクーポン 2 枚を使用すると、
- 1 番目の商品を \max\lbrace A_1-X, 0 \rbrace = 1 円で買うことができ、
- 2 番目の商品を \max\lbrace A_2, 0 \rbrace = 3 円で買うことができ、
- 3 番目の商品を \max\lbrace A_3-X, 0 \rbrace = 3 円で買うことができ、
- 4 番目の商品を \max\lbrace A_4, 0 \rbrace = 5 円で買うことができ、
- 5 番目の商品を \max\lbrace A_5-2X, 0 \rbrace = 0 円で買うことができます。
よって、すべての商品を 1 + 3 + 3 + 5 + 0 = 12 円で買うことができ、これが最小です。
入力例 2
5 100 7 8 3 10 5 13
出力例 2
0
入力例 3
20 815 60 2066 3193 2325 4030 3725 1669 1969 763 1653 159 5311 5341 4671 2374 4513 285 810 742 2981 202
出力例 3
112
Score : 300 points
Problem Statement
There are N items in a shop. For each i = 1, 2, \ldots, N, the price of the i-th item is A_i yen (the currency of Japan).
Takahashi has K coupons.
Each coupon can be used on one item. You can use any number of coupons, possibly zero, on the same item. Using k coupons on an item with a price of a yen allows you to buy it for \max\lbrace a - kX, 0\rbrace yen.
Print the minimum amount of money Takahashi needs to buy all the items.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K, X \leq 10^9
- 1 \leq A_i \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K X A_1 A_2 \ldots A_N
Output
Print the answer.
Sample Input 1
5 4 7 8 3 10 5 13
Sample Output 1
12
By using 1 coupon on the 1-st item, 1 coupon on the 3-rd item, and 2 coupons on the 5-th item, Takahashi can:
- buy the 1-st item for \max\lbrace A_1-X, 0 \rbrace = 1 yen,
- buy the 2-nd item for \max\lbrace A_2, 0 \rbrace = 3 yen,
- buy the 3-rd item for \max\lbrace A_3-X, 0 \rbrace = 3 yen,
- buy the 4-th item for \max\lbrace A_4, 0 \rbrace = 5 yen,
- buy the 5-th item for \max\lbrace A_5-2X, 0 \rbrace = 0 yen,
for a total of 1 + 3 + 3 + 5 + 0 = 12 yen, which is the minimum possible.
Sample Input 2
5 100 7 8 3 10 5 13
Sample Output 2
0
Sample Input 3
20 815 60 2066 3193 2325 4030 3725 1669 1969 763 1653 159 5311 5341 4671 2374 4513 285 810 742 2981 202
Sample Output 3
112
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
一方の端が赤に塗られており、もう一方の端が青に塗られているロープが N 本あります。ロープには 1 から N までの番号がつけられています。
これからロープの端を結ぶ操作を M 回行います。i 回目の操作ではロープ A_i の色 B_i の端とロープ C_i の色 D_i の端を結びます。ただし、色 R は赤を意味し、色 B は青を意味します。各ロープについて、同じ色の端が複数回結ばれることはありません。
すべての操作を終えた後に、ひとつながりになっているロープの組について環状になっているものとそうでないものの個数を出力してください。
ただし、ひとつながりになっているロープの組 \lbrace v_0, v_1, \ldots, v_{x-1} \rbrace が環状になっているとは、v の要素の順序を適切に入れ替えることで各 0 \leq i < x についてロープ v_i とロープ v_{(i+1) \bmod x} が結ばれているようにできることをいいます。
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq A_i, C_i \leq N
- (A_i, B_i) \neq (A_j, B_j), (C_i, D_i) \neq (C_j, D_j) (i \neq j)
- (A_i, B_i) \neq (C_j, D_j)
- N, M, A_i, C_i は整数
- B_i, D_i は
RかBのいずれか
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 C_1 D_1 A_2 B_2 C_2 D_2 \vdots A_M B_M C_M D_M
出力
ひとつながりになっているロープの組について、環状になっているものの個数を X、そうでないものの個数を Y として X と Y をこの順に空白区切りで出力せよ。
入力例 1
5 3 3 R 5 B 5 R 3 B 4 R 2 B
出力例 1
1 2
ひとつながりになっているロープの組は \lbrace 1 \rbrace、\lbrace 2,4 \rbrace、\lbrace 3,5 \rbrace の 3 つです。
ロープ \lbrace 3,5 \rbrace の組は環状になっており、ロープ \lbrace 1 \rbrace と \lbrace 2,4 \rbrace の組は環状になっていません。したがって、X = 1, Y = 2 です。
入力例 2
7 0
出力例 2
0 7
入力例 3
7 6 5 R 3 R 7 R 4 R 4 B 1 R 2 R 3 B 2 B 5 B 1 B 7 B
出力例 3
2 1
Score : 400 points
Problem Statement
There are N ropes numbered 1 through N. One end of each rope is painted red, and the other is painted blue.
You are going to perform M operations of tying ropes. In the i-th operation, you tie the end of rope A_i painted B_i with the end of rope C_i painted D_i, where R means red and B means blue. For each rope, an end with the same color is not tied multiple times.
Find the number of groups of connected ropes that form cycles, and the number of those that do not, after all the operations.
Here, a group of connected ropes \lbrace v_0, v_1, \ldots, v_{x-1} \rbrace is said to form a cycle if one can rearrange the elements of v so that, for each 0 \leq i < x, rope v_i is tied to rope v_{(i+1) \bmod x}.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq A_i, C_i \leq N
- (A_i, B_i) \neq (A_j, B_j), (C_i, D_i) \neq (C_j, D_j) (i \neq j)
- (A_i, B_i) \neq (C_j, D_j)
- N, M, A_i, and C_i are integers.
- B_i is
RorB, and so is D_i.
Input
The input is given from Standard Input in the following format:
N M A_1 B_1 C_1 D_1 A_2 B_2 C_2 D_2 \vdots A_M B_M C_M D_M
Output
Print X and Y in this order, separated by a space, where X is the number of groups of connected ropes that form cycles, and Y is the number of those that do not.
Sample Input 1
5 3 3 R 5 B 5 R 3 B 4 R 2 B
Sample Output 1
1 2
There are three groups of connected ropes: \lbrace 1 \rbrace, \lbrace 2,4 \rbrace, and \lbrace 3,5 \rbrace.
The group of ropes \lbrace 3,5 \rbrace forms a cycle, while the groups of rope \lbrace 1 \rbrace and ropes \lbrace 2,4 \rbrace do not. Thus, X = 1 and Y = 2.
Sample Input 2
7 0
Sample Output 2
0 7
Sample Input 3
7 6 5 R 3 R 7 R 4 R 4 B 1 R 2 R 3 B 2 B 5 B 1 B 7 B
Sample Output 3
2 1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
1, \dots, N と番号づけられた N 個の都市と、1, \dots, M と番号づけられた M 本の道があります。
全ての道は一方通行であり、道 i \, (1 \leq i \leq M) を通ると、都市 A_i から都市 B_i へ移動することができます。また、道 i の長さは C_i です。
1 以上 M 以下の整数からなる長さ K の数列 E = (E_1, \dots, E_K) が与えられます。都市 1 から都市 N までいくつかの道を使って移動する方法であって、以下の条件を満たすものを良い経路と呼びます。
- 通る道の番号を通った順番に並べた列は、E の部分列である。
なお、部分列とは、数列から 0 個以上の要素を削除し、残った要素を元の順序で並べて得られる数列のことを指します。
全ての良い経路における、通る道の長さの合計の最小値を求めてください。
ただし、良い経路が存在しない場合は、そのことを報告してください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M, K \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq N, A_i \neq B_i \, (1 \leq i \leq M)
- 1 \leq C_i \leq 10^9 \, (1 \leq i \leq M)
- 1 \leq E_i \leq M \, (1 \leq i \leq K)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M K A_1 B_1 C_1 \vdots A_M B_M C_M E_1 \ldots E_K
出力
全ての良い経路における、通る道の長さの合計の最小値を出力せよ。ただし、良い経路が存在しないならば、-1 を出力せよ。
入力例 1
3 4 4 1 2 2 2 3 2 1 3 3 1 3 5 4 2 1 2
出力例 1
4
良い経路として考えられるのは次の二つです。
- 道 4 を使う。通る道の長さの合計は 5 である。
- 道 1, 2 をこの順で使う。通る道の長さの合計は 2 + 2 = 4 である。
よって、求める最小値は 4 です。
入力例 2
3 2 3 1 2 1 2 3 1 2 1 1
出力例 2
-1
良い経路は存在しません。
入力例 3
4 4 5 3 2 2 1 3 5 2 4 7 3 4 10 2 4 1 4 3
出力例 3
14
Score : 500 points
Problem Statement
There are N towns numbered 1, \dots, N, and M roads numbered 1, \dots, M.
Every road is directed; road i (1 \leq i \leq M) leads you from Town A_i to Town B_i. The length of road i is C_i.
You are given a sequence E = (E_1, \dots, E_K) of length K consisting of integers between 1 and M. A way of traveling from town 1 to town N using roads is called a good path if:
- the sequence of the roads' numbers arranged in the order used in the path is a subsequence of E.
Note that a subsequence of a sequence is a sequence obtained by removing 0 or more elements from the original sequence and concatenating the remaining elements without changing the order.
Find the minimum sum of the lengths of the roads used in a good path.
If there is no good path, report that fact.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M, K \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq N, A_i \neq B_i \, (1 \leq i \leq M)
- 1 \leq C_i \leq 10^9 \, (1 \leq i \leq M)
- 1 \leq E_i \leq M \, (1 \leq i \leq K)
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M K A_1 B_1 C_1 \vdots A_M B_M C_M E_1 \ldots E_K
Output
Find the minimum sum of the lengths of the roads used in a good path.
If there is no good path, print -1.
Sample Input 1
3 4 4 1 2 2 2 3 2 1 3 3 1 3 5 4 2 1 2
Sample Output 1
4
There are two possible good paths as follows:
- Using road 4. In this case, the sum of the length of the used road is 5.
- Using road 1 and 2 in this order. In this case, the sum of the lengths of the used roads is 2 + 2 = 4.
Therefore, the desired minimum value is 4.
Sample Input 2
3 2 3 1 2 1 2 3 1 2 1 1
Sample Output 2
-1
There is no good path.
Sample Input 3
4 4 5 3 2 2 1 3 5 2 4 7 3 4 10 2 4 1 4 3
Sample Output 3
14
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 行 M 列のグリッドがあり、上から i 行目、左から j 列目のマス (i,j) には整数 (i-1) \times M + j が書かれています。
このグリッドに、以下の操作を施します。
- 全てのマス (i,j) について、 i+j が奇数ならそのマスに書かれている数字を 0 に書き換える。
操作後のグリッドについて、Q 個の質問に答えてください。
i 個目の質問は以下の通りです:
- 以下の条件を全て満たすマス (p,q) 全てについて、そこに書かれている整数を全て足し合わせた値を 998244353 で割った余りを求めよ。
- A_i \le p \le B_i
- C_i \le q \le D_i
制約
- 入力は全て整数
- 1 \le N,M \le 10^9
- 1 \le Q \le 2 \times 10^5
- 1 \le A_i \le B_i \le N
- 1 \le C_i \le D_i \le M
入力
入力は以下の形式で標準入力から与えられる。
N M Q A_1 B_1 C_1 D_1 A_2 B_2 C_2 D_2 \vdots A_Q B_Q C_Q D_Q
出力
Q 行出力せよ。
そのうち i 行目には、 i 個目の質問に対する答えを整数として出力せよ。
入力例 1
5 4 6 1 3 2 4 1 5 1 1 5 5 1 4 4 4 2 2 5 5 4 4 1 5 1 4
出力例 1
28 27 36 14 0 104
この入力では、グリッドは以下の通りです。

この入力には 6 つの質問が含まれます。
- 1 個目の質問の答えは 0+3+0+6+0+8+0+11+0=28 です。
- 2 個目の質問の答えは 1+0+9+0+17=27 です。
- 3 個目の質問の答えは 17+0+19+0=36 です。
- 4 個目の質問の答えは 14 です。
- 5 個目の質問の答えは 0 です。
- 6 個目の質問の答えは 104 です。
入力例 2
1000000000 1000000000 3 1000000000 1000000000 1000000000 1000000000 165997482 306594988 719483261 992306147 1 1000000000 1 1000000000
出力例 2
716070898 240994972 536839100
1 個目の質問について、マス (10^9,10^9) に書かれている整数は 10^{18} ですが、それを 998244353 で割った余りを求めることに注意してください。
入力例 3
999999999 999999999 3 999999999 999999999 999999999 999999999 216499784 840031647 84657913 415448790 1 999999999 1 999999999
出力例 3
712559605 648737448 540261130
Score : 500 points
Problem Statement
We have a grid with N rows and M columns. The square (i,j) at the i-th row from the top and j-th column from the left has an integer (i-1) \times M + j written on it.
Let us perform the following operation on this grid.
- For every square (i,j) such that i+j is odd, replace the integer on that square with 0.
Answer Q questions on the grid after the operation.
The i-th question is as follows:
- Find the sum of the integers written on all squares (p,q) that satisfy all of the following conditions, modulo 998244353.
- A_i \le p \le B_i.
- C_i \le q \le D_i.
Constraints
- All values in the input are integers.
- 1 \le N,M \le 10^9
- 1 \le Q \le 2 \times 10^5
- 1 \le A_i \le B_i \le N
- 1 \le C_i \le D_i \le M
Input
The input is given from Standard Input in the following format:
N M Q A_1 B_1 C_1 D_1 A_2 B_2 C_2 D_2 \vdots A_Q B_Q C_Q D_Q
Output
Print Q lines.
The i-th line should contain the answer to the i-th question as an integer.
Sample Input 1
5 4 6 1 3 2 4 1 5 1 1 5 5 1 4 4 4 2 2 5 5 4 4 1 5 1 4
Sample Output 1
28 27 36 14 0 104
The grid in this input is shown below.

This input contains six questions.
- The answer to the first question is 0+3+0+6+0+8+0+11+0=28.
- The answer to the second question is 1+0+9+0+17=27.
- The answer to the third question is 17+0+19+0=36.
- The answer to the fourth question is 14.
- The answer to the fifth question is 0.
- The answer to the sixth question is 104.
Sample Input 2
1000000000 1000000000 3 1000000000 1000000000 1000000000 1000000000 165997482 306594988 719483261 992306147 1 1000000000 1 1000000000
Sample Output 2
716070898 240994972 536839100
For the first question, note that although the integer written on the square (10^9,10^9) is 10^{18}, you are to find it modulo 998244353.
Sample Input 3
999999999 999999999 3 999999999 999999999 999999999 999999999 216499784 840031647 84657913 415448790 1 999999999 1 999999999
Sample Output 3
712559605 648737448 540261130