実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
N 問の問題からなるコンテストが開催され、i\ (1\leq i\leq N) 問目の配点は A_i 点でした。
すぬけくんはこのコンテストに参加し、B_1,B_2,\ldots,B_M 問目の M 問を解きました。 すぬけくんの総得点を求めてください。
ただし、総得点とは解いた問題の配点の総和を意味するものとします。
制約
- 1\leq M \leq N \leq 100
- 1\leq A_i \leq 100
- 1\leq B_1 < B_2 < \ldots < B_M \leq N
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \dots A_N B_1 B_2 \dots B_M
出力
答えを整数として出力せよ。
入力例 1
3 2 10 20 30 1 3
出力例 1
40
すぬけくんは 1 問目と 3 問目を解きました。 配点はそれぞれ 10 点と 30 点なので、総得点は 10+30=40 点です。
入力例 2
4 1 1 1 1 100 4
出力例 2
100
入力例 3
8 4 22 75 26 45 72 81 47 29 4 6 7 8
出力例 3
202
Score : 100 points
Problem Statement
There was a contest with N problems. The i-th (1\leq i\leq N) problem was worth A_i points.
Snuke took part in this contest and solved M problems: the B_1-th, B_2-th, \ldots, and B_M-th ones. Find his total score.
Here, the total score is defined as the sum of the points for the problems he solved.
Constraints
- 1\leq M \leq N \leq 100
- 1\leq A_i \leq 100
- 1\leq B_1 < B_2 < \ldots < B_M \leq N
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \dots A_N B_1 B_2 \dots B_M
Output
Print the answer as an integer.
Sample Input 1
3 2 10 20 30 1 3
Sample Output 1
40
Snuke solved the 1-st and 3-rd problems, which are worth 10 and 30 points, respectively. Thus, the total score is 10+30=40 points.
Sample Input 2
4 1 1 1 1 100 4
Sample Output 2
100
Sample Input 3
8 4 22 75 26 45 72 81 47 29 4 6 7 8
Sample Output 3
202
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
6 桁の正整数 N が与えられます。
この整数が以下の条件を全て満たすか判定してください。
- N の各桁のうち、 1 は丁度 1 つである。
- N の各桁のうち、 2 は丁度 2 つである。
- N の各桁のうち、 3 は丁度 3 つである。
制約
- N は 100000 \le N \le 999999 を満たす整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
N が問題文中の条件を全て満たすなら Yes 、そうでないなら No と 1 行に出力せよ。
入力例 1
123233
出力例 1
Yes
123233 は問題文中の条件を満たすので、 Yes と出力します。
入力例 2
123234
出力例 2
No
123234 は問題文中の条件を満たさないので、 No と出力します。
入力例 3
323132
出力例 3
Yes
入力例 4
500000
出力例 4
No
Score : 100 points
Problem Statement
You are given a 6-digit positive integer N.
Determine whether N satisfies all of the following conditions.
- Among the digits of N, the digit 1 appears exactly once.
- Among the digits of N, the digit 2 appears exactly twice.
- Among the digits of N, the digit 3 appears exactly three times.
Constraints
- N is an integer satisfying 100000 \le N \le 999999.
Input
The input is given from Standard Input in the following format:
N
Output
Print Yes if N satisfies all the conditions described in the problem statement, and No otherwise, in one line.
Sample Input 1
123233
Sample Output 1
Yes
123233 satisfies the conditions in the problem statement, so print Yes.
Sample Input 2
123234
Sample Output 2
No
123234 does not satisfy the conditions in the problem statement, so print No.
Sample Input 3
323132
Sample Output 3
Yes
Sample Input 4
500000
Sample Output 4
No
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
1,2,\dots,N の番号が付いた N 個の箱があります。最初は全ての箱が空です。
これから Q 個のボールが順番にやってきます。
高橋君は、数列 X=(X_1,X_2,\dots,X_Q) に従ってボールを箱に入れます。
具体的には、 i 番目にやってきたボールに次の処理を行います。
- X_i \ge 1 である場合 : このボールを、箱 X_i に入れる。
- X_i = 0 である場合 : このボールを、現在入っているボールが最も少ない箱のうち番号が最小である箱に入れる。
それぞれのボールをどの箱に入れたかを求めてください。
制約
- 入力は全て整数
- 1 \le N \le 100
- 1 \le Q \le 100
- 0 \le X_i \le N
入力
入力は以下の形式で標準入力から与えられる。
N Q X_1 X_2 \dots X_Q
出力
i 番目にやってきたボールを箱 B_i に入れたとき、次の形式に従って出力せよ。
B_1 B_2 \dots B_Q
入力例 1
4 5 2 0 3 0 0
出力例 1
2 1 3 4 1
箱が 4 つあり、ボールは 5 個やってきます。
- 最初、全ての箱は空です。
- 各箱に入っているボールの数は箱 1 から順に 0,0,0,0 個です。
- X_1=2 なので、 1 番目にやってきたボールを箱 2 に入れます。
- 各箱に入っているボールの数は箱 1 から順に 0,1,0,0 個となります。
- X_2=0 なので、 2 番目にやってきたボールを現在入っているボールが最も少ない箱のうち番号が最小である箱である箱 1 に入れます。
- 各箱に入っているボールの数は箱 1 から順に 1,1,0,0 個となります。
- X_3=3 なので、 3 番目にやってきたボールを箱 3 に入れます。
- 各箱に入っているボールの数は箱 1 から順に 1,1,1,0 個となります。
- X_4=0 なので、 4 番目にやってきたボールを現在入っているボールが最も少ない箱のうち番号が最小である箱である箱 4 に入れます。
- 各箱に入っているボールの数は箱 1 から順に 1,1,1,1 個となります。
- X_5=0 なので、 5 番目にやってきたボールを現在入っているボールが最も少ない箱のうち番号が最小である箱である箱 1 に入れます。
- 各箱に入っているボールの数は箱 1 から順に 2,1,1,1 個となります。
各ボールを、やってきた順に箱 2,1,3,4,1 に入れました。よって、 2 1 3 4 1 と出力します。
入力例 2
3 7 1 1 0 0 0 0 0
出力例 2
1 1 2 3 2 3 1
入力例 3
6 20 4 6 0 3 4 2 6 5 2 3 0 3 2 5 0 3 5 0 2 0
出力例 3
4 6 1 3 4 2 6 5 2 3 1 3 2 5 1 3 5 4 2 6
Score : 200 points
Problem Statement
There are N boxes numbered 1,2,\dots,N. Initially, all boxes are empty.
Q balls will come in order.
Takahashi will put the balls into the boxes according to the sequence X=(X_1,X_2,\dots,X_Q).
Specifically, he performs the following process for the i-th ball:
- If X_i \ge 1: Put this ball into box X_i.
- If X_i = 0: Put this ball into the box with the smallest number among those containing the fewest balls.
Find which box each ball was put into.
Constraints
- All input values are integers.
- 1 \le N \le 100
- 1 \le Q \le 100
- 0 \le X_i \le N
Input
The input is given from Standard Input in the following format:
N Q X_1 X_2 \dots X_Q
Output
If the i-th ball was put into box B_i, output in the following format:
B_1 B_2 \dots B_Q
Sample Input 1
4 5 2 0 3 0 0
Sample Output 1
2 1 3 4 1
There are 4 boxes, and 5 balls come.
- Initially, all boxes are empty.
- The numbers of balls in box 1,2,3,4 are 0,0,0,0, respectively.
- Since X_1=2, put the 1st ball into box 2.
- The numbers of balls in box 1,2,3,4 are 0,1,0,0, respectively.
- Since X_2=0, put the 2nd ball into box 1, which has the smallest number among those containing the fewest balls.
- The numbers of balls in box 1,2,3,4 are 1,1,0,0, respectively.
- Since X_3=3, put the 3rd ball into box 3.
- The numbers of balls in box 1,2,3,4 are 1,1,1,0, respectively.
- Since X_4=0, put the 4th ball into box 4, which has the smallest number among those containing the fewest balls.
- The numbers of balls in box 1,2,3,4 are 1,1,1,1, respectively.
- Since X_5=0, put the 5th ball into box 1, which has the smallest number among those containing the fewest balls.
- The numbers of balls in box 1,2,3,4 are 2,1,1,1, respectively.
The balls were put into boxes 2,1,3,4,1 in order. Thus, output 2 1 3 4 1.
Sample Input 2
3 7 1 1 0 0 0 0 0
Sample Output 2
1 1 2 3 2 3 1
Sample Input 3
6 20 4 6 0 3 4 2 6 5 2 3 0 3 2 5 0 3 5 0 2 0
Sample Output 3
4 6 1 3 4 2 6 5 2 3 1 3 2 5 1 3 5 4 2 6
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
高橋君は野球をモチーフにしたゲームを作ろうとしましたが、うまくコードが書けなくて困っています。
高橋君の代わりに次の問題を解くプログラムを作ってください。
マス 0, マス 1, マス 2, マス 3 の 4 つのマス目があります。はじめマスの上には何もありません。
また、整数 P があり、はじめ P = 0 です。
正の整数からなる数列 A = (A_1, A_2, \dots, A_N) が与えられるので、i = 1, 2, \dots, N について順番に次の操作を行います。
- マス 0 に駒を 1 個置く。
- マス上のすべての駒を番号が A_i 大きいマスに進める。言い換えると、駒がマス x にあればその駒をマス x + A_i に移動する。
ただし移動先のマスが存在しない (すなわち x + A_i が 4 以上になる) 駒たちに関しては、それらを取り除いて P に取り除いた個数を加算する。
すべての操作を行った後の P の値を出力してください。
制約
- 1 \leq N \leq 100
- 1 \leq A_i \leq 4
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
操作終了時点での P の値を出力せよ。
入力例 1
4 1 1 3 2
出力例 1
3
操作を説明すると次のようになり、操作終了時点での P の値は 3 になります。
- i=1 での操作
- マス 0 に駒を置く。この時点でマス 0 にコマが乗っている。
- すべての駒を 1 大きいマスに進める。移動を終えた時点でマス 1 に駒が乗っている。
- i=2 での操作
- マス 0 に駒を置く。この時点でマス 0, 1 にコマが乗っている。
- すべての駒を 1 大きいマスに進める。移動を終えた時点でマス 1, 2 に駒が乗っている。
- i=3 での操作
- マス 0 に駒を置く。この時点でマス 0, 1, 2 にコマが乗っている。
- すべての駒を 3 大きいマスに進める。
この時、マス 1,2 にある駒は移動先のマスが存在しないため (それぞれ 1+3=4,2+3=5 なので) 、盤上から取り除いて P に 2 を加算する。P の値は 2 になる。
移動を終えた時点でマス 3 に駒が乗っている。
- i=4 での操作
- マス 0 に駒を置く。この時点でマス 0, 3 にコマが乗っている。
- すべての駒を 2 大きいマスに進める。
この時、マス 3 にある駒は移動先のマスが存在しないため (3+2=5 なので) 、盤上から取り除いて P に 1 を加算する。P の値は 3 になる。
移動を終えた時点でマス 2 に駒が乗っている。
入力例 2
3 1 1 1
出力例 2
0
P の値が操作中に変化しない場合もあります。
入力例 3
10 2 2 4 1 1 1 4 2 2 1
出力例 3
8
Score : 200 points
Problem Statement
Takahashi is trying to create a game inspired by baseball, but he is having difficulty writing the code.
Write a program for Takahashi that solves the following problem.
There are 4 squares called Square 0, Square 1, Square 2, and Square 3. Initially, all squares are empty.
There is also an integer P; initially, P = 0.
Given a sequence of positive integers A = (A_1, A_2, \dots, A_N), perform the following operations for i = 1, 2, \dots, N in this order:
- Put a piece on Square 0.
- Advance every piece on the squares A_i squares ahead. In other words, if Square x has a piece, move the piece to Square (x + A_i).
If, however, the destination square does not exist (i.e. x + A_i is greater than or equal to 4) for a piece, remove it. Add to P the number of pieces that have been removed.
Print the value of P after all the operations have been performed.
Constraints
- 1 \leq N \leq 100
- 1 \leq A_i \leq 4
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Print the value of P after all the operations have been performed.
Sample Input 1
4 1 1 3 2
Sample Output 1
3
The operations are described below. After all the operations have been performed, P equals 3.
- The operations for i=1:
- Put a piece on Square 0. Now, Square 0 has a piece.
- Advance every piece on the squares 1 square ahead. After these moves, Square 1 has a piece.
- The operations for i=2:
- Put a piece on Square 0. Now, Squares 0 and 1 have a piece.
- Advance every piece on the squares 1 square ahead. After these moves, Squares 1 and 2 have a piece.
- The operations for i=3:
- Put a piece on Square 0. Now, Squares 0, 1, and 2 have a piece.
- Advance every piece on the squares 3 squares ahead.
Here, for the pieces on Squares 1 and 2, the destination squares do not exist (since 1+3=4 and 2+3=5), so remove these pieces and add 2 to P. P now equals 2. After these moves, Square 3 has a piece.
- The operations for i=4:
- Put a piece on Square 0. Now, Squares 0 and 3 have a piece.
- Advance every piece on the squares 2 squares ahead.
Here, for the piece on Square 3, the destination square does not exist (since 3+2=5), so remove this piece and add 1 to P. P now equals 3.
After these moves, Square 2 has a piece.
Sample Input 2
3 1 1 1
Sample Output 2
0
The value of P may not be updated by the operations.
Sample Input 3
10 2 2 4 1 1 1 4 2 2 1
Sample Output 3
8
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
N 頂点の木 T があり、 i (1\leq i\leq N-1) 番目の辺は頂点 U_i と頂点 V_i を結んでいます。
T 上の相異なる 2 頂点 X,Y が与えられるので、 頂点 X から頂点 Y への単純パス上の頂点(端点含む)を順に列挙してください。
ただし、木上の任意の相異なる 2 頂点 a,b について、a から b への単純パスがただ一つ存在することが証明できます。
単純パスとは?
グラフ G 上の頂点 X,Y に対して、頂点列 v_1,v_2, \ldots, v_k であって、 v_1=X, v_k=Y かつ、1\leq i\leq k-1 に対して v_i と v_{i+1} が辺で結ばれているようなものを頂点 X から頂点 Y への パス と呼びます。 さらに、v_1,v_2, \ldots, v_k がすべて異なるようなものを頂点 X から頂点 Y への 単純パス と呼びます。制約
- 1\leq N\leq 2\times 10^5
- 1\leq X,Y\leq N
- X\neq Y
- 1\leq U_i,V_i\leq N
- 入力はすべて整数
- 与えられるグラフは木
入力
入力は以下の形式で標準入力から与えられる。
N X Y
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
出力
頂点 X から頂点 Y への単純パス上の頂点番号を順に空白区切りで出力せよ。
入力例 1
5 2 5 1 2 1 3 3 4 3 5
出力例 1
2 1 3 5
木 T は以下のような形であり、頂点 2 から頂点 5への単純パスは
頂点 2 \to 頂点 1 \to 頂点 3 \to 頂点 5 となります。
よって、2,1,3,5 をこの順に空白区切りで出力します。

