A - T-shirt

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

あるプログラミングコンテストでは、以下のルールに従って参加者に T シャツをプレゼントします。

  • 上位 A 位までの参加者は、必ず T シャツが貰える。
  • 加えて、上位 A+1 位から B 位までの参加者のうち C 人が一様ランダムに選ばれ、選ばれた参加者は T シャツを貰える。

コンテストには 1000 人が参加し、全ての参加者が相異なる順位を取りました。
このコンテストの参加者であるいろはちゃんは、X 位を取りました。
このとき、いろはちゃんが T シャツを貰える確率を求めてください。

制約

  • 入力はすべて整数
  • 1 \le A < B \le 1000
  • 1 \le C \le B-A
  • 1 \le X \le 1000

入力

入力は以下の形式で標準入力から与えられる。

A B C X

出力

答えを出力せよ。 なお、想定解との絶対誤差または相対誤差が 10^{−6} 以下であれば、正解として扱われる。


入力例 1

30 500 20 103

出力例 1

0.042553191489

いろはちゃんは 103 位を取りました。
31 位から 500 位までの 470 人の参加者の中から 20 人が一様ランダムに選ばれ、ここで選ばれるといろはちゃんは T シャツを貰えます。この確率は \frac{20}{470}=0.04255319\dots です。


入力例 2

50 500 100 1

出力例 2

1.000000000000

いろはちゃんは 1 位を取りました。この入力において、いろはちゃんは確実に T シャツを貰えます。


入力例 3

1 2 1 1000

出力例 3

0.000000000000

いろはちゃんは 1000 位を取りました。この入力において、いろはちゃんが T シャツを貰えることはありません。

Score : 100 points

Problem Statement

In a certain programming contest, T-shirts are awarded to participants according to the following rules.

  • All participants who ranked A-th or higher get a T-shirt.
  • Additionally, from the participants who ranked between (A+1)-th and B-th (inclusive), C participants chosen uniformly at random get a T-shirt.

There were 1000 participants in this contest, and all of them got different ranks.
Iroha-chan, who participated in this contest, ranked X-th.
Find the probability that she gets a T-shirt.

Constraints

  • All values in input are integers.
  • 1 \le A < B \le 1000
  • 1 \le C \le B-A
  • 1 \le X \le 1000

Input

Input is given from Standard Input in the following format:

A B C X

Output

Print the answer. Your output will be considered correct if the absolute or relative error from the judge's answer is at most 10^{−6}.


Sample Input 1

30 500 20 103

Sample Output 1

0.042553191489

Iroha-chan ranked 103-rd.
She will get a T-shirt if she is among the 20 participants chosen uniformly at random from the 470 participants who ranked between 31-st and 500-th, which happens with probability \frac{20}{470}=0.04255319\dots.


Sample Input 2

50 500 100 1

Sample Output 2

1.000000000000

Iroha-chan ranked 1-st. This time, she is guaranteed to get a T-shirt.


Sample Input 3

1 2 1 1000

Sample Output 3

0.000000000000

Iroha-chan ranked 1000-th. This time, she will never get a T-shirt.

B - Capitalized?

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

英大文字・英小文字からなる空でない文字列 S が与えられます。以下の条件が満たされているか判定してください。

  • S の先頭の文字は大文字であり、それ以外の文字はすべて小文字である。

制約

  • 1 \leq |S| \leq 100(|S| は文字列 S の長さ)
  • S の各文字は英大文字または英小文字である。

入力

入力は以下の形式で標準入力から与えられる。

S

出力

条件が満たされていれば Yes、そうでなければ No を出力せよ。


入力例 1

Capitalized

出力例 1

Yes

Capitalized の先頭の文字 C は大文字であり、それ以外の文字 apitalized はすべて小文字であるため、Yes を出力します。


入力例 2

AtCoder

出力例 2

No

AtCoder は先頭以外にも大文字 C を含むため、No を出力します。


入力例 3

yes

出力例 3

No

yes の先頭の文字 y は大文字でないため、No を出力します。


