Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
非負整数 K に対して、以下のようにレベル K のカーペットを定義します。
- レベル 0 のカーペットは黒いマス 1 個のみからなる 1\times 1 のグリッドである。
- K>0 のとき、レベル K のカーペットは 3^K\times 3^K のグリッドである。
このグリッドを 3^{K-1}\times 3^{K-1} のブロック 9 個に分割したとき、
- 中央のブロックはすべて白いマスからなる。
- 他の 8 個のブロックは、レベル (K-1) のカーペットである。
非負整数 N が与えられます。
レベル N のカーペットを出力の形式に従って出力してください。
制約
- 0\leq N \leq 6
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
3^N 行出力せよ。
i 行目 (1\leq i\leq 3^N) には、. と # からなる長さ 3^N の文字列 S_i を出力せよ。
S_i の j 文字目 (1\leq j\leq 3^N) は、レベル N のカーペットの上から i 行目かつ左から j 列目のマスが黒いとき # とし、白いとき . とせよ。
入力例 1
1
出力例 1
### #.# ###
レベル 1 のカーペットは次のような 3\times 3 のグリッドです。

これを出力形式にしたがって出力すると出力例のようになります。
入力例 2
2
出力例 2
######### #.##.##.# ######### ###...### #.#...#.# ###...### ######### #.##.##.# #########
レベル 2 のカーペットは 9\times 9 のグリッドとなります。
Score : 250 points
Problem Statement
For a non-negative integer K, we define a level-K carpet as follows:
- A level-0 carpet is a 1 \times 1 grid consisting of a single black cell.
- For K > 0, a level-K carpet is a 3^K \times 3^K grid. When this grid is divided into nine 3^{K-1} \times 3^{K-1} blocks:
- The central block consists entirely of white cells.
- The other eight blocks are level-(K-1) carpets.
You are given a non-negative integer N.
Print a level-N carpet according to the specified format.
Constraints
- 0 \leq N \leq 6
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Print 3^N lines.
The i-th line (1 \leq i \leq 3^N) should contain a string S_i of length 3^N consisting of . and #.
The j-th character of S_i (1 \leq j \leq 3^N) should be # if the cell at the i-th row from the top and j-th column from the left of a level-N carpet is black, and . if it is white.
Sample Input 1
1
Sample Output 1
### #.# ###
A level-1 carpet is a 3 \times 3 grid as follows:

When output according to the specified format, it looks like the sample output.
Sample Input 2
2
Sample Output 2
######### #.##.##.# ######### ###...### #.#...#.# ###...### ######### #.##.##.# #########
A level-2 carpet is a 9 \times 9 grid.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
座標平面上に 2\times1 の大きさのタイルが敷き詰められています。 タイルは、次の規則に従って敷き詰められています。
- 整数の組 (i,j) に対し、正方形 A _ {i,j}=\lbrace(x,y)\mid i\leq x\leq i+1\wedge j\leq y\leq j+1\rbrace は 1 つのタイルに含まれる。
- i+j が偶数のとき、A _ {i,j} と A _ {i + 1,j} は同じタイルに含まれる。
ただし、タイルは境界を含むものとし、共通部分が正の面積をもつような 2 つの異なるタイルは存在しないとします。
原点の近くでは、タイルは以下のように敷き詰められています。

高橋君は、はじめ座標平面上の点 (S _ x+0.5,S _ y+0.5) にいます。
高橋君は、次の移動を好きなだけ繰り返します。
- 上下左右の方向と正の整数 n を選ぶ。その方向に n だけ進む。
高橋君が異なるタイルを通るたび、高橋君は通行料を 1 だけ支払います。
高橋君が点 (T _ x+0.5,T _ y+0.5) にたどり着くために支払わなければならない通行料の最小値を求めてください。
制約
- 0\leq S _ x\leq2\times10 ^ {16}
- 0\leq S _ y\leq2\times10 ^ {16}
- 0\leq T _ x\leq2\times10 ^ {16}
- 0\leq T _ y\leq2\times10 ^ {16}
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
S _ x S _ y T _ x T _ y
出力
高橋君が支払わなければならない通行料の最小値を出力せよ。
入力例 1
5 0 2 5
出力例 1
5
例えば、以下のように移動することで支払う通行料を 5 にすることができます。