入力例 2
6 1 2 3 1 2 5 1 2 4 1 2 6
出力例 2
1 2
木 T は以下のような形です。

Score : 300 points
Problem Statement
There is a tree T with N vertices. The i-th edge (1\leq i\leq N-1) connects vertex U_i and vertex V_i.
You are given two different vertices X and Y in T. List all vertices along the simple path from vertex X to vertex Y in order, including endpoints.
It can be proved that, for any two different vertices a and b in a tree, there is a unique simple path from a to b.
What is a simple path?
For vertices X and Y in a graph G, a path from vertex X to vertex Y is a sequence of vertices v_1,v_2, \ldots, v_k such that v_1=X, v_k=Y, and v_i and v_{i+1} are connected by an edge for each 1\leq i\leq k-1. Additionally, if all of v_1,v_2, \ldots, v_k are distinct, the path is said to be a simple path from vertex X to vertex Y.Constraints
- 1\leq N\leq 2\times 10^5
- 1\leq X,Y\leq N
- X\neq Y
- 1\leq U_i,V_i\leq N
- All values in the input are integers.
- The given graph is a tree.
Input
The input is given from Standard Input in the following format:
N X Y
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
Output
Print the indices of all vertices along the simple path from vertex X to vertex Y in order, with spaces in between.
Sample Input 1
5 2 5 1 2 1 3 3 4 3 5
Sample Output 1
2 1 3 5
The tree T is shown below. The simple path from vertex 2 to vertex 5 is 2 \to 1 \to 3 \to 5.
Thus, 2,1,3,5 should be printed in this order, with spaces in between.