入力例 4

A

出力例 4

Yes

Score: 100 points

Problem Statement

You are given a non-empty string S consisting of uppercase and lowercase English letters. Determine whether the following condition is satisfied:

  • The first character of S is uppercase, and all other characters are lowercase.

Constraints

  • 1 \leq |S| \leq 100 (|S| is the length of the string S.)
  • Each character of S is an uppercase or lowercase English letter.

Input

The input is given from Standard Input in the following format:

S

Output

If the condition is satisfied, print Yes; otherwise, print No.


Sample Input 1

Capitalized

Sample Output 1

Yes

The first character C of Capitalized is uppercase, and all other characters apitalized are lowercase, so you should print Yes.


Sample Input 2

AtCoder

Sample Output 2

No

AtCoder contains an uppercase letter C that is not at the beginning, so you should print No.


Sample Input 3

yes

Sample Output 3

No

The first character y of yes is not uppercase, so you should print No.


Sample Input 4

A

Sample Output 4

Yes
C - Round-Robin Tournament

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

1 から N までの番号が付いた N 人のプレイヤーが総当たり戦をしました。この総当たり戦で行われた試合全てについて、二人の一方が勝ち、もう一方が負けました。

総当たり戦の結果は N 個の長さ N の文字列 S_1,S_2,\ldots,S_N によって以下の形式で与えられます。

  • i\neq j のとき、S_i の j 文字目は o, x のいずれかであり、o のときプレイヤー i がプレイヤー j に勝ったことを、x のときプレイヤー i がプレイヤー j に負けたことを意味する。

  • i=j のとき、S_i の j 文字目は - である。

総当たり戦で勝った試合数が多いほうが順位が上であり、勝った試合数が同じ場合は、プレイヤーの番号が小さいほうが順位が上となります。 N 人のプレイヤーの番号を順位が高い順に答えてください。

制約

  • 2\leq N\leq 100
  • N は整数
  • S_i は o, x, - からなる長さ N の文字列
  • S_1,\ldots,S_N は問題文中の形式を満たす

入力

入力は以下の形式で標準入力から与えられる。

N 
S_1
S_2
\vdots
S_N

出力

N 人のプレイヤーの番号を、順位が高い順に空白区切りで出力せよ。


入力例 1

3
-xx
o-x
oo-

出力例 1

3 2 1

プレイヤー 1 は 0 勝、プレイヤー 2 は 1 勝、プレイヤー 3 は 2 勝なので、プレイヤーの番号は順位が高い順に 3,2,1 です。


入力例 2

7
-oxoxox
x-xxxox
oo-xoox
xoo-ooo
ooxx-ox
xxxxx-x
oooxoo-

出力例 2

4 7 3 1 5 2 6

プレイヤー 4 とプレイヤー 7 はどちらも 5 勝ですが、プレイヤー番号が小さいプレイヤー 4 のほうが順位が上になります。

Score : 200 points

Problem Statement

There are N players numbered 1 to N, who have played a round-robin tournament. For every match in this tournament, one player won and the other lost.

The results of the matches are given as N strings S_1,S_2,\ldots,S_N of length N each, in the following format:

  • If i\neq j, the j-th character of S_i is o or x. o means that player i won against player j, and x means that player i lost to player j.

  • If i=j, the j-th character of S_i is -.

The player with more wins ranks higher. If two players have the same number of wins, the player with the smaller player number ranks higher. Report the player numbers of the N players in descending order of rank.

Constraints

  • 2\leq N\leq 100
  • N is an integer.
  • S_i is a string of length N consisting of o, x, and -.
  • S_1,\ldots,S_N conform to the format described in the problem statement.

Input

The input is given from Standard Input in the following format:

N 
S_1
S_2
\vdots
S_N

Output

Print the player numbers of the N players in descending order of rank.


Sample Input 1

3
-xx
o-x
oo-

Sample Output 1

3 2 1

Player 1 has 0 wins, player 2 has 1 win, and player 3 has 2 wins. Thus, the player numbers in descending order of rank are 3,2,1.


