A - Lexicographic Order

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

相異なる二つの文字列 S, T が与えられます。
ST よりも辞書順で小さい場合は Yes を、大きい場合は No を出力してください。

辞書順とは?

辞書順とは簡単に説明すると「単語が辞書に載っている順番」を意味します。より厳密な説明として、相異なる文字列 S と文字列 T の大小を判定するアルゴリズムを以下に説明します。

以下では「 Si 文字目の文字」を S_i のように表します。また、 ST より辞書順で小さい場合は S \lt T 、大きい場合は S \gt T と表します。

  1. ST のうち長さが短い方の文字列の長さを L とします。i=1,2,\dots,L に対して S_iT_i が一致するか調べます。
  2. S_i \neq T_i である i が存在する場合、そのような i のうち最小のものを j とします。そして、S_jT_j を比較して、 S_j がアルファベット順で T_j より小さい場合は S \lt T 、大きい場合は S \gt T と決定して、アルゴリズムを終了します。
  3. S_i \neq T_i である i が存在しない場合、 ST の長さを比較して、ST より短い場合は S \lt T 、長い場合は S \gt T と決定して、アルゴリズムを終了します。

なお、主要なプログラミング言語の多くでは、文字列の辞書順による比較は標準ライブラリに含まれる関数や演算子として実装されています。詳しくは各言語のリファレンスをご参照ください。

制約

  • S, T は英小文字からなる長さ 1 以上 10 以下の相異なる文字列である。

入力

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

S T

出力

ST より辞書順で小さい場合は Yes を、大きい場合は No を出力せよ。


入力例 1

abc atcoder

出力例 1

Yes

abcatcoder1 文字目が同じで 2 文字目が異なります。 アルファベットの bt よりもアルファベット順で先に来るので、 abc の方が atcoder よりも辞書順で小さいことがわかります。


入力例 2

arc agc

出力例 2

No

入力例 3

a aa

出力例 3

Yes

Score : 100 points

Problem Statement

You are given two different strings S and T.
If S is lexicographically smaller than T, print Yes; otherwise, print No.

What is the lexicographical order?

Simply speaking, the lexicographical order is the order in which words are listed in a dictionary. As a more formal definition, here is the algorithm to determine the lexicographical order between different strings S and T.

Below, let S_i denote the i-th character of S. Also, if S is lexicographically smaller than T, we will denote that fact as S \lt T; if S is lexicographically larger than T, we will denote that fact as S \gt T.

  1. Let L be the smaller of the lengths of S and T. For each i=1,2,\dots,L, we check whether S_i and T_i are the same.
  2. If there is an i such that S_i \neq T_i, let j be the smallest such i. Then, we compare S_j and T_j. If S_j comes earlier than T_j in alphabetical order, we determine that S \lt T and quit; if S_j comes later than T_j, we determine that S \gt T and quit.
  3. If there is no i such that S_i \neq T_i, we compare the lengths of S and T. If S is shorter than T, we determine that S \lt T and quit; if S is longer than T, we determine that S \gt T and quit.

Note that many major programming languages implement lexicographical comparison of strings as operators or functions in standard libraries. For more detail, see your language's reference.

Constraints

  • S and T are different strings, each of which consists of lowercase English letters and has a length of between 1 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

S T

Output

If S is lexicographically smaller than T, print Yes; otherwise, print No.


Sample Input 1

abc atcoder

Sample Output 1

Yes

abc and atcoder begin with the same character, but their second characters are different. Since b comes earlier than t in alphabetical order, we can see that abc is lexicographically smaller than atcoder.


Sample Input 2

arc agc

Sample Output 2

No

Sample Input 3

a aa

Sample Output 3

Yes
B - Power

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

整数 A, B が与えられます。 A^B の値を出力してください。

制約

  • 1 \leq A, B \leq 9
  • 入力はすべて整数

入力

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

A B

出力

答えを出力せよ。


入力例 1

4 3

出力例 1

64

4^3 = 64 であるので、64 を出力します。


入力例 2

5 5

出力例 2

3125

入力例 3

8 1

出力例 3

8

Score : 100 points

Problem Statement

Given integers A and B, print the value A^B.

Constraints

  • 1 \leq A, B \leq 9
  • All values in the input are integers.

Input

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

A B

Output

Print the answer.


Sample Input 1

4 3

Sample Output 1

64

4^3 = 64, so 64 should be printed.


Sample Input 2

