Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
英小文字からなる文字列 S と正の整数 N が与えられます。S の長さは 2N+1 以上です。
S の先頭と末尾から N 文字ずつ取り除いて得られる文字列を求めてください。
制約
- S は英小文字からなる文字列
- N は整数
- 2N+1 \leq |S| \leq 30
- 1 \leq N \leq 10
入力
入力は以下の形式で標準入力から与えられる。
S N
出力
答えを出力せよ。
入力例 1
chemotherapy 3
出力例 1
mother
chemotherapy の先頭 3 文字 (che) と末尾 3 文字 (apy) を取り除くと、mother が得られます。
入力例 2
thermometer 4
出力例 2
mom
入力例 3
burger 1
出力例 3
urge
Score : 100 points
Problem Statement
You are given a string S consisting of lowercase English letters and a positive integer N. The length of S is at least 2N+1.
Find the string obtained by removing N characters from the beginning and N characters from the end of S.
Constraints
- S is a string consisting of lowercase English letters.
- N is an integer.
- 2N+1 \leq |S| \leq 30
- 1 \leq N \leq 10
Input
The input is given from Standard Input in the following format:
S N
Output
Output the answer.
Sample Input 1
chemotherapy 3
Sample Output 1
mother
Removing the first three characters (che) and the last three characters (apy) from chemotherapy gives mother.
Sample Input 2
thermometer 4
Sample Output 2
mom
Sample Input 3
burger 1
Sample Output 3
urge
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
高橋君のケーキが誰かに食べられてしまいました。ケーキを食べた犯人の候補として、人 1、人 2、人 3 の三人が挙げられています。
犯人の目撃者はりんごさんとすぬけくんの二人がいます。りんごさんは人 A が犯人でないことを覚えており、すぬけくんは人 B が犯人でないことを覚えています。
二人の記憶から犯人を一人に特定できるかどうか判定し、特定できるならばその人の番号を出力してください。
制約
- 1\leq A,B\leq 3
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
A B
出力
二人の記憶から犯人を一人に特定できるならばその人の番号を出力し、特定できないならば -1 を出力せよ。
入力例 1
1 2
出力例 1
3
二人の記憶から、人 3 が犯人であることが特定できます。
入力例 2
1 1
出力例 2
-1
二人の記憶からでは、人 2,3 のどちらが犯人か特定することができません。よって -1 を出力します。
入力例 3
3 1
出力例 3
2
Score : 100 points
Problem Statement
Takahashi's cake has been eaten by someone. There are three suspects: person 1, person 2, and person 3.
There are two witnesses, Ringo and Snuke. Ringo remembers that person A is not the culprit, and Snuke remembers that person B is not the culprit.
Determine if the culprit can be uniquely identified based on the memories of the two witnesses. If the culprit can be identified, print the person's number.
Constraints
- 1 \leq A, B \leq 3
- All input values are integers.
Input
The input is given from Standard Input in the following format:
A B
Output
If the culprit can be uniquely identified based on the memories of the two witnesses, print the person's number; otherwise, print -1.
Sample Input 1
1 2
Sample Output 1
3
From the memories of the two witnesses, it can be determined that person 3 is the culprit.
Sample Input 2
1 1
Sample Output 2
-1
From the memories of the two witnesses, it cannot be determined whether person 2 or person 3 is the culprit. Therefore, print -1.
Sample Input 3
3 1
Sample Output 3
2
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
N 人の競技プログラマーがいます。順に 人 1, 人 2, \dots, 人 N と呼びます。
競技プログラマーの間には 強さ と呼ばれる関係性があり、相異なる 2 人組 (人 X, 人 Y) 全てに対して 「人 X は人 Y より強い」または「人 Y は人 X より強い」のどちらか一方が成り立ちます。
強さ は 推移律 が成り立ちます。言い換えると、相異なる 3 人組 (人 X, 人 Y, 人 Z) 全てに対して次の条件が成り立ちます。
- 人 X が人 Y よりも強く、かつ人 Y が人 Z よりも強いとき、人 X は人 Z よりも強い。
人 X が自分以外のどの人 Y に対しても「人 X は人 Y より強い」という関係が成り立つ時、人 X を 最強のプログラマー と呼びます。(上記の制約下においてそのような人がちょうど 1 人存在することが証明できます)
あなたは M 個の強さに関する情報を知っています。i 個目の情報は「人 A_i は人 B_i より強い」という情報です。
あなたは情報を元に N 人の中から最強のプログラマーを特定することができますか?
最強のプログラマーを特定できる場合はその人の番号を出力してください。特定できない場合、つまり最強のプログラマーとしてあり得る人が複数人いる場合は -1 を出力してください。
制約
- 2 \leq N \leq 50
- 0 \leq M \leq \frac{N(N-1)}{2}
- 1 \leq A_i, B_i \leq N
- A_i \neq B_i
- i \neq j ならば (A_i, B_i) \neq (A_j, B_j)
- 全ての情報が正しくなるように、全ての相異なる 2 人組にどちらが強いかを割り当てる方法が少なくとも 1 通り存在する
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 A_2 B_2 \vdots A_M B_M
出力
最強のプログラマーを特定できる場合はその人の番号を出力せよ。特定できない場合は -1 を出力せよ。
入力例 1
3 2 1 2 2 3
出力例 1
1
あなたは「人 1 は人 2 より強い」「人 2 は人 3 より強い」という情報が分かっています。
推移律から「人 1 は人 3 より強い」という情報もわかるので、人 1 は最強のプログラマーです。
入力例 2
3 2 1 3 2 3
出力例 2
-1
最強のプログラマーである可能性がある人は人 1 と人 2 です。最強のプログラマーを一意に特定できないので -1 を出力してください。
入力例 3
6 6 1 6 6 5 6 2 2 3 4 3 4 2
出力例 3
-1
Score : 300 points
Problem Statement
There are N competitive programmers numbered person 1, person 2, \ldots, and person N.
There is a relation called superiority between the programmers. For all pairs of distinct programmers (person X, person Y), exactly one of the following two relations holds: "person X is stronger than person Y" or "person Y is stronger than person X."
The superiority is transitive. In other words, for all triplets of distinct programmers (person X, person Y, person Z), it holds that:
- if person X is stronger than person Y and person Y is stronger than person Z, then person X is stronger than person Z.
A person X is said to be the strongest programmer if person X is stronger than person Y for all people Y other than person X. (Under the constraints above, we can prove that there is always exactly one such person.)
You have M pieces of information on their superiority. The i-th of them is that "person A_i is stronger than person B_i."
Can you determine the strongest programmer among the N based on the information?
If you can, print the person's number. Otherwise, that is, if there are multiple possible strongest programmers, print -1.
Constraints
- 2 \leq N \leq 50
- 0 \leq M \leq \frac{N(N-1)}{2}
- 1 \leq A_i, B_i \leq N
- A_i \neq B_i
- If i \neq j, then (A_i, B_i) \neq (A_j, B_j).
- There is at least one way to determine superiorities for all pairs of distinct programmers, that is consistent with the given information.
Input
The input is given from Standard Input in the following format:
N M A_1 B_1 A_2 B_2 \vdots A_M B_M
Output
If you can uniquely determine the strongest programmer, print the person's number; otherwise, print -1.
Sample Input 1
3 2 1 2 2 3
Sample Output 1
1
You have two pieces of information: "person 1 is stronger than person 2" and "person 2 is stronger than person 3."
By the transitivity, you can also infer that "person 1 is stronger than person 3," so person 1 is the strongest programmer.
Sample Input 2
3 2 1 3 2 3
Sample Output 2
-1
Both person 1 and person 2 may be the strongest programmer. Since you cannot uniquely determine which is the strongest, you should print -1.
Sample Input 3
6 6 1 6 6 5 6 2 2 3 4 3 4 2
Sample Output 3
-1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
正整数 n の桁和を、 n を十進法で表したときの各桁の和と定めます。例えば 2026 の桁和は 2+0+2+6=10 です。
N 以下の正整数のうち、桁和が K であるものの個数を求めてください。
制約
- 1\leq N,K \leq 10^5
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K
出力
答えを出力せよ。
入力例 1
30 4
出力例 1
3
30 以下の正整数のうち、桁和が 4 であるものは、4,13,22 の 3 個です。
入力例 2
2026 10
出力例 2
121
入力例 3
99999 45
出力例 3
1
Score : 200 points
Problem Statement
The digit sum of a positive integer n is defined as the sum of the digits when n is represented in decimal. For example, the digit sum of 2026 is 2+0+2+6=10.
Find the number of positive integers not exceeding N whose digit sum is K.
Constraints
- 1\leq N,K \leq 10^5
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K
Output
Output the answer.
Sample Input 1
30 4
Sample Output 1
3
Among the positive integers not exceeding 30, there are three integers whose digit sum is 4: 4,13,22.
Sample Input 2
2026 10
Sample Output 2
121
Sample Input 3
99999 45
Sample Output 3
1
Time Limit: 2 sec / Memory Limit: 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
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
英大文字のみからなる文字列 S が与えられます。
S に対して、次の手順を行った後に得られる文字列を出力してください。
文字列が
WAを(連続する)部分文字列として含む限り、次の操作を繰り返す。
- 文字列中に登場する
WAのうち最も先頭のものをACに置換する。
なお、本問題の制約下で、この操作は高々有限回しか繰り返されないことが証明できます。
制約
- S は長さ 1 以上 3\times 10^5 以下の英大文字のみからなる文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S に対して、問題文中に記された手順を行った後に得られる文字列を出力せよ。
入力例 1
WACWA
出力例 1
ACCAC
最初、文字列は S=WACWA です。
この文字列には 1 文字目から 2 文字目、および、4 文字目から 5 文字目の 2 ヶ所に WA が部分文字列として含まれています。
1 回目の操作では、そのうち先頭のもの、すなわち 1 文字目から 2 文字目の部分を AC に置換し、文字列は ACCWA となります。
1 回目の操作後の文字列には 4 文字目から 5 文字目の 1 ヶ所にのみ WA が部分文字列として含まれているため、2 回目の操作ではこれを AC に置換し、文字列は ACCAC となります。
ACCAC は WA を部分文字列として含まないため、手順は終了します。よって、ACCAC を出力します。
入力例 2
WWA
出力例 2
ACC
最初、文字列は S=WWA です。
この文字列には 2 文字目から 3 文字目の 1 ヶ所にのみ WA が部分文字列として含まれているため、1 回目の操作ではこれを AC に置換し、文字列は WAC となります。
次に、1 回目の操作後の文字列は 1 文字目から 2 文字目の 1 ヶ所にのみ WA が部分文字列として含まれているため、2 回目の操作ではこれを AC に置換し、文字列は ACC となります。
ACC は WA を部分文字列として含まないため、手順は終了します。よって、ACC を出力します。
入力例 3
WWWWW
出力例 3
WWWWW
S には最初から WA が部分文字列として含まれてないため、操作は 1 回も行われず手順は終了します。よって、WWWWW を出力します。
Score : 300 points
Problem Statement
You are given a string S consisting of uppercase English letters.
Apply the following procedure to S, and then output the resulting string:
As long as the string contains
WAas a (contiguous) substring, repeat the following operation:
- Among all occurrences of
WAin the string, replace the leftmost one withAC.
It can be proved under the constraints of this problem that this operation is repeated at most a finite number of times.
Constraints
- S is a string of uppercase English letters with length between 1 and 3\times 10^5, inclusive.
Input
The input is given from Standard Input in the following format:
S
Output
Print the resulting string after performing the procedure described in the problem statement on S.
Sample Input 1
WACWA
Sample Output 1
ACCAC
Initially, the string is S= WACWA.
This string contains WA as a substring in two places: from the 1st to the 2nd character, and from the 4th to the 5th character.
In the first operation, we replace the leftmost occurrence (the substring from the 1st to the 2nd character) with AC, resulting in ACCWA.
After the first operation, the string contains WA as a substring in exactly one place: from the 4th to the 5th character.
In the second operation, we replace it with AC, resulting in ACCAC.
Since ACCAC does not contain WA as a substring, the procedure ends. Therefore, we output ACCAC.
Sample Input 2
WWA
Sample Output 2
ACC
Initially, the string is S= WWA.
This string contains WA as a substring in exactly one place: from the 2nd to the 3rd character.
In the first operation, we replace it with AC, resulting in WAC.
Then, after the first operation, the string contains WA in exactly one place: from the 1st to the 2nd character.
In the second operation, we replace it with AC, resulting in ACC.
Since ACC does not contain WA as a substring, the procedure ends. Therefore, we output ACC.
Sample Input 3
WWWWW
Sample Output 3
WWWWW
Since S does not contain WA as a substring from the start, no operations are performed and the procedure ends immediately. Therefore, we output WWWWW.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 425 点
問題文
AtCoder 王国では、1 ドル紙幣と K ドル紙幣の 2 種類の紙幣が流通しています。
AtCoder 王国にある J 社の全自動食堂では、デザートやドリンクが商品として売られています。
J 社の全自動食堂には、N 個のデザートを売るデザート販売機と、M 個のドリンクを売るドリンク販売機があります。 デザートには 1 から N までの、ドリンクには 1 から M までの番号が付けられています。
デザート i の価格は A_i ドル、ドリンク j の価格は B_j ドルです。
支払いに使う紙幣として、デザート販売機は 1 ドル紙幣と K ドル紙幣の両方を受け入れますが、ドリンク販売機は K ドル紙幣しか受け入れません。 どちらの販売機も、お釣りはすべて 1 ドル紙幣のみを使って返します。 同じ商品を 2 個以上買うことはできません。
高橋君は 1 ドル紙幣 X 枚と K ドル紙幣 Y 枚を持って J 社の全自動食堂にやってきました。
高橋君が手持ちの紙幣で買える商品の組合せにおいて、購入できる商品数の最大値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 2 \leq K \leq 10^9
- 0 \leq X \leq 10^{15}
- 0 \leq Y \leq 10^9
- 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq B_j \leq 10^9 (1 \leq j \leq M)
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M K X Y A_1 \dots A_N B_1 \dots B_M
出力
答えを出力せよ。
入力例 1
2 3 10 50 6 22 30 20 12 24
出力例 1
4
以下の手順で買い物を行うことで、4 個の商品を買うことができます。
- 10 ドル紙幣 2 枚を払ってドリンク 1 を買う。お釣りは無し。
- 10 ドル紙幣 2 枚を払ってドリンク 2 を買う。お釣りは 1 ドル紙幣 8 枚。
- 10 ドル紙幣 2 枚と 1 ドル紙幣 10 枚を払ってデザート 2 を買う。お釣りは無し。
- 1 ドル紙幣 22 枚を払ってデザート 1 を買う。お釣りは無し。
4 個よりも多く買うことはできないので、4 が答えとなります。
入力例 2
1 7 67 677677677766666 0 777666777 20 12 24 67 67 67 67
出力例 2
1
以下の手順で買い物を行うことで、1 個の商品を買うことができます。
- 1 ドル紙幣 600000777666777 枚を払ってデザート 1 を買う。お釣りは 1 ドル紙幣 600000000000000 枚。
1 個よりも多く買うことはできないので、1 が答えとなります。
入力例 3
20 20 30 605776135 133105105 97363214 218434035 697895427 109255624 299037330 227873982 195540071 411713803 828357845 244535208 138059186 639510883 39844882 707397687 371274487 696536603 351588202 319490007 47121612 87169661 32256972 567982330 554885983 299718223 443859449 687952877 264684780 666659381 576335424 941894234 406248934 321334900 423472560 863738035 213143887 384834384 468161291 673106162 164648316 15903323
出力例 3
22
Score : 425 points
Problem Statement
In the Kingdom of AtCoder, two kinds of bills are in circulation: 1-dollar bills and K-dollar bills.
At Company J's fully automated cafeteria in the kingdom, desserts and drinks are sold as products.
The company's fully automated cafeteria has a dessert vending machine that sells N desserts, and a drink vending machine that sells M drinks. The desserts are numbered 1 through N, and the drinks are numbered 1 through M.
Dessert i costs A_i dollars, and drink j costs B_j dollars.
As payment, the dessert vending machine accepts both 1-dollar bills and K-dollar bills, but the drink vending machine accepts only K-dollar bills. Both vending machines give change using only 1-dollar bills. You cannot buy two or more of the same product.
Takahashi came to Company J's fully automated cafeteria with X 1-dollar bills and Y K-dollar bills.
Among the combinations of products that he can buy with the bills he has, find the maximum number of products he can purchase.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 2 \leq K \leq 10^9
- 0 \leq X \leq 10^{15}
- 0 \leq Y \leq 10^9
- 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq B_j \leq 10^9 (1 \leq j \leq M)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M K X Y A_1 \dots A_N B_1 \dots B_M
Output
Output the answer.
Sample Input 1
2 3 10 50 6 22 30 20 12 24
Sample Output 1
4
By shopping as follows, Takahashi can buy four products.
- Pay two 10-dollar bills to buy drink 1. No change.
- Pay two 10-dollar bills to buy drink 2. The change is eight 1-dollar bills.
- Pay two 10-dollar bills and ten 1-dollar bills to buy dessert 2. No change.
- Pay twenty-two 1-dollar bills to buy dessert 1. No change.
He cannot buy more than four products, so the answer is 4.
Sample Input 2
1 7 67 677677677766666 0 777666777 20 12 24 67 67 67 67
Sample Output 2
1
By shopping as follows, Takahashi can buy one product.
- Pay 600000777666777 1-dollar bills to buy dessert 1. The change is 600000000000000 1-dollar bills.
He cannot buy more than one product, so the answer is 1.
Sample Input 3
20 20 30 605776135 133105105 97363214 218434035 697895427 109255624 299037330 227873982 195540071 411713803 828357845 244535208 138059186 639510883 39844882 707397687 371274487 696536603 351588202 319490007 47121612 87169661 32256972 567982330 554885983 299718223 443859449 687952877 264684780 666659381 576335424 941894234 406248934 321334900 423472560 863738035 213143887 384834384 468161291 673106162 164648316 15903323
Sample Output 3
22
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 475 点
問題文
(1,2,\ldots,N) の並べ替え P=(P _ 1,P _ 2,\ldots,P _ N) が与えられます。
次の操作を K 回行います。
- i=1,2,\ldots,N に対して同時に P _ i を P _ {P _ i} で更新する
すべての操作を終えたあとの P を出力してください。
制約
- 1\leq N\leq2\times10 ^ 5
- 1\leq K\leq10 ^ {18}
- 1\leq P _ i\leq N\ (1\leq i\leq N)
- P _ i\neq P _ j\ (1\leq i\lt j\leq N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K P _ 1 P _ 2 \ldots P _ N
出力
操作をすべて行ったあとの P について、P _ 1,P _ 2,\ldots,P _ N をこの順に空白を区切りとして出力せよ。
入力例 1
6 3 5 6 3 1 2 4
出力例 1
6 1 3 2 4 5
それぞれの操作によって、P は次のように変化します。
- 1 回目の操作の結果、P=(2,4,3,5,6,1) となります。
- 2 回目の操作の結果、P=(4,5,3,6,1,2) となります。
- 3 回目の操作の結果、P=(6,1,3,2,4,5) となります。
よって、6 1 3 2 4 5 を出力してください。
入力例 2
5 1000000000000000000 1 2 3 4 5
出力例 2
1 2 3 4 5
P _ i=i なので、何度操作を行っても P は変化しません。
入力例 3
29 51912426 7 24 8 23 6 1 4 19 11 18 20 9 17 28 22 27 15 2 12 26 10 13 14 25 5 29 3 21 16
出力例 3
18 23 16 24 21 10 2 27 19 7 12 8 13 5 15 26 17 4 3 9 1 22 25 14 28 11 29 6 20
Score : 475 points
Problem Statement
You are given a permutation P=(P_1,P_2,\ldots,P_N) of (1,2,\ldots,N).
The following operation will be performed K times:
- For i=1,2,\ldots,N, simultaneously update P_i to P_{P_i}.
Print P after all operations.
Constraints
- 1\leq N\leq2\times10^5
- 1\leq K\leq10^{18}
- 1\leq P_i\leq N\ (1\leq i\leq N)
- P_i\neq P_j\ (1\leq i\lt j\leq N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K P_1 P_2 \ldots P_N
Output
For the P after all operations, print P_1,P_2,\ldots,P_N in this order, separated by spaces.
Sample Input 1
6 3 5 6 3 1 2 4
Sample Output 1
6 1 3 2 4 5
With each operation, P changes as follows:
- After the first operation, P is (2,4,3,5,6,1).
- After the second operation, P is (4,5,3,6,1,2).
- After the third operation, P is (6,1,3,2,4,5).
Thus, print 6 1 3 2 4 5.
Sample Input 2
5 1000000000000000000 1 2 3 4 5
Sample Output 2
1 2 3 4 5
Since P_i=i, P does not change no matter how many operations are performed.
Sample Input 3
29 51912426 7 24 8 23 6 1 4 19 11 18 20 9 17 28 22 27 15 2 12 26 10 13 14 25 5 29 3 21 16
Sample Output 3
18 23 16 24 21 10 2 27 19 7 12 8 13 5 15 26 17 4 3 9 1 22 25 14 28 11 29 6 20
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 550 点
問題文
N 個の押しボタンがあります。このうち 1 個が当たりで、残りの N-1 個はハズレです。
青木君はどのボタンが当たりであるか知っており、高橋君は知りません。また高橋君には N 個のボタンの区別はつきません。
このボタンを使って青木君と高橋君がゲームを行います。ゲームは以下の一連の流れの T 回の繰り返しからなります。
- 青木君が N 個のボタンをランダムな順序に並べる
- 高橋君が「ボタンを 1 つ選び、そのボタンを押す」という操作を M 回行う。同じボタンを複数回選んでもよい
- ゲーム開始時からこれまでに当たりのボタンが何回押されたかを青木君が高橋君に教える
T 回の繰り返しで、当たりのボタンを合計 K 回以上押すことができたとき、かつ、そのときに限り高橋君の勝ちです。
勝つ確率が最大になるように高橋君が行動したときの、高橋君の勝率を求めてください。
制約
- 1 \leq N \leq 2\times 10^5
- 1 \leq T \leq 30
- 1 \leq M \leq 30
- 1 \leq K \leq 30
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N T M K
出力
答えを出力せよ。 真の解との絶対誤差または相対誤差が 10^{-6} 以下のとき正解と判定される。
入力例 1
3 2 2 3
出力例 1
0.222222222222222
ゲームは例えば次のように進行します。(最適な行動とは限りません)
- 1回目の繰り返し
- 青木君がボタンをランダムに並び替え、当たりのボタンが 1 番目 、ハズレのボタンが 2,3 番目となる
- 高橋君が 1 番目のボタンを押す
- 高橋君が 2 番目のボタンを押す
- 当たりのボタンが 1 度押されたことを青木君が高橋君に伝える
- 2回目の繰り返し
- 青木君がボタンをランダムに並び替え、当たりのボタンが 3 番目 、ハズレのボタンが 1,2 番目となる
- 高橋君が 3 番目のボタンを押す
- 高橋君が 3 番目のボタンを押す
- 当たりのボタンが 3 度押されたことを青木君が高橋君に伝える
- 当たりのボタンを 3 回以上押したため、高橋君の勝ちです
なお、このケースの真の解は \frac{2}{9} であるため、0.222222 や 0.222223141592 などの出力でも正解と判定されます。
入力例 2
10 1 10 1
出力例 2
1.000000000000000
入力例 3
100 10 10 2
出力例 3
0.401263060761621
Score : 550 points
Problem Statement
There are N push buttons. One of them is the winning button, and the other N-1 are losing buttons.
Aoki knows which button is winning, but Takahashi does not. Also, Takahashi cannot distinguish the N buttons.
They play a game using these buttons. The game consists of repeating the following sequence T times:
- Aoki arranges the N buttons in a random order.
- Takahashi performs the operation “choose a button and press it” M times. He may choose the same button multiple times.
- Aoki tells Takahashi the number of times the winning button has been pressed so far since the start of the game.
Takahashi wins if and only if the winning button has been pressed a total of at least K times throughout the T repetitions.
Find Takahashi’s probability of winning when he plays to maximize his win probability.
Constraints
- 1 \le N \le 2\times 10^5
- 1 \le T \le 30
- 1 \le M \le 30
- 1 \le K \le 30
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N T M K
Output
Output the answer. Your output is considered correct if its absolute or relative error from the true answer is at most 10^{-6}.
Sample Input 1
3 2 2 3
Sample Output 1
0.222222222222222
The game can proceed as follows (not necessarily optimally):
- 1st repetition
- Aoki randomly arranges the buttons so that the winning button is at position 1, and the losing buttons are at positions 2 and 3.
- Takahashi presses button 1.
- Takahashi presses button 2.
- Aoki tells him the winning button has been pressed 1 time.
- 2nd repetition
- Aoki randomly arranges the buttons so that the winning button is at position 3, and the losing buttons are at positions 1 and 2.
- Takahashi presses button 3.
- Takahashi presses button 3.
- Aoki tells him the winning button has been pressed 3 times.
- Since the winning button has been pressed not less than 3 times, Takahashi wins.
The true answer in this case is \tfrac{2}{9}, so outputs like 0.222222 or 0.222223141592 are also accepted.
Sample Input 2
10 1 10 1
Sample Output 2
1.000000000000000
Sample Input 3
100 10 10 2
Sample Output 3
0.401263060761621