実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
英大文字からなる長さ 3 の文字列 T が、英小文字からなる文字列 S の 空港コード であるとは、 T が S から次のいずれかの方法により得られることとします。
- S の長さ 3 の(連続とは限らない)部分列をとり、それを英大文字に変換したものを T とする
- S の長さ 2 の(連続とは限らない)部分列をとり、それを英大文字に変換したものの末尾に
Xを追加したものを T とする
文字列 S, T が与えられるので、 T が S の空港コードであるか判定してください。
制約
- S は英小文字からなる長さ 3 以上 10^5 以下の文字列
- T は英大文字からなる長さ 3 の文字列
入力
入力は以下の形式で標準入力から与えられる。
S T
出力
T が S の空港コードであるならば Yes を、そうでないならば No を出力せよ。
入力例 1
narita NRT
出力例 1
Yes
narita の部分列 nrt を英大文字に変換した文字列 NRT は、 narita の空港コードです。
入力例 2
losangeles LAX
出力例 2
Yes
losangeles の部分列 la を英大文字に変換した文字列 LA の末尾に X を追加したもの LAX は、 losangeles の空港コードです。
入力例 3
snuke RNG
出力例 3
No
Score: 300 points
Problem Statement
A string T of length 3 consisting of uppercase English letters is an airport code for a string S of lowercase English letters if and only if T can be derived from S by one of the following methods:
- Take a subsequence of length 3 from S (not necessarily contiguous) and convert it to uppercase letters to form T.
- Take a subsequence of length 2 from S (not necessarily contiguous), convert it to uppercase letters, and append
Xto the end to form T.
Given strings S and T, determine if T is an airport code for S.
Constraints
- S is a string of lowercase English letters with a length between 3 and 10^5, inclusive.
- T is a string of uppercase English letters with a length of 3.
Input
The input is given from Standard Input in the following format:
S T
Output
Print Yes if T is an airport code for S, and No otherwise.
Sample Input 1
narita NRT
Sample Output 1
Yes
The subsequence nrt of narita, when converted to uppercase, forms the string NRT, which is an airport code for narita.
Sample Input 2
losangeles LAX
Sample Output 2
Yes
The subsequence la of losangeles, when converted to uppercase and appended with X, forms the string LAX, which is an airport code for losangeles.
Sample Input 3
snuke RNG
Sample Output 3
No
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
縦 H マス, 横 W マスのグリッドがあります。上から i 行目、左から j 列目のマスを (i, j) と呼びます。
はじめ、グリッド上には、ある 縦横 2 マス以上 の部分長方形の内部にあるマスにクッキーが 1 枚ずつ置かれていて、それ以外のマスにはクッキーが置かれていません。
形式的に説明すると、以下の条件を全て満たす 4 つの整数の組 (a,b,c,d) がただ 1 つ存在します。
- 1 \leq a \lt b \leq H
- 1 \leq c \lt d \leq W
- グリッド上のマスのうち、a \leq i \leq b, c \leq j \leq d を満たす全てのマス (i, j) にはクッキーが 1 枚ずつ置かれていて、それ以外のマスにはクッキーが置かれていない。
ところが、すぬけ君がグリッド上のクッキーのどれか 1 枚を取って食べてしまいました。
すぬけ君がクッキーを取ったマスは、クッキーが置かれていない状態に変わります。
すぬけ君がクッキーを食べた後のグリッドの状態が入力として与えられます。
マス (i, j) の状態は文字 S_{i,j} として与えられて、# はクッキーが置かれているマスを, . はクッキーが置かれていないマスを意味します。
すぬけ君が食べたクッキーが元々置かれていたマスを答えてください。(答えは一意に定まります。)
制約
- 2 \leq H, W \leq 500
- S_{i,j} は
#または.
入力
入力は以下の形式で標準入力から与えられる。
H W
S_{1,1}S_{1,2}\dotsS_{1,W}
S_{2,1}S_{2,2}\dotsS_{2,W}
\vdots
S_{H,1}S_{H,2}\dotsS_{H,W}
出力
すぬけ君が食べたクッキーが元々置かれていたマスを (i, j) とする。i, j をこの順に空白区切りで出力せよ。
入力例 1
5 6 ...... ..#.#. ..###. ..###. ......
出力例 1
2 4
はじめ、クッキーは (2, 3) を左上、(4, 5) を右下とする部分長方形の内部にあるマスに置かれていて、すぬけ君は (2, 4) にあるクッキーを食べたことがわかります。よって (2, 4) を出力します。
入力例 2
3 2 #. ## ##
出力例 2
1 2
はじめ、クッキーは (1, 1) を左上、(3, 2) を右下とする部分長方形の内部にあるマスに置かれていて、すぬけ君は (1, 2) にあるクッキーを食べたことがわかります。
入力例 3
6 6 ..#### ..##.# ..#### ..#### ..#### ......
出力例 3
2 5
Score : 300 points
Problem Statement
There is a grid with H rows and W columns. Let (i, j) denote the square at the i-th row from the top and the j-th column from the left.
Initially, there was one cookie on each square inside a rectangle whose height and width were at least 2 squares long, and no cookie on the other squares.
Formally, there was exactly one quadruple of integers (a,b,c,d) that satisfied all of the following conditions.
- 1 \leq a \lt b \leq H
- 1 \leq c \lt d \leq W
- There was one cookie on each square (i, j) such that a \leq i \leq b, c \leq j \leq d, and no cookie on the other squares.
However, Snuke took and ate one of the cookies on the grid.
The square that contained that cookie is now empty.
As the input, you are given the state of the grid after Snuke ate the cookie.
The state of the square (i, j) is given as the character S_{i,j}, where # means a square with a cookie, and . means a square without one.
Find the square that contained the cookie eaten by Snuke. (The answer is uniquely determined.)
Constraints
- 2 \leq H, W \leq 500
- S_{i,j} is
#or..
Input
The input is given from Standard Input in the following format:
H W
S_{1,1}S_{1,2}\dotsS_{1,W}
S_{2,1}S_{2,2}\dotsS_{2,W}
\vdots
S_{H,1}S_{H,2}\dotsS_{H,W}
Output
Let (i, j) the square contained the cookie eaten by Snuke. Print i and j in this order, separated by a space.
Sample Input 1
5 6 ...... ..#.#. ..###. ..###. ......
Sample Output 1
2 4
Initially, cookies were on the squares inside the rectangle with (2, 3) as the top-left corner and (4, 5) as the bottom-right corner, and Snuke ate the cookie on (2, 4). Thus, you should print (2, 4).
Sample Input 2
3 2 #. ## ##
Sample Output 2
1 2
Initially, cookies were placed on the squares inside the rectangle with (1, 1) as the top-left corner and (3, 2) as the bottom-right corner, and Snuke ate the cookie at (1, 2).
Sample Input 3
6 6 ..#### ..##.# ..#### ..#### ..#### ......
Sample Output 3
2 5
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
高橋君と青木君が次のようなゲームをします。
- まず、高橋君が A 以上 B 以下の好きな整数を選び、青木君に伝える
- 次に、青木君が C 以上 D 以下の好きな整数を選ぶ
- 二人の選んだ整数の和が素数なら青木君の勝ち、そうでなければ高橋君の勝ち
二人が最適な戦略を取るとき、どちらが勝ちますか?
制約
- 1 \leq A \leq B \leq 100
- 1 \leq C \leq D \leq 100
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
A B C D
出力
二人が最適な戦略をとったとき、高橋君が勝つなら Takahashi、青木君が勝つなら Aoki を出力せよ。
入力例 1
2 3 3 4
出力例 1
Aoki
例えば高橋君が 2 を選んだときは、青木君は 3 を選ぶことで、和を素数である 5 にすることができます。
入力例 2
1 100 50 60
出力例 2
Takahashi
最適な戦略を取ると高橋君が必ず勝ちます。
入力例 3
3 14 1 5
出力例 3
Aoki
Score : 400 points
Problem Statement
Takahashi and Aoki are playing a game.
- First, Takahashi chooses an integer between A and B (inclusive) and tells it to Aoki.
- Next, Aoki chooses an integer between C and D (inclusive).
- If the sum of these two integers is a prime, then Aoki wins; otherwise, Takahashi wins.
When the two players play optimally, which player will win?
Constraints
- 1 \leq A \leq B \leq 100
- 1 \leq C \leq D \leq 100
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
A B C D
Output
If Takahashi wins when the two players play optimally, print Takahashi; if Aoki wins, print Aoki.
Sample Input 1
2 3 3 4
Sample Output 1
Aoki
For example, if Takahashi chooses 2, Aoki can choose 3 to make the sum 5, which is a prime.
Sample Input 2
1 100 50 60
Sample Output 2
Takahashi
If they play optimally, Takahashi always wins.
Sample Input 3
3 14 1 5
Sample Output 3
Aoki
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
AtCoder社の社員である青木さんの今月の給料は、整数 N と長さ N の数列 A を用いて以下のように決められます。
まず、青木さんに 1 から N までの整数が等確率で出る N 面ダイスと変数 x=0 を渡します。
その後、以下の手順を終了まで繰り返します。
- ダイスを 1 度振り、出た目を y とする。
- もし x<y なら A_y 円支給し、 x=y と更新する。
- そうでないなら終了する。
青木さんの今月の給料は、この手順によって支給された金額の合計です。
青木さんの今月の給料の期待値を {}\bmod{998244353} で求めてください。
期待値 {}\bmod{998244353} の定義
この問題で求める期待値は必ず有理数になることが証明できます。 また、この問題の制約下では、求める期待値を既約分数 \frac yx で表したときに x が 998244353 で割り切れないことが保証されます。 このとき、y\equiv xz\pmod{998244353} を満たす 0\leq z\lt998244353 がただ一つ存在するので、z を出力してください。制約
- 入力は全て整数
- 1 \le N \le 3 \times 10^5
- 0 \le A_i < 998244353
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
答えを出力せよ。
入力例 1
3 3 2 6
出力例 1
776412280
手順の一例は、以下の通りです。
- 最初、 x=0 である。
- ダイスを 1 度振り、出た目が 1 であった。 0<1 であるため、 A_1 = 3 円支給し、 x=1 とする。
- ダイスを 1 度振り、出た目が 3 であった。 1<3 であるため、 A_3 = 6 円支給し、 x=3 とする。
- ダイスを 1 度振り、出た目が 1 であった。 3 \ge 1 であるため、終了する。
この例では、青木さんの今月の給料は 9 円です。
なお、青木さんの今月の給料の期待値は \frac{49}{9} 円と求めることができ、これを {}\bmod{998244353} 上で表現すると 776412280 となります。
入力例 2
1 998244352
出力例 2
998244352
入力例 3
9 3 14 159 2653 58979 323846 2643383 27950288 419716939
出力例 3
545252774
Score : 450 points
Problem Statement
Aoki, an employee at AtCoder Inc., has his salary for this month determined by an integer N and a sequence A of length N as follows.
First, he is given an N-sided die (dice) that shows the integers from 1 to N with equal probability, and a variable x=0.
Then, the following steps are repeated until terminated.
- Roll the die once and let y be the result.
- If x<y, pay him A_y yen and let x=y.
- Otherwise, terminate the process.
Aoki's salary for this month is the total amount paid through this process.
Find the expected value of Aoki's salary this month, modulo 998244353.
How to find an expected value modulo 998244353
It can be proved that the sought expected value in this problem is always a rational number. Also, the constraints of this problem guarantee that if the sought expected value is expressed as a reduced fraction \frac yx, then x is not divisible by 998244353. Here, there is exactly one 0\leq z\lt998244353 such that y\equiv xz\pmod{998244353}. Print this z.Constraints
- All inputs are integers.
- 1 \le N \le 3 \times 10^5
- 0 \le A_i < 998244353
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Print the answer.
Sample Input 1
3 3 2 6
Sample Output 1
776412280
Here is an example of how the process goes.
- Initially, x=0.
- Roll the die once, and it shows 1. Since 0<1, pay him A_1 = 3 yen and let x=1.
- Roll the die once, and it shows 3. Since 1<3, pay him A_3 = 6 yen and let x=3.
- Roll the die once, and it shows 1. Since 3 \ge 1, terminate the process.
In this case, his salary for this month is 9 yen.
It can be calculated that the expected value of his salary this month is \frac{49}{9} yen, whose representation modulo 998244353 is 776412280.
Sample Input 2
1 998244352
Sample Output 2
998244352
Sample Input 3
9 3 14 159 2653 58979 323846 2643383 27950288 419716939
Sample Output 3
545252774
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 625 点
問題文
1 から N までの番号が付けられた N 枚のカードがあります。 カードのそれぞれの面には整数が書かれており、カード i の表には A_i が、裏には B_i が書かれています。 最初、全てのカードは表を向いています。
今ここに M 台のマシーンがあり、1 から M までの番号が付けられています。 マシーン j は(相異なるとは限らない)2 つの 1 以上 N 以下の整数 X_j,Y_j を持っており、マシーン j が起動されると、 \frac{1}{2} の確率でカード X_j を、残りの \frac{1}{2} の確率でカード Y_j を裏返します。 この確率は各起動ごとに独立です。
すぬけくんは今から以下の操作を順に行います。
- 1 以上 M 以下の整数からなる集合 S を選ぶ。
- S に含まれる番号のマシーンを、番号が小さい順に 1 度ずつ起動する。
すぬけくんがうまく S を選んだとき、「すべての操作が終了した後に各カードが向いている面に書かれた整数の合計」の期待値が最大でいくつになるか求めてください。
制約
- 1\leq N \leq 40
- 1\leq M \leq 10^5
- 1\leq A_i,B_i \leq 10^4
- 1\leq X_j,Y_j \leq N
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 \vdots A_N B_N X_1 Y_1 \vdots X_M Y_M
出力
答えを出力せよ。 出力は、真の値との絶対誤差または相対誤差が 10^{-6} 以下のとき正解と判定される。
入力例 1
3 1 3 10 10 6 5 2 1 2
出力例 1
19.500000
S として空集合を選んだ場合、どのマシーンも起動されないので、「すべての操作が終了した後に各カードが向いている面に書かれた整数の合計」の期待値は 3+10+5=18 です。
S として \lbrace 1 \rbrace を選んだ場合、マシーン 1 が起動され、
- カード X_1 = 1 が裏返された場合、「すべての操作が終了した後に各カードが向いている面に書かれた整数の合計」は 10+10+5=25
- カード Y_1 = 2 が裏返された場合、「すべての操作が終了した後に各カードが向いている面に書かれた整数の合計」は 3+6+5=14
なので、その期待値は \frac{25+14}{2} = 19.5 です。
よって、「すべての操作が終了した後に各カードが向いている面に書かれた整数の合計」の期待値の最大値は 19.5 です。
入力例 2
1 3 5 100 1 1 1 1 1 1
出力例 2
100.000000
同じ (X_j,Y_j) を持つマシーンが複数存在することもあります。
入力例 3
8 10 6918 9211 16 1868 3857 8537 3340 8506 6263 7940 1449 4593 5902 1932 310 6991 4 4 8 6 3 5 1 1 4 2 5 6 7 5 3 3 1 5 3 1
出力例 3
45945.000000
Score : 625 points
Problem Statement
There are N cards numbered 1 through N. Each face of a card has an integer written on it; card i has A_i on its front and B_i on its back. Initially, all cards are face up.
There are M machines numbered 1 through M. Machine j has two (not necessarily distinct) integers X_j and Y_j between 1 and N. If you power up machine j, it flips card X_j with the probability of \frac{1}{2}, and flips card Y_j with the remaining probability of \frac{1}{2}. This probability is independent for each power-up.
Snuke will perform the following procedure.
- Choose a set S consisting of integers from 1 through M.
- For each element in S in ascending order, power up the machine with that number.
Among Snuke's possible choices of S, find the maximum expected value of the sum of the integers written on the face-up sides of the cards after the procedure.
Constraints
- 1\leq N \leq 40
- 1\leq M \leq 10^5
- 1\leq A_i,B_i \leq 10^4
- 1\leq X_j,Y_j \leq N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 B_1 \vdots A_N B_N X_1 Y_1 \vdots X_M Y_M
Output
Print the answer. Your output is considered correct if the absolute or relative difference from the true value is at most 10^{-6}.
Sample Input 1
3 1 3 10 10 6 5 2 1 2
Sample Output 1
19.500000
If S is chosen to be an empty set, no machine is powered up, so the expected sum of the integers written on the face-up sides of the cards after the procedure is 3+10+5=18.
If S is chosen to be \lbrace 1 \rbrace, machine 1 is powered up.
- If card X_1 = 1 is flipped, the expected sum of the integers written on the face-up sides of the cards after the procedure is 10+10+5=25.
- If card Y_1 = 2 is flipped, the expected sum of the integers written on the face-up sides of the cards after the procedure is 3+6+5=14.
Thus, the expected value is \frac{25+14}{2} = 19.5.
Therefore, the maximum expected value of the sum of the integers written on the face-up sides of the cards after the procedure is 19.5.
Sample Input 2
1 3 5 100 1 1 1 1 1 1
Sample Output 2
100.000000
Different machines may have the same (X_j,Y_j).
Sample Input 3
8 10 6918 9211 16 1868 3857 8537 3340 8506 6263 7940 1449 4593 5902 1932 310 6991 4 4 8 6 3 5 1 1 4 2 5 6 7 5 3 3 1 5 3 1
Sample Output 3
45945.000000