5 5

Sample Output 2

3125

Sample Input 3

8 1

Sample Output 3

8
C - Who is missing?

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

整数 1, 2, \dots, N が書かれたカードが 4 枚ずつ、合計 4N 枚あります。

高橋君は、これらのカードをシャッフルしたのち 1 枚のカードを選んで抜き取り、残りの 4N - 1 枚を束にしてあなたに渡しました。渡された束の i \, (1 \leq i \leq 4N - 1) 枚目のカードには、整数 A_i が書かれています。

高橋君が抜き取ったカードに書かれていた整数を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq A_i \leq N \, (1 \leq i \leq 4N - 1)
  • k \, (1 \leq k \leq N) に対し、A_i = k となる i4 個以下である。
  • 入力は全て整数である。

入力

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

N
A_1 A_2 \ldots A_{4N - 1}

出力

答えを出力せよ。


入力例 1

3
1 3 2 3 3 2 2 1 1 1 2

出力例 1

3

高橋君が抜き取ったカードには 3 が書かれています。


入力例 2

1
1 1 1

出力例 2

1

入力例 3

4
3 2 1 1 2 4 4 4 4 3 1 3 2 1 3

出力例 3

2

Score : 200 points

Problem Statement

We have 4 cards with an integer 1 written on it, 4 cards with 2, \ldots, 4 cards with N, for a total of 4N cards.

Takahashi shuffled these cards, removed one of them, and gave you a pile of the remaining 4N-1 cards. The i-th card (1 \leq i \leq 4N - 1) of the pile has an integer A_i written on it.

Find the integer written on the card removed by Takahashi.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq A_i \leq N \, (1 \leq i \leq 4N - 1)
  • For each k \, (1 \leq k \leq N), there are at most 4 indices i such that A_i = k.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_{4N - 1}

Output

Print the answer.


Sample Input 1

3
1 3 2 3 3 2 2 1 1 1 2

Sample Output 1

3

Takahashi removed a card with 3 written on it.


Sample Input 2

1
1 1 1

Sample Output 2

1

Sample Input 3

4
3 2 1 1 2 4 4 4 4 3 1 3 2 1 3

Sample Output 3

2
D - Let's Get a Perfect Score

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

1 から N までの番号がついた N 人の参加者が、1 から M までの番号がついた M 問からなるコンテストに参加します。

1 以上 N 以下の整数 i1 以上 M 以下の整数 j について、S_ij 番目の文字が o のとき参加者 i は問題 j を解くことが可能で、S_ij 番目の文字が x のとき参加者 i は問題 j を解くことが不可能です。

このコンテストは、二人の参加者でペアを組んで参加します。二人が協力することで M 問全てを解くことが可能であるようなペアの個数を答えてください。

より厳密には、1\leq x < y\leq N を満たす整数の組 (x,y) であって、 1 以上 M 以下の任意の整数 j について、参加者 x か参加者 y の少なくとも一方は問題 j を解くことが可能であるという条件を満たすものの個数を答えてください。

制約

  • N2 以上 30 以下の整数
  • M1 以上 30 以下の整数
  • S_io, x からなる長さ M の文字列

入力

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

N M
S_1
S_2
\vdots
S_N

出力

答えを出力せよ。


入力例 1

5 5
ooooo
oooxx
xxooo
oxoxo
xxxxx

出力例 1

5

参加者 12 のペア、参加者 13 のペア、参加者 14 のペア、参加者 15 のペア、参加者 23 のペアの 5 個のペアが条件を満たします。

例えば参加者 24 のペアは、問題 4 が解けないので条件を満たしません。


入力例 2

3 2
ox
xo
xx

出力例 2

1

入力例 3

2 4
xxxx
oxox

出力例 3

0

Score : 200 points

Problem Statement

N participants, numbered 1 to N, will participate in a contest with M problems, numbered 1 to M.

For an integer i between 1 and N and an integer j between 1 and M, participant i can solve problem j if the j-th character of S_i is o, and cannot solve it if that character is x.

The participants must be in pairs. Print the number of ways to form a pair of participants who can collectively solve all the M problems.

More formally, print the number of pairs (x,y) of integers satisfying 1\leq x < y\leq N such that for any integer j between 1 and M, at least one of participant x and participant y can solve problem j.

Constraints

  • N is an integer between 2 and 30, inclusive.
  • M is an integer between 1 and 30, inclusive.
  • S_i is a string of length M consisting of o and x.