Sample Input 2
6 1 2 3 1 2 5 1 2 4 1 2 6
Sample Output 2
1 2
The tree T is shown below.

実行時間制限: 2 sec / メモリ制限: 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.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 425 点
問題文
N+2 個のマスが横一列に並んでいます。左から i 番目のマスをマス i と表します。
マス 1 からマス N には石が 1 個ずつ置かれています。
各 1\leq i \leq N について、S_i が W のときマス i に置かれている石の色は白であり、S_i が B のときマス i に置かれている石の色は黒です。
マス N+1,N+2 には何も置かれていません。
あなたは以下の操作を好きな回数(0 回でもよい)行うことができます。
- 石が 2 個並んでいる箇所を選び、その 2 個の石を順序を保って空きマスに移す。
より正確には次の通り。1 以上 N+1 以下の整数 x であって、マス x,x+1 の両方に石が置かれているものを選ぶ。石の置かれていないマスを k,k+1 とする。マス x,x+1 にある石をそれぞれマス k,k+1 に移動する。
以下の状態にすることが可能か判定し、可能なら操作回数の最小値を求めてください。
- マス 1 からマス N には石が 1 個ずつ置かれており、各 1\leq i \leq N について、T_i が
Wのときマス i に置かれている石の色は白、T_i がBのときマス i に置かれている石の色は黒である。
制約
- 2 \leq N \leq 14
- N は整数である
- S,T は
BおよびWのみからなる長さ N の文字列である
入力
入力は以下の形式で標準入力から与えられる。
N S T
出力
目的の状態にすることが可能なら操作回数の最小値を出力せよ。不可能ならかわりに -1 を出力せよ。
入力例 1
6 BWBWBW WWWBBB
出力例 1
4
石が置かれていないマスを . と表します。以下のようにして 4 回の操作で目的の状態にすることができ、これが最小回数です。
BWBWBW..BW..BWBWBWWBB..W..WBBBWWWWWBBB..
入力例 2
6 BBBBBB WWWWWW
出力例 2
-1
入力例 3
14 BBBWBWWWBBWWBW WBWWBBWWWBWBBB
出力例 3
7
Score : 425 points
Problem Statement
There are N+2 cells arranged in a row. Let cell i denote the i-th cell from the left.
There is one stone placed in each of the cells from cell 1 to cell N.
For each 1 \leq i \leq N, the stone in cell i is white if S_i is W, and black if S_i is B.
Cells N+1 and N+2 are empty.
You can perform the following operation any number of times (possibly zero):
- Choose a pair of adjacent cells that both contain stones, and move these two stones to the empty two cells while preserving their order.
More precisely, choose an integer x such that 1 \leq x \leq N+1 and both cells x and x+1 contain stones. Let k and k+1 be the empty two cells. Move the stones from cells x and x+1 to cells k and k+1, respectively.
Determine if it is possible to achieve the following state, and if so, find the minimum number of operations required:
- Each of the cells from cell 1 to cell N contains one stone, and for each 1 \leq i \leq N, the stone in cell i is white if T_i is
W, and black if T_i isB.
Constraints
- 2 \leq N \leq 14
- N is an integer.
- Each of S and T is a string of length N consisting of
BandW.
Input
The input is given from Standard Input in the following format:
N S T
Output
If it is possible to achieve the desired state, print the minimum number of operations required. If it is impossible, print -1.
Sample Input 1
6 BWBWBW WWWBBB
Sample Output 1
4
Using . to represent an empty cell, the desired state can be achieved in four operations as follows, which is the minimum:
BWBWBW..BW..BWBWBWWBB..W..WBBBWWWWWBBB..
Sample Input 2
6 BBBBBB WWWWWW
Sample Output 2
-1
Sample Input 3
14 BBBWBWWWBBWWBW WBWWBBWWWBWBBB
Sample Output 3
7
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 475 点
問題文
整数の組 (a, b, c) であって以下の条件を満たすものの個数を 998244353 で割ったあまりを求めてください。
- 1 \leq a,b,c \leq N
- a,b,c は相異なる。
- a を b で割ったあまりは c に等しい。
制約
- 3 \leq N \leq 10^{12}
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを 1 行で出力せよ。
入力例 1
7
出力例 1
12
条件を満たす整数の組 (a,b,c) は以下の 12 通り存在します。
- (3,2,1)
- (4,3,1)
- (5,2,1)
- (5,3,2)
- (5,4,1)
- (6,4,2)
- (6,5,1)
- (7,2,1)
- (7,3,1)
- (7,4,3)
- (7,5,2)
- (7,6,1)
入力例 2
441
出力例 2
94700
入力例 3
411111111114
出力例 3
462474062
Score : 475 points
Problem Statement
Find the number, modulo 998244353, of integer tuples (a, b, c) that satisfy the following conditions.
- 1 \leq a,b,c \leq N
- a,b,c are pairwise distinct.
- The remainder when a is divided by b is equal to c.
Constraints
- 3 \leq N \leq 10^{12}
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Output the answer in one line.
Sample Input 1
7
Sample Output 1
12
There are 12 integer tuples (a,b,c) that satisfy the conditions:
- (3,2,1)
- (4,3,1)
- (5,2,1)
- (5,3,2)
- (5,4,1)
- (6,4,2)
- (6,5,1)
- (7,2,1)
- (7,3,1)
- (7,4,3)
- (7,5,2)
- (7,6,1)
Sample Input 2
441
Sample Output 2
94700
Sample Input 3
411111111114
Sample Output 3
462474062
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 550 点
問題文
N 頂点の木があります。 1 番目の頂点が根であり、i 番目 (2\leq i\leq N) の頂点の親は p _ i\ (1\leq p _ i\lt i) です。
根でない頂点には、敵か薬のどちらか一方が配置されています。 高橋くんは、すべての敵を倒したいです。 はじめ、高橋くんの強さは 1 で、頂点 1 にいます。 i=2,\ldots,N について、i 番目の頂点の情報は整数の組 (t _ i,s _ i,g _ i) を用いて次のように表されます。
- t _ i=1 ならば i 番目の頂点には敵がいます。この頂点に高橋くんが初めて訪れたとき、高橋くんの強さが s _ i 未満だった場合高橋くんは敵に倒されて敗北し、高橋くんは他の頂点に移動できなくなります。そうでなかった場合、高橋くんは敵を倒し、強さが g _ i 上昇します。
- t _ i=2 ならば i 番目の頂点には薬があります。この頂点に高橋くんが初めて訪れたとき、高橋くんは薬を飲み、強さが g _ i 倍になります。(薬がある頂点では、s _ i=0 です。)
薬がある頂点はたかだか 10 個です。
高橋くんは、隣接する頂点に移動することができます。 高橋くんがすべての敵を倒すことができるか判定してください。
制約
- 2\leq N\leq 500
- 1\leq p _ i\lt i\ (2\leq i\leq N)
- t _ i\in\lbrace1,2\rbrace\ (2\leq i\leq N)
- t _ i=1\implies1\leq s _ i\leq 10 ^ 9\ (2\leq i\leq N)
- t _ i=2\implies s _ i=0\ (2\leq i\leq N)
- 1\leq g _ i\leq 10 ^ 9\ (2\leq i\leq N)
- t _ i=2 である頂点は 10 個以下
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N p _ 2 t _ 2 s _ 2 g _ 2 p _ 3 t _ 3 s _ 3 g _ 3 \vdots p _ N t _ N s _ N g _ N
出力
答え(Yes または No)を 1 行で出力せよ。
入力例 1
8 1 2 0 3 2 1 3 3 1 2 0 4 4 1 2 2 1 2 0 5 6 1 5 5 5 1 140 1
出力例 1
Yes
はじめ、木は以下のようになっています。