Sample Input 2

7
-oxoxox
x-xxxox
oo-xoox
xoo-ooo
ooxx-ox
xxxxx-x
oooxoo-

Sample Output 2

4 7 3 1 5 2 6

Both players 4 and 7 have 5 wins, but player 4 ranks higher because their player number is smaller.

D - Weak Password

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

4 桁の暗証番号 X_1X_2X_3X_4 が与えられます。 番号は先頭の桁が 0 であることもあり得ます。 暗証番号は以下のいずれかの条件をみたすとき弱い暗証番号と呼ばれます。

  • 4 桁とも同じ数字である。
  • 1\leq i\leq 3 をみたす任意の整数 i について、 X_{i+1} が、 X_i の次の数字である。 ただし、 0\leq j\leq 8 について j の次の数字は j+1 であり、 9 の次の数字は 0 である。

与えられた暗証番号が弱い暗証番号ならば Weak を、そうでないならば Strong を出力してください。

制約

  • 0 \leq X_1, X_2, X_3, X_4 \leq 9
  • X_1, X_2, X_3, X_4 は整数である。

入力

入力は以下の形式で標準入力から与えられる。

X_1X_2X_3X_4

出力

与えられた暗証番号が弱い暗証番号ならば Weak を、そうでないならば Strong を出力せよ。


入力例 1

7777

出力例 1

Weak

4 桁ともすべて 7 であるため、 1 つめの条件をみたしており、弱い暗証番号です。


入力例 2

0112

出力例 2

Strong

1 桁目と 2 桁目が異なっており、 3 桁目は 2 桁目の次の数字ではないため、どちらの条件もみたしていません。


入力例 3

9012

出力例 3

Weak

9 の次の数字が 0 であることに注意してください。

Score : 200 points

Problem Statement

You are given a 4-digit PIN: X_1X_2X_3X_4, which may begin with a 0. The PIN is said to be weak when it satisfies one of the following conditions:

  • All of the four digits are the same.
  • For each integer i such that 1\leq i\leq 3, X_{i+1} follows X_i. Here, j+1 follows j for each 0\leq j\leq 8, and 0 follows 9.

If the given PIN is weak, print Weak; otherwise, print Strong.

Constraints

  • 0 \leq X_1, X_2, X_3, X_4 \leq 9
  • X_1, X_2, X_3, and X_4 are integers.

Input

Input is given from Standard Input in the following format:

X_1X_2X_3X_4

Output

If the given PIN is weak, print Weak; otherwise, print Strong.


Sample Input 1

7777

Sample Output 1

Weak

All four digits are 7, satisfying the first condition, so this PIN is weak.


Sample Input 2

0112

Sample Output 2

Strong

The first and second digits differ, and the third digit does not follow the second digit, so neither condition is satisfied.


Sample Input 3

9012

Sample Output 3

Weak

Note that 0 follows 9.

E - Minimize Abs 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

正整数 D が与えられます。

非負整数 x,y に対する |x^2+y^2-D| の最小値を求めてください。

制約

  • 1\leq D \leq 2\times 10^{12}
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

D

出力

答えを出力せよ。


入力例 1

21

出力例 1

1

x=4,y=2 のとき |x^2+y^2-D| = |16+4-21|=1 となります。

|x^2+y^2-D|=0 を満たすような非負整数 x,y は存在しないので、答えは 1 です。


入力例 2

998244353

出力例 2

0

入力例 3

264428617

出力例 3

32

Score : 300 points

Problem Statement

You are given a positive integer D.

Find the minimum value of |x^2+y^2-D| for non-negative integers x and y.

Constraints

  • 1\leq D \leq 2\times 10^{12}
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

D

Output

Print the answer.


Sample Input 1

21

Sample Output 1

1

For x=4 and y=2, we have |x^2+y^2-D| = |16+4-21|=1.

There are no non-negative integers x and y such that |x^2+y^2-D|=0, so the answer is 1.


Sample Input 2

998244353

Sample Output 2