Input

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

N M
S_1
S_2
\vdots
S_N

Output

Print the answer.


Sample Input 1

5 5
ooooo
oooxx
xxooo
oxoxo
xxxxx

Sample Output 1

5

The following five pairs satisfy the condition: participants 1 and 2, participants 1 and 3, participants 1 and 4, participants 1 and 5, and participants 2 and 3.

On the other hand, the pair of participants 2 and 4, for instance, does not satisfy the condition because they cannot solve problem 4.


Sample Input 2

3 2
ox
xo
xx

Sample Output 2

1

Sample Input 3

2 4
xxxx
oxox

Sample Output 3

0
E - Long Sequence

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

長さ N の正整数のみからなる数列 A=(A_1,\dots,A_N) があります。
A10^{100} 回連結した数列を数列 B とします。

B の項を前から順に足したとき、和が初めて X を超えるのは何項目まで足したときですか?
すなわち、以下の式を満たす最小の整数 k を求めてください。

\displaystyle{\sum_{i=1}^{k} B_i \gt X}

制約

  • 1 \leq N \leq 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq X \leq 10^{18}
  • 入力は全て整数

入力

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

N
A_1 \ldots A_N
X

出力

答えを出力せよ。


入力例 1

3
3 5 2
26

出力例 1

8

B=(3,5,2,3,5,2,3,5,2,\dots) です。
\displaystyle{\sum_{i=1}^{8} B_i = 28 \gt 26} であり、k7 以下のとき条件を満たさないので、8 が答えです。


入力例 2

4
12 34 56 78
1000

出力例 2

23

Score : 300 points

Problem Statement

We have a sequence of N positive integers: A=(A_1,\dots,A_N).
Let B be the concatenation of 10^{100} copies of A.

Consider summing up the terms of B from left to right. When does the sum exceed X for the first time?
In other words, find the minimum integer k such that:

\displaystyle{\sum_{i=1}^{k} B_i \gt X}.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq X \leq 10^{18}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N
X

Output

Print the answer.


Sample Input 1

3
3 5 2
26

Sample Output 1

8

We have B=(3,5,2,3,5,2,3,5,2,\dots).
\displaystyle{\sum_{i=1}^{8} B_i = 28 \gt 26} holds, but the condition is not satisfied when k is 7 or less, so the answer is 8.


Sample Input 2

4
12 34 56 78
1000

Sample Output 2

23
F - Virus

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

1, 2, \ldots, N の番号がついた N 人の人が二次元平面上におり、人 i は座標 (X_i,Y_i) で表される地点にいます。

1 がウイルスに感染しました。ウイルスに感染した人から距離が D 以内にいる人にウイルスはうつります。

ただし、距離はユークリッド距離、すなわち 2(a_1, a_2)(b_1, b_2) に対し、この 2 点間の距離が \sqrt {(a_1-b_1)^2 + (a_2-b_2)^2} であるものとして定められています。

十分に時間が経過した、すなわち人 i がウイルスに感染しているならば 人 i との距離が D 以内にいるすべての人がウイルスに感染している状態になったときに、各 i について人 i がウイルスに感染しているか判定してください。

制約

  • 1 \leq N, D \leq 2000
  • -1000 \leq X_i, Y_i \leq 1000
  • i \neq j のとき (X_i, Y_i) \neq (X_j, Y_j)
  • 入力はすべて整数

入力

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

N D
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

出力

N 行出力せよ。i 行目には、人 i がウイルスに感染しているならば Yes を、そうでないならば No を出力せよ。


入力例 1

4 5
2 -1
3 1
8 8
0 5

出力例 1

Yes
Yes
No
Yes

1 と人 2 の距離は \sqrt 5 であるため、人 2 はウイルスに感染します。
また、人 2 と人 4 の距離は 5 であるため、人 4 はウイルスに感染します。
3 は距離 5 以内に人がいないので、ウイルスに感染することはありません。


入力例 2

3 1
0 0
-1000 -1000
1000 1000

出力例 2

Yes
No
No

入力例 3

9 4
3 2
6 -1
1 6
6 5
-2 -3
5 3
2 -3
2 1
2 6

出力例 3

Yes
No
No
Yes
Yes
Yes
Yes
Yes
No

Score : 300 points

Problem Statement

There are N people numbered 1, 2, \ldots, N on a two-dimensional plane, and person i is at the point represented by the coordinates (X_i,Y_i).