- 左に 1 進む。通行料を 0 支払う。
- 上に 1 進む。通行料を 1 支払う。
- 左に 1 進む。通行料を 0 支払う。
- 上に 3 進む。通行料を 3 支払う。
- 左に 1 進む。通行料を 0 支払う。
- 上に 1 進む。通行料を 1 支払う。
支払う通行料を 4 以下にすることはできないので、5 を出力してください。
入力例 2
3 1 4 1
出力例 2
0
通行料を支払わなくてよい場合もあります。
入力例 3
2552608206527595 5411232866732612 771856005518028 7206210729152763
出力例 3
1794977862420151
出力すべき値が 32\operatorname{bit} 整数の範囲に収まらない場合があることに注意してください。
Score : 350 points
Problem Statement
The coordinate plane is covered with 2\times1 tiles. The tiles are laid out according to the following rules:
- For an integer pair (i,j), the square A _ {i,j}=\lbrace(x,y)\mid i\leq x\leq i+1\wedge j\leq y\leq j+1\rbrace is contained in one tile.
- When i+j is even, A _ {i,j} and A _ {i + 1,j} are contained in the same tile.
Tiles include their boundaries, and no two different tiles share a positive area.
Near the origin, the tiles are laid out as follows:

Takahashi starts at the point (S _ x+0.5,S _ y+0.5) on the coordinate plane.
He can repeat the following move as many times as he likes:
- Choose a direction (up, down, left, or right) and a positive integer n. Move n units in that direction.
Each time he enters a tile, he pays a toll of 1.
Find the minimum toll he must pay to reach the point (T _ x+0.5,T _ y+0.5).
Constraints
- 0\leq S _ x\leq2\times10 ^ {16}
- 0\leq S _ y\leq2\times10 ^ {16}
- 0\leq T _ x\leq2\times10 ^ {16}
- 0\leq T _ y\leq2\times10 ^ {16}
- All input values are integers.
Input
The input is given from Standard Input in the following format:
S _ x S _ y T _ x T _ y
Output
Print the minimum toll Takahashi must pay.
Sample Input 1
5 0 2 5
Sample Output 1
5
For example, Takahashi can pay a toll of 5 by moving as follows:

- Move left by 1. Pay a toll of 0.
- Move up by 1. Pay a toll of 1.
- Move left by 1. Pay a toll of 0.
- Move up by 3. Pay a toll of 3.
- Move left by 1. Pay a toll of 0.
- Move up by 1. Pay a toll of 1.
It is impossible to reduce the toll to 4 or less, so print 5.
Sample Input 2
3 1 4 1
Sample Output 2
0
There are cases where no toll needs to be paid.
Sample Input 3
2552608206527595 5411232866732612 771856005518028 7206210729152763
Sample Output 3
1794977862420151
Note that the value to be output may exceed the range of a 32-bit integer.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 425 点
問題文
あなたは最初、空文字列 S を持っています。
さらに、文字列がいくつか入った袋 1,2,\dots,N があります。
袋 i には A_i 個の文字列 S_{i,1},S_{i,2},\dots,S_{i,A_i} が入っています。
これから、以下の手順を i=1,2,\dots,N について繰り返します。
- 以下のふたつの行動のうち、どちらかを選択して行う。
- 1 円を支払い、袋 i からちょうどひとつの文字列を選択して S の末尾に連結する。
- 何もしない。
文字列 T が与えられるとき、最終的に S と T を一致させるために必要な最小の金額を求めてください。
但し、どのようにしても最終的な S を T に一致させることができない場合、 -1 と出力してください。
制約
- T は長さ 1 以上 100 以下の英小文字からなる文字列
- N は 1 以上 100 以下の整数
- A_i は 1 以上 10 以下の整数
- S_{i,j} は長さ 1 以上 10 以下の英小文字からなる文字列
入力
入力は以下の形式で標準入力から与えられる。
T
N
A_1 S_{1,1} S_{1,2} \dots S_{1,A_1}
A_2 S_{2,1} S_{2,2} \dots S_{2,A_2}
\vdots
A_N S_{N,1} S_{N,2} \dots S_{N,A_N}
出力
答えを整数として出力せよ。
入力例 1
abcde 3 3 ab abc abcd 4 f c cd bcde 2 e de
出力例 1
2
例えば、以下のようにすると 2 円で最終的な S と T を一致させることができ、これが必要な金額の最低値であることが示せます。
- i=1 について、袋 1 から
abcを選択し S の末尾に連結する。 S=abcとなる。 - i=2 について、何もしない。
- i=3 について、袋 3 から
deを選択し S の末尾に連結する。 S=abcdeとなる。
入力例 2
abcde 3 2 ab abc 3 f c bcde 1 e
出力例 2
-1
どのようにしても最終的な S と T を一致させることができないので、 -1 と出力してください。
入力例 3
aaabbbbcccc 6 2 aa aaa 2 dd ddd 2 ab aabb 4 bbaa bbbc bbb bbcc 2 cc bcc 3 ccc cccc ccccc
出力例 3
4
Score: 425 points
Problem Statement
You initially have an empty string S.
Additionally, there are bags 1, 2, \dots, N, each containing some strings.
Bag i contains A_i strings S_{i,1}, S_{i,2}, \dots, S_{i,A_i}.
You will repeat the following steps for i = 1, 2, \dots, N:
- Choose and perform one of the following two actions:
- Pay 1 yen, select exactly one string from bag i, and concatenate it to the end of S.
- Do nothing.
Given a string T, find the minimum amount of money required to make the final S equal T.
If there is no way to make the final S equal T, print -1.
Constraints
- T is a string consisting of lowercase English letters with length between 1 and 100, inclusive.
- N is an integer between 1 and 100, inclusive.
- A_i is an integer between 1 and 10, inclusive.
- S_{i,j} is a string consisting of lowercase English letters with length between 1 and 10, inclusive.
Input
The input is given from Standard Input in the following format:
T
N
A_1 S_{1,1} S_{1,2} \dots S_{1,A_1}
A_2 S_{2,1} S_{2,2} \dots S_{2,A_2}
\vdots
A_N S_{N,1} S_{N,2} \dots S_{N,A_N}
Output
Print the answer as an integer.
Sample Input 1
abcde 3 3 ab abc abcd 4 f c cd bcde 2 e de
Sample Output 1
2
For example, doing the following makes the final S equal T with two yen, which can be shown to be the minimum amount required.
- For i=1, select
abcfrom bag 1 and concatenate it to the end of S, making S=abc. - For i=2, do nothing.
- For i=3, select
defrom bag 3 and concatenate it to the end of S, making S=abcde.
Sample Input 2
abcde 3 2 ab abc 3 f c bcde 1 e
Sample Output 2
-1
There is no way to make the final S equal T, so print -1.
Sample Input 3
aaabbbbcccc 6 2 aa aaa 2 dd ddd 2 ab aabb 4 bbaa bbbc bbb bbcc 2 cc bcc 3 ccc cccc ccccc
Sample Output 3
4
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 475 点
問題文
N 枚のカードが入ったパックが無限にあります。各パックについて、i 枚目に入っているカードは P_i\% の確率でレアです。また、各カードがレアであることは他のカードがレアであることと独立です。
これからパックを一つずつ開封していき、開封したパックに入っているすべてのカードを手に入れます。レアカードを合計 X 枚以上手に入れるまでパックを開封するとき、開封するパックの個数の期待値を求めてください。
制約
- 1 \leq N \leq 5000
- 1 \leq X \leq 5000
- 1 \leq P_i \leq 100
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N X P_1 P_2 \ldots P_N
出力
答えを出力せよ。
真の答えとの絶対誤差または相対誤差が 10^{-6} 以下ならば正解と判定される。
入力例 1
2 2 50 100
出力例 1
1.5000000000000000
開封するパックの個数は \frac{1}{2} の確率で 1 個、\frac{1}{2} の確率で 2 個となります。
入力例 2
2 3 40 60
出力例 2
3.2475579530543811
入力例 3
6 3 10 33 33 10 100 10
出力例 3
1.8657859189536100
Score: 475 points
Problem Statement
There are infinitely many packs, each containing N cards. In each pack, the i-th card is rare with probability P_i percent. Whether each card is rare is independent of other cards being rare.
You will now open the packs one by one, and obtain all the cards in each opened pack. When you keep opening packs until you have obtained a total of at least X rare cards, find the expected number of packs you will open.
Constraints
- 1 \leq N \leq 5000
- 1 \leq X \leq 5000
- 1 \leq P_i \leq 100
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N X P_1 P_2 \ldots P_N
Output
Print the answer.
Your answer will be considered correct if the absolute or relative error from the true answer is at most 10^{-6}.
Sample Input 1
2 2 50 100
Sample Output 1
1.5000000000000000
The number of packs opened will be 1 with probability \frac{1}{2}, and 2 with probability \frac{1}{2}.
Sample Input 2
2 3 40 60
Sample Output 2
3.2475579530543811
Sample Input 3
6 3 10 33 33 10 100 10
Sample Output 3
1.8657859189536100
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
長さ N の英小文字からなる文字列 S,T が与えられます。
文字列 X と整数 i に対し、f(X,i) を X に対して以下の操作を i 回行い得られる文字列とします。
- X の先頭の文字を削除し、同じ文字を X の末尾に挿入する。
0 \le i,j \le N-1 を満たす正整数の組 (i,j) のうち、辞書順で f(S,i) が f(T,j) より小さいか同じであるものの個数を求めてください。
辞書順とは?
辞書順とは簡単に説明すると「単語が辞書に載っている順番」を意味します。より厳密な説明として、英小文字からなる相異なる文字列 S, T の大小を判定するアルゴリズムを以下に説明します。
以下では「 S の i 文字目の文字」を S_i のように表します。また、 S が T より辞書順で小さい場合は S \lt T 、大きい場合は S \gt T と表します。
- S, T のうち長さが大きくない方の文字列の長さを L とします。i=1,2,\dots,L に対して S_i と T_i が一致するか調べます。
- S_i \neq T_i である i が存在する場合、そのような i のうち最小のものを j とします。そして、S_j と T_j を比較して、S_j が T_j よりアルファベット順で小さい場合は S \lt T 、そうでない場合は S \gt T と決定して、アルゴリズムを終了します。
- S_i \neq T_i である i が存在しない場合、S と T の長さを比較して、S が T より短い場合は S \lt T 、長い場合は S \gt T と決定して、アルゴリズムを終了します。
制約
- 1 \le N \le 2 \times 10^5
- S,T は英小文字からなる長さ N の文字列
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N S T
出力
答えを出力せよ。
入力例 1
3 adb cab
出力例 1
4
条件を満たす (i,j) の組は (0,0),(0,2),(2,0),(2,2) の 4 個があります。
(i,j)=(1,2) は、f(S,i)=dba,f(T,j)=bca であるため条件を満たしません。
入力例 2
10 wsiuhwijsl pwqoketvun
出力例 2
56
Score : 500 points
Problem Statement
You are given strings S and T of length N each, consisting of lowercase English letters.
For a string X and an integer i, let f(X,i) be the string obtained by performing on X the following operation i times:
- Remove the first character of X and append the same character to the end of X.
Find the number of integer pairs (i,j) satisfying 0 \le i,j \le N-1 such that f(S,i) is lexicographically smaller than or equal to f(T,j).
What is the lexicographical order?
Simply put, the lexicographical order is the order in which the words are arranged in a dictionary. Let us define it formally by describing an algorithm of finding the ordering of two distinct strings S and T consisting of lowercase English letters.
Here, we denote "the i-th character of a string S" by S_i. Also, we write S \lt T and S \gt T if S is lexicographically smaller and larger than T, respectively.
- Let L be the smallest of the lengths of S and T. For i=1,2,\dots,L, we check if S_i equals T_i.
- If there is an i such that S_i \neq T_i, let j be the smallest such i. Comparing S_j and T_j, we terminate the algorithm by determining that S \lt T if S_j is smaller than T_j in the alphabetical order, and that S \gt T otherwise.
- If there is no i such that S_i \neq T_i, comparing the lengths of S and T, we terminate the algorithm by determining that S \lt T if S is shorter than T, and that S \gt T otherwise.
Constraints
- 1 \le N \le 2 \times 10^5
- S and T are strings of length N each, consisting of lowercase English letters.
- N is an integer.
Input
The input is given from Standard Input in the following format:
N S T
Output
Print the answer.
Sample Input 1
3 adb cab
Sample Output 1
4
There are 4 pairs of (i, j) satisfying the conditions: (0,0),(0,2),(2,0), and (2,2).
(i,j)=(1,2) does not satisfy the conditions because f(S,i)=dba and f(T,j)=bca.
Sample Input 2
10 wsiuhwijsl pwqoketvun
Sample Output 2
56