0

Sample Input 3

264428617

Sample Output 3

32
F - ±1 Operation 1

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

整数 X が与えられます。この X に以下を施すことを「操作」と呼びます。

  • 以下の 2 つのうちどちらかを選択し、実行する。
    • X に 1 を加算する。
    • X から 1 を減算する。

初項 A 、公差 D 、項数 N の等差数列 S に含まれる数を「良い数」と呼びます。
「操作」を 0 回以上何度でも使って X を「良い数」にする時、必要な「操作」の最小回数を求めてください。

制約

  • 入力は全て整数
  • -10^{18} \le X,A \le 10^{18}
  • -10^6 \le D \le 10^6
  • 1 \le N \le 10^{12}

入力

入力は以下の形式で標準入力から与えられる。

X A D N

出力

答えを整数として出力せよ。


入力例 1

6 2 3 3

出力例 1

1

A=2,D=3,N=3 であるため、 S=(2,5,8) です。
X=6 を「良い数」にするためには、 X から 1 を減算することを 1 度行えば良いです。
0 回の操作で X を「良い数」にすることはできません。


入力例 2

0 0 0 1

出力例 2

0

D=0 である場合もあります。また、操作を 1 回も必要としない場合もあります。


入力例 3

998244353 -10 -20 30

出力例 3

998244363

入力例 4

-555555555555555555 -1000000000000000000 1000000 1000000000000

出力例 4

444445

Score : 300 points

Problem Statement

You are given an integer X. The following action on this integer is called an operation.

  • Choose and do one of the following.
    • Add 1 to X.
    • Subtract 1 from X.

The terms in the arithmetic progression S with N terms whose initial term is A and whose common difference is D are called good numbers.
Consider performing zero or more operations to make X a good number. Find the minimum number of operations required to do so.

Constraints

  • All values in input are integers.
  • -10^{18} \le X,A \le 10^{18}
  • -10^6 \le D \le 10^6
  • 1 \le N \le 10^{12}

Input

Input is given from Standard Input in the following format:

X A D N

Output

Print the answer as an integer.


Sample Input 1

6 2 3 3

Sample Output 1

1

Since A=2,D=3,N=3, we have S=(2,5,8).
You can subtract 1 from X once to make X=6 a good number.
It is impossible to make X good in zero operations.


Sample Input 2

0 0 0 1

Sample Output 2

0

We might have D=0. Additionally, no operation might be required.


Sample Input 3

998244353 -10 -20 30

Sample Output 3

998244363

Sample Input 4

-555555555555555555 -1000000000000000000 1000000 1000000000000

Sample Output 4

444445
G - Strange Mirroring

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350 点

問題文

英大小文字からなる文字列 S が与えられます。

S に以下の操作を 10^{100} 回繰り返します。

  • まず、 S の大文字を小文字に、小文字を大文字に書き換えた文字列を T とする。
  • その後、 S と T とをこの順に連結した文字列を新たな S とする。

Q 個の質問に答えて下さい。 そのうち i 個目は次の通りです。

  • 全ての操作を終えた後の S の先頭から K_i 文字目を求めよ。

制約

  • S は英大小文字からなる長さ 1 以上 2 \times 10^5 以下の文字列
  • Q,K_i は整数
  • 1 \le Q \le 2 \times 10^5
  • 1 \le K_i \le 10^{18}

入力

入力は以下の形式で標準入力から与えられる。

S
Q
K_1 K_2 \dots K_Q

出力

i 個目の質問の答えを C_i とする時、以下の形式で 1 行に空白区切りで出力せよ。

C_1 C_2 \dots C_Q

入力例 1

aB
16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

出力例 1

a B A b A b a B A b a B a B A b

操作前の S = aB です。

  • aB に 1 回操作を行うと aBAb となります。
  • aB に 2 回操作を行うと aBAbAbaB となります。
  • \dots

10^{100} 回の操作を終えた後の S = aBAbAbaBAbaBaBAb... です。


入力例 2

qWeRtYuIoP
8
1 1 2 3 5 8 13 21