高橋くんは、頂点 1 から 2,3,2,1,6,7,6,1,4,5,8 の順に移動することで、すべての敵を倒すことができます。 このとき、高橋くんがいる頂点と高橋くんの強さは以下の図のように変化します(図では、すでに訪れたことのある頂点への移動は省略しています)。

例えば、頂点 1 から 4,5,8 の順に移動すると、頂点 8 に訪れた時点での強さが s _ 8=140 より小さいので高橋くんは敗北してしまい、すべての敵を倒すことができません。
入力例 2
12 1 1 166 619 1 1 17 592 2 1 222 983 2 1 729 338 5 1 747 62 3 1 452 815 3 2 0 1 4 2 0 40 4 1 306 520 6 1 317 591 1 1 507 946
出力例 2
No
入力例 3
12 1 1 1 791 2 2 0 410 2 1 724 790 2 1 828 599 5 2 0 13 3 1 550 803 1 1 802 506 5 1 261 587 6 1 663 329 8 1 11 955 9 1 148 917
出力例 3
Yes
入力例 4
12 1 2 0 1000000000 2 2 0 1000000000 3 2 0 1000000000 4 2 0 1000000000 5 2 0 1000000000 6 2 0 1000000000 7 2 0 1000000000 8 2 0 1000000000 9 2 0 1000000000 10 2 0 1000000000 11 1 1 1
出力例 4
Yes
Score : 550 points
Problem Statement
There is a tree with N vertices. The 1-st vertex is the root, and the parent of the i-th vertex (2\leq i\leq N) is p _ i\ (1\leq p _ i\lt i).
Each non-root vertex has an enemy or a medicine on it. Takahashi wants to defeat all the enemies. Initially, his strength is 1, and he is at vertex 1. For i=2,\ldots,N, the information of the i-th vertex is represented by a triple of integers (t _ i,s _ i,g _ i) as follows.
- If t _ i=1, there is an enemy at the i-th vertex. When Takahashi visits this vertex for the first time, if his strength is less than s _ i, Takahashi is defeated by the enemy and loses, after which he cannot move to other vertices. Otherwise, he defeats the enemy, and his strength increases by g _ i.
- If t _ i=2, there is a medicine at the i-th vertex. When Takahashi visits this vertex for the first time, he takes the medicine, and his strength is multiplied by g _ i. (For a vertex with a medicine, s _ i=0.)
There are at most 10 vertices with a medicine.
Takahashi can repeatedly move to an adjacent vertex. Determine if he can defeat all the enemies.
Constraints
- 2\leq N\leq 500
- 1\leq p _ i\lt i\ (2\leq i\leq N)
- t _ i\in\lbrace1,2\rbrace\ (2\leq i\leq N)
- t _ i=1\implies1\leq s _ i\leq 10 ^ 9\ (2\leq i\leq N)
- t _ i=2\implies s _ i=0\ (2\leq i\leq N)
- 1\leq g _ i\leq 10 ^ 9\ (2\leq i\leq N)
- There are at most 10 vertices with t _ i=2.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N p _ 2 t _ 2 s _ 2 g _ 2 p _ 3 t _ 3 s _ 3 g _ 3 \vdots p _ N t _ N s _ N g _ N
Output
Print the answer (Yes or No) in one line.
Sample Input 1
8 1 2 0 3 2 1 3 3 1 2 0 4 4 1 2 2 1 2 0 5 6 1 5 5 5 1 140 1
Sample Output 1
Yes
Initially, the tree looks like this:

Takahashi can defeat all the enemies by moving from vertex 1 to 2,3,2,1,6,7,6,1,4,5,8 in this order. Here, his position and strength change as shown in the following figure (movements to vertices that have already been visited are omitted).

On the other hand, if he moves from vertex 1 to 4,5,8 in this order, for example, his strength when visiting vertex 8 will be less than s _ 8=140, so he will lose without defeating all the enemies.
Sample Input 2
12 1 1 166 619 1 1 17 592 2 1 222 983 2 1 729 338 5 1 747 62 3 1 452 815 3 2 0 1 4 2 0 40 4 1 306 520 6 1 317 591 1 1 507 946
Sample Output 2
No
Sample Input 3
12 1 1 1 791 2 2 0 410 2 1 724 790 2 1 828 599 5 2 0 13 3 1 550 803 1 1 802 506 5 1 261 587 6 1 663 329 8 1 11 955 9 1 148 917
Sample Output 3
Yes
Sample Input 4
12 1 2 0 1000000000 2 2 0 1000000000 3 2 0 1000000000 4 2 0 1000000000 5 2 0 1000000000 6 2 0 1000000000 7 2 0 1000000000 8 2 0 1000000000 9 2 0 1000000000 10 2 0 1000000000 11 1 1 1
Sample Output 4
Yes