Person 1 has been infected with a virus. The virus spreads to people within a distance of D from an infected person.

Here, the distance is defined as the Euclidean distance, that is, for two points (a_1, a_2) and (b_1, b_2), the distance between these two points is \sqrt {(a_1-b_1)^2 + (a_2-b_2)^2}.

After a sufficient amount of time has passed, that is, when all people within a distance of D from person i are infected with the virus if person i is infected, determine whether person i is infected with the virus for each i.

Constraints

  • 1 \leq N, D \leq 2000
  • -1000 \leq X_i, Y_i \leq 1000
  • (X_i, Y_i) \neq (X_j, Y_j) if i \neq j.
  • All input values are integers.

Input

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

N D
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

Output

Print N lines. The i-th line should contain Yes if person i is infected with the virus, and No otherwise.


Sample Input 1

4 5
2 -1
3 1
8 8
0 5

Sample Output 1

Yes
Yes
No
Yes

The distance between person 1 and person 2 is \sqrt 5, so person 2 gets infected with the virus.
Also, the distance between person 2 and person 4 is 5, so person 4 gets infected with the virus.
Person 3 has no one within a distance of 5, so they will not be infected with the virus.


Sample Input 2

3 1
0 0
-1000 -1000
1000 1000

Sample Output 2

Yes
No
No

Sample Input 3

9 4
3 2
6 -1
1 6
6 5
-2 -3
5 3
2 -3
2 1
2 6

Sample Output 3

Yes
No
No
Yes
Yes
Yes
Yes
Yes
No
G - AABCC

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

N 以下の正整数のうち、 a<b<c なる 素数 a,b,c を用いて a^2 \times b \times c^2 と表せるものはいくつありますか?

制約

  • N300 \le N \le 10^{12} を満たす整数

入力

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

N

出力

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


入力例 1

1000

出力例 1

3

1000 以下で条件を満たす整数は以下の 3 つです。

  • 300 = 2^2 \times 3 \times 5^2
  • 588 = 2^2 \times 3 \times 7^2
  • 980 = 2^2 \times 5 \times 7^2

入力例 2

1000000000000

出力例 2

2817785

Score : 400 points

Problem Statement

How many positive integers no greater than N can be represented as a^2 \times b \times c^2 with three primes a,b, and c such that a<b<c?

Constraints

  • N is an integer satisfying 300 \le N \le 10^{12}.

Input

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

N

Output

Print the answer as an integer.


Sample Input 1

1000

Sample Output 1

3

The conforming integers no greater than 1000 are the following three.

  • 300 = 2^2 \times 3 \times 5^2
  • 588 = 2^2 \times 3 \times 7^2
  • 980 = 2^2 \times 5 \times 7^2

Sample Input 2

1000000000000

Sample Output 2

2817785
H - Sugoroku 3

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

マス 1 からマス NN 個のマスがあります。はじめ、あなたはマス 1 にいます。

また、マス 1 からマス N-1 にはそれぞれサイコロが置いてあります。マス i のサイコロは 0 以上 A_i 以下の整数を等確率にランダムで出します。(サイコロを振る操作は毎回独立です。)

あなたは、マス N に到達するまで、現在いるマスに置かれているサイコロを振り、出た目の数だけ進むことを繰り返します。厳密に言うと、マス X にいるときにサイコロで Y が出た場合はマス X+Y に移動します。

サイコロを振る回数の期待値 \bmod\ 998244353 を求めてください。

注記

求める期待値は必ず有理数となることが証明できます。またこの問題の制約下では、その値を互いに素な 2 つの整数 P, Q を用いて \frac{P}{Q} と表したとき、R \times Q \equiv P\pmod{998244353} かつ 0 \leq R \lt 998244353 を満たす整数 R がただ一つ存在することが証明できます。この R を求めてください。

制約

  • 2 \le N \le 2 \times 10^5
  • 1 \le A_i \le N-i(1 \le i \le N-1)
  • 入力は全て整数。

入力

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

N
A_1 A_2 \dots A_{N-1}

出力

答えを出力せよ。


入力例 1

3
1 1

出力例 1

4

求める期待値は 4 であるため、4 を出力します。

マス N に到達するまでの流れとしては、以下のようなものが考えられます。

  • マス 11 を出し、マス 2 に移動する。
  • マス 20 を出し、移動しない。
  • マス 21 を出し、マス 3 に移動する。

このようになる確率は \frac{1}{8} です。


入力例 2

5
3 1 2 1

出力例 2

332748122

Score : 500 points

Problem Statement

There are N squares called Square 1 though Square N. You start on Square 1.

Each of the squares from Square 1 through Square N-1 has a die on it. The die on Square i is labeled with the integers from 0 through A_i, each occurring with equal probability. (Die rolls are independent of each other.)

Until you reach Square N, you will repeat rolling a die on the square you are on. Here, if the die on Square x rolls the integer y, you go to Square x+y.

Find the expected value, modulo 998244353, of the number of times you roll a die.

Notes

It can be proved that the sought expected value is always a rational number. Additionally, if that value is represented \frac{P}{Q} using two coprime integers P and Q, there is a unique integer R such that R \times Q \equiv P\pmod{998244353} and 0 \leq R \lt 998244353. Find this R.

Constraints

  • 2 \le N \le 2 \times 10^5
  • 1 \le A_i \le N-i(1 \le i \le N-1)
  • 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-1}

Output

Print the answer.


Sample Input 1

3
1 1

Sample Output 1

4

The sought expected value is 4, so 4 should be printed.

Here is one possible scenario until reaching Square N:

  • Roll 1 on Square 1, and go to Square 2.
  • Roll 0 on Square 2, and stay there.
  • Roll 1 on Square 2, and go to Square 3.

This scenario occurs with probability \frac{1}{8}.


Sample Input 2

5
3 1 2 1

Sample Output 2

332748122
I - ABCBAC

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

長さ N の文字列 S および整数 i\ (0\leq i\leq N) に対して、f_i(S) を、

  • S の先頭 i 文字
  • S を反転した文字列
  • S の末尾 N-i 文字

をこの順に連結した文字列と定義します。 例えば、S= abci=2 のとき、f_i(S)= abcbac です。

長さ 2N の文字列 T が与えられます。 f_i(S)=T を満たす長さ N の文字列 S と整数 i\ (0\leq i\leq N) の組を 1 つ見つけてください。 そのような S,i の組が存在しない場合は、それを報告してください。

制約

  • 1\leq N \leq 10^6
  • N は整数
  • T は英小文字からなる長さ 2N の文字列

入力

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

N 
T

出力

条件を満たす S,i の組が存在しないならば、-1 と出力せよ。 存在するならば、S,i を改行区切りで出力せよ。 条件を満たす S,i の組が複数存在する場合は、そのどれを出力しても良い。


入力例 1

3
abcbac

出力例 1

abc
2

問題文中に書いた通り、S= abci=2 とすると f_i(S)= abcbac となって T に一致するため、abc2 を出力します。


入力例 2

4
abababab

出力例 2

abab
1

S= ababi=3 としても条件を満たします。


入力例 3

3
agccga

出力例 3

cga
0

S= agci=3 としても条件を満たします。


入力例 4

4
atcodeer

出力例 4

-1

条件を満たす S,i の組が存在しない場合は -1 を出力してください。

Score : 500 points

Problem Statement

For a string S of length N and an integer i\ (0\leq i\leq N), let us define the string f_i(S) as the concatenation of:

  • the first i characters of S,
  • the reversal of S, and
  • the last (N-i) characters of S,

in this order. For instance, if S= abc and i=2, we have f_i(S)= abcbac.

You are given a string T of length 2N. Find a pair of a string S of length N and an integer i\ (0\leq i\leq N) such that f_i(S)=T. If no such pair of S and i exists, report that fact.

Constraints

  • 1\leq N \leq 10^6
  • N is an integer.
  • T is a string of length 2N consisting of lowercase English letters.

Input

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

N 
T

Output

If no pair of S and i satisfies the condition, print -1. Otherwise, print S and i, separated by a newline. If multiple pairs of S and i satisfy the condition, you may print any of them.


Sample Input 1

3
abcbac

Sample Output 1

abc
2

As mentioned in the problem statement, if S= abc and i=2, we have f_i(S)= abcbac, which equals T, so you should print abc and 2.


Sample Input 2

4
abababab

Sample Output 2

abab
1

S= abab and i=3 also satisfy the condition.


Sample Input 3

3
agccga

Sample Output 3

cga
0

S= agc and i=3 also satisfy the condition.


Sample Input 4

4
atcodeer

Sample Output 4

-1

If no pair of S and i satisfies the condition, print -1.