出力例 2

q q W e t I E Q

入力例 3

AnUoHrjhgfLMcDIpzxXmEWPwBZvbKqQuiJTtFSlkNGVReOYCdsay
5
1000000000000000000 123456789 1 987654321 999999999999999999

出力例 3

K a A Z L

Score : 350 points

Problem Statement

You are given a string S consisting of uppercase and lowercase English letters.

We perform the following operation on S 10^{100} times:

  • First, create a string T by changing uppercase letters in S to lowercase, and lowercase letters to uppercase.
  • Then, concatenate S and T in this order to form a new S.

Answer Q queries. The i-th query is as follows:

  • Find the K_i-th character from the beginning of S after all operations are completed.

Constraints

  • S is a string consisting of uppercase and lowercase English letters, with length between 1 and 2 \times 10^5, inclusive.
  • Q and K_i are integers.
  • 1 \le Q \le 2 \times 10^5
  • 1 \le K_i \le 10^{18}

Input

The input is given from Standard Input in the following format:

S
Q
K_1 K_2 \dots K_Q

Output

Let C_i be the answer to the i-th query. Print them in a single line, separated by spaces, in the following format:

C_1 C_2 \dots C_Q

Sample Input 1

aB
16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Sample Output 1

a B A b A b a B A b a B a B A b

Before the operations, S = aB.

  • After performing the operation once on aB, it becomes aBAb.
  • After performing the operation twice on aB, it becomes aBAbAbaB.
  • \dots

After performing the operation 10^{100} times, S = aBAbAbaBAbaBaBAb...


Sample Input 2

qWeRtYuIoP
8
1 1 2 3 5 8 13 21

Sample Output 2

q q W e t I E Q

Sample Input 3

AnUoHrjhgfLMcDIpzxXmEWPwBZvbKqQuiJTtFSlkNGVReOYCdsay
5
1000000000000000000 123456789 1 987654321 999999999999999999

Sample Output 3

K a A Z L
H - Path Decomposition of a Tree

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475 点

問題文

NK 頂点の木が与えられます。頂点には 1,2,\dots,NK の番号がついており、i 番目 (i=1,2,\dots,NK-1) の辺は頂点 u_i,v_i を双方向に結んでいます。

この木を N 本の長さ K のパスに分解できるか判定してください。より詳細には、以下を満たす N \times K 行列 P が存在するかどうか判定してください。

  • P_{1,1},\dots,P_{1,K},P_{2,1},\dots,P_{N,K} は 1,2,\dots,NK の並べ替えである。
  • 各 i=1,2,\dots,N,\;j=1,2,\dots,K-1 について、頂点 P_{i,j} と頂点 P_{i,j+1} を結ぶ辺が存在する。

制約

  • 1 \leq N
  • 1 \leq K
  • NK \leq 2 \times 10^5
  • 1 \leq u_i < v_i \leq NK
  • 与えられるグラフは木である
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N K
u_1 v_1
u_2 v_2
\vdots
u_{NK-1} v_{NK-1}

出力

N 本の長さ K のパスに分解できるなら Yes を、そうでないなら No を出力せよ。


入力例 1

3 2
1 2
2 3
3 4
2 5
5 6

出力例 1

Yes

頂点 1,2 からなるパス、頂点 3,4 からなるパス、頂点 5,6 からなるパスに分解することができます。


入力例 2

3 2
1 2
2 3
3 4
2 5
3 6

出力例 2

No

Score : 475 points

Problem Statement

You are given a tree with NK vertices. The vertices are numbered 1,2,\dots,NK, and the i-th edge (i=1,2,\dots,NK-1) connects vertices u_i and v_i bidirectionally.

Determine whether this tree can be decomposed into N paths, each of length K. More precisely, determine whether there exists an N \times K matrix P satisfying the following:

  • P_{1,1}, \dots, P_{1,K}, P_{2,1}, \dots, P_{N,K} is a permutation of 1,2,\dots,NK.
  • For each i=1,2,\dots,N and j=1,2,\dots,K-1, there is an edge connecting vertices P_{i,j} and P_{i,j+1}.

Constraints

  • 1 \leq N
  • 1 \leq K
  • NK \leq 2 \times 10^5
  • 1 \leq u_i < v_i \leq NK
  • The given graph is a tree.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N K
u_1 v_1
u_2 v_2
\vdots
u_{NK-1} v_{NK-1}

Output

If it is possible to decompose the tree into N paths each of length K, print Yes. Otherwise, print No.


Sample Input 1

3 2
1 2
2 3
3 4
2 5
5 6

Sample Output 1

Yes

It can be decomposed into a path with vertices 1,2, a path with vertices 3,4, and a path with vertices 5,6.


Sample Input 2

3 2
1 2
2 3
3 4
2 5
3 6

Sample Output 2

No
I - Simultaneous Swap

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

長さ N の数列 A=(A_1,A_2,\ldots,A_N), B=(B_1,B_2,\ldots,B_N) が与えられます。

高橋君は次の操作を好きなだけ (0 回でも良い) 繰り返す事ができます。

1 以上 N 以下の、どの 2 つも互いに相異なる 3 つの整数 i,j,k を選ぶ。
A の i 番目の要素と j 番目の要素を交換し、B の i 番目の要素と k 番目の要素を交換する。

高橋君がうまく操作を繰り返すことによって、 A と B を一致させる事が可能ならば Yes を、不可能ならば No を出力してください。
ただし、A と B が一致しているとは、任意の 1\leq i\leq N について A の i 番目の要素と B の i 番目の要素が等しいことを言います。

制約

  • 3 \leq N \leq 2\times 10^5
  • 1\leq A_i,B_i\leq N
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

出力

操作を繰り返すことによって、高橋君が A と B を一致させる事が可能ならば Yes を、不可能ならば No を出力せよ。


入力例 1

3
1 2 1
1 1 2

出力例 1

Yes

(i,j,k)=(1,2,3) として 1 回操作を行うことで、A_1 と A_2、B_1 と B_3 がそれぞれ交換され、
A,B はともに (2,1,1) となって一致します。よって、Yes を出力します。


入力例 2

3
1 2 2
1 1 2

出力例 2

No

どのように操作を行っても A と B を一致させることはできません。よって、No を出力します。


入力例 3

5
1 2 3 2 1
3 2 2 1 1

出力例 3

Yes

入力例 4

8
1 2 3 4 5 6 7 8
7 8 5 6 4 3 1 2

出力例 4

No

Score : 500 points

Problem Statement

You are given two sequences of N numbers: A=(A_1,A_2,\ldots,A_N) and B=(B_1,B_2,\ldots,B_N).

Takahashi can repeat the following operation any number of times (possibly zero).

Choose three pairwise distinct integers i, j, and k between 1 and N.
Swap the i-th and j-th elements of A, and swap the i-th and k-th elements of B.

If there is a way for Takahashi to repeat the operation to make A and B equal, print Yes; otherwise, print No.
Here, A and B are said to be equal when, for every 1\leq i\leq N, the i-th element of A and that of B are equal.

Constraints

  • 3 \leq N \leq 2\times 10^5
  • 1\leq A_i,B_i\leq N
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

Output

Print Yes if there is a way for Takahashi to repeat the operation to make A and B equal, and print No otherwise.


Sample Input 1

3
1 2 1
1 1 2

Sample Output 1

Yes

Performing the operation once with (i,j,k)=(1,2,3) swaps A_1 and A_2, and swaps B_1 and B_3,
making both A and B equal to (2,1,1). Thus, you should print Yes.


Sample Input 2

3
1 2 2
1 1 2

Sample Output 2

No

There is no way to perform the operation to make A and B equal, so you should print No.


Sample Input 3

5
1 2 3 2 1
3 2 2 1 1

Sample Output 3

Yes

Sample Input 4

8
1 2 3 4 5 6 7 8
7 8 5 6 4 3 1 2

Sample Output 4

No