A - Count Down

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

N 以下の非負整数を大きい方から順にすべて出力してください。

制約

  • 1 \leq N \leq 100
  • N は整数

入力

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

N

出力

N 以下の非負整数が X 個存在するとき、X 行出力せよ。
i=1,2,\ldots,X に対し、i 行目には N 以下の非負整数のうち大きい方から i 番目のものを出力せよ。


入力例 1

3

出力例 1

3
2
1
0

3 以下の非負整数は 0,1,2,34 個です。
1 行目に 3 を、2 行目に 2 を、3 行目に 1 を、4 行目に 0 を出力することでこれらを大きい方から順に出力したことになります。


入力例 2

22

出力例 2

22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0

Score : 100 points

Problem Statement

Print all non-negative integers less than or equal to N in descending order.

Constraints

  • 1 \leq N \leq 100
  • N is an integer.

Input

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

N

Output

Print X lines, where X is the number of non-negative integers less than or equal to N.
For each i=1, 2, \ldots, X, the i-th line should contain the i-th greatest non-negative integer less than or equal to N.


Sample Input 1

3

Sample Output 1

3
2
1
0

We have four non-negative integers less than or equal to 3, which are 0, 1, 2, and 3.
To print them in descending order, print 3 in the first line, 2 in the second, 1 in the third, and 0 in the fourth.


Sample Input 2

22

Sample Output 2

22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
B - Sandwich Number

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

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

  • S は次の文字または文字列をこの順番で連結して得られる。
    • 一文字の英大文字
    • 100000 以上 999999 以下の整数を 10 進表記して得られる長さ 6 の文字列
    • 一文字の英大文字

制約

  • S は英大文字と数字からなる
  • S の長さは 1 以上 10 以下

入力

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

S

出力

S が問題文中の条件を満たすなら Yes と、満たさないなら No と出力せよ。


入力例 1

Q142857Z

出力例 1

Yes

SQ142857Z をこの順に連結して得られます。
QZ は英大文字であり、142857100000 以上 999999 以下の整数を 10 進表記して得られる長さ 6 の文字列なので、S は条件を満たします。


入力例 2

AB912278C

出力例 2

No

AB は一文字の英大文字ではないため、S は条件を満たしません。


入力例 3

X900000

出力例 3

No

S の末尾の一文字が英大文字ではないため、S は条件を満たしません。


入力例 4

K012345K

出力例 4

No

012345100000 以上 999999 以下の整数を 10 進表記して得られる長さ 6 の文字列ではないため、S は条件を満たしません。

Score : 200 points

Problem Statement

You are given a string S consisting of uppercase English letters and digits. Determine whether S satisfies the following condition.

  • S is a concatenation of the following characters and string in the order listed.
    • An uppercase English letter
    • A string of length 6 that is a decimal representation of an integer between 100000 and 999999, inclusive
    • An uppercase English letter

Constraints

  • S consists of uppercase English letters and digits.
  • The length of S is between 1 and 10, inclusive.

Input

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

S

Output

If S satisfies the condition in the problem statement, print Yes; otherwise, print No.


Sample Input 1

Q142857Z

Sample Output 1

Yes

S is a concatenation of Q, 142857, and Z in this order.
Q and Z are uppercase English letters, and 142857 is a string of length 6 that is a decimal representation of an integer between 100000 and 999999, so S satisfies the condition.


Sample Input 2

AB912278C

Sample Output 2

No

AB is not an uppercase English letter, so S does not satisfy the condition.


Sample Input 3

X900000

Sample Output 3

No

The last character of S is not an uppercase English letter, so S does not satisfy the condition.


Sample Input 4

K012345K

Sample Output 4

No

012345 is not a string of length 6 that is a decimal representation of an integer between 100000 and 999999, so S does not satisfy the condition.

C - Circular Playlist

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

N 曲からなるプレイリストがあり、曲には 1, \dots, N の番号が付けられています。
i の長さは A_i 秒です。

プレイリストを再生すると、曲 1、曲 2\ldots、曲 N の順に流れます。曲 N が流れ終わると、再び曲 1 から順に流れていきます。ある曲の途中で次の曲が流れることはなく、曲が流れ終わると、その瞬間に次の曲が流れ始めます。

プレイリストを再生してから T 秒後に流れているのはどの曲ですか?また、その曲が流れ始めてから何秒の時点ですか?
ただし、T 秒後ちょうどに曲が切り替わるような入力は与えられません。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq T \leq 10^{18}
  • 1 \leq A_i \leq 10^9
  • プレイリストを再生して T 秒後ちょうどに曲が切り替わることはない
  • 入力される値は全て整数

入力

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

N T
A_1 \ldots A_N

出力

プレイリストを再生してから T 秒後に流れている曲の番号と、その曲が流れ始めてから何秒たったかを表す整数を空白区切りで出力せよ。


入力例 1

3 600
180 240 120

出力例 1

1 60

プレイリストを再生してからの様子は次のようになります。

  • 0 秒後から 180 秒後まで曲 1 が流れる。
  • 180 秒後から 420 秒後まで曲 2 が流れる。
  • 420 秒後から 540 秒後まで曲 3 が流れる。
  • 540 秒後から 720 秒後まで曲 1 が流れる。
  • 720 秒後から 960 秒後まで曲 2 が流れる。
  • \qquad\vdots

600 秒後の時点で流れているのは曲 1 であり、流れ始めて 60 秒の時点です。


入力例 2

3 281
94 94 94

出力例 2

3 93

入力例 3

10 5678912340
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

出力例 3

6 678912340

Score : 300 points

Problem Statement

We have a playlist with N songs numbered 1, \dots, N.
Song i lasts A_i seconds.

When the playlist is played, song 1, song 2, \ldots, and song N play in this order. When song N ends, the playlist repeats itself, starting from song 1 again. While a song is playing, the next song does not play; when a song ends, the next song starts immediately.

At exactly T seconds after the playlist starts playing, which song is playing? Also, how many seconds have passed since the start of that song?
There is no input where the playlist changes songs at exactly T seconds after it starts playing.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq T \leq 10^{18}
  • 1 \leq A_i \leq 10^9
  • The playlist does not change songs at exactly T seconds after it starts playing.
  • All values in the input are integers.

Input

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

N T
A_1 \ldots A_N

Output

Print an integer representing the song that is playing at exactly T seconds after the playlist starts playing, and an integer representing the number of seconds that have passed since the start of that song, separated by a space.


Sample Input 1

3 600
180 240 120

Sample Output 1

1 60

When the playlist is played, the following happens. (Assume that it starts playing at time 0.)

  • From time 0 to time 180, song 1 plays.
  • From time 180 to time 420, song 2 plays.
  • From time 420 to time 540, song 3 plays.
  • From time 540 to time 720, song 1 plays.
  • From time 720 to time 960, song 2 plays.
  • \qquad\vdots

At time 600, song 1 is playing, and 60 seconds have passed since the start of that song.


Sample Input 2

3 281
94 94 94

Sample Output 2

3 93

Sample Input 3

10 5678912340
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3

6 678912340
D - Max Multiple

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

非負整数列 A=(a_1,a_2,\ldots,a_N) が与えられます。

A の(添え字が相異なる) K 個の項の和として考えられる非負整数の集合を S とします。

S に含まれる D の倍数の最大値を求めてください。ただし、SD の倍数が含まれない場合、代わりに -1 と出力してください。

制約

  • 1 \leq K \leq N \leq 100
  • 1 \leq D \leq 100
  • 0 \leq a_i \leq 10^9
  • 入力はすべて整数

入力

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

N K D
a_1 \ldots a_N

出力

答えを出力せよ。


入力例 1

4 2 2
1 2 3 4

出力例 1

6

A から 2 個の項を選ぶ方法を列挙すると

  • a_1a_2 を選ぶ。選ばれた項の和は 1+2=3 となる。
  • a_1a_3 を選ぶ。選ばれた項の和は 1+3=4 となる。
  • a_1a_4 を選ぶ。選ばれた項の和は 1+4=5 となる。
  • a_2a_3 を選ぶ。選ばれた項の和は 2+3=5 となる。
  • a_2a_4 を選ぶ。選ばれた項の和は 2+4=6 となる。
  • a_3a_4 を選ぶ。選ばれた項の和は 3+4=7 となる。

となり、S=\{3,4,5,6,7\} となります。S に含まれる 2 の倍数のうち最大のものは 6 なので、6 と出力します。


入力例 2

3 1 2
1 3 5

出力例 2

-1

この例では S=\{1,3,5\} です。S に含まれる非負整数はいずれも 2 の倍数でないため、-1 と出力します。

Score : 400 points

Problem Statement

You are given a sequence of non-negative integers A=(a_1,a_2,\ldots,a_N).

Let S be the set of non-negative integers that can be the sum of K terms in A (with distinct indices).

Find the greatest multiple of D in S. If there is no multiple of D in S, print -1 instead.

Constraints

  • 1 \leq K \leq N \leq 100
  • 1 \leq D \leq 100
  • 0 \leq a_i \leq 10^9
  • All values in the input are integers.

Input

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

N K D
a_1 \ldots a_N

Output

Print the answer.


Sample Input 1

4 2 2
1 2 3 4

Sample Output 1

6

Here are all the ways to choose two terms in A.

  • Choose a_1 and a_2, whose sum is 1+2=3.
  • Choose a_1 and a_3, whose sum is 1+3=4.
  • Choose a_1 and a_4, whose sum is 1+4=5.
  • Choose a_2 and a_3, whose sum is 2+3=5.
  • Choose a_2 and a_4, whose sum is 2+4=6.
  • Choose a_3 and a_4, whose sum is 3+4=7.

Thus, we have S=\{3,4,5,6,7\}. The greatest multiple of 2 in S is 6, so you should print 6.


Sample Input 2

3 1 2
1 3 5

Sample Output 2

-1

In this example, we have S=\{1,3,5\}. Nothing in S is a multiple of 2, so you should print -1.

E - Least Elements

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

長さ N の整数列 A = (A_1, \dots, A_N) と整数 M, K が与えられます。
i = 1, \dots, N - M + 1 に対して、次の独立な問題を解いてください。

M 個の整数 A_i, A_{i + 1}, \dots, A_{i + M - 1} を昇順に並べ替えたときの先頭 K 個の値の総和を求めよ。

制約

  • 1 \leq K \leq M \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 入力される値は全て整数

入力

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

N M K
A_1 A_2 \ldots A_N

出力

i = k のときの問題の答えを \mathrm{answer}_k として、次の形式で出力せよ。

\mathrm{answer}_1 \mathrm{answer}_2 \ldots \mathrm{answer}_{N-M+1}

入力例 1

6 4 3
3 1 4 1 5 9

出力例 1

5 6 10
  • i = 1 のとき、A_i, A_{i+1}, A_{i+2}, A_{i+3} を小さい順に並べると 1, 1, 3, 4 となり、小さい方から 3 個の値の総和は 5 です。
  • i = 2 のとき、A_i, A_{i+1}, A_{i+2}, A_{i+3} を小さい順に並べると 1, 1, 4, 5 となり、小さい方から 3 個の値の総和は 6 です。
  • i = 3 のとき、A_i, A_{i+1}, A_{i+2}, A_{i+3} を小さい順に並べると 1, 4, 5, 9 となり、小さい方から 3 個の値の総和は 10 です。

入力例 2

10 6 3
12 2 17 11 19 8 4 3 6 20

出力例 2

21 14 15 13 13

Score : 500 points

Problem Statement

You are given an integer sequence A = (A_1, \dots, A_N) of length N, and integers M and K.
For each i = 1, \dots, N - M + 1, solve the following independent problem.

Find the sum of the first K values in the sorted list of the M integers A_i, A_{i + 1}, \dots, A_{i + M - 1} in ascending order.

Constraints

  • 1 \leq K \leq M \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • All values in the input are integers.

Input

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

N M K
A_1 A_2 \ldots A_N

Output

Let \mathrm{answer}_k be the answer to the problem for i = k, and print them in the following format:

\mathrm{answer}_1 \mathrm{answer}_2 \ldots \mathrm{answer}_{N-M+1}

Sample Input 1

6 4 3
3 1 4 1 5 9

Sample Output 1

5 6 10
  • For i = 1, sorting A_i, A_{i+1}, A_{i+2}, A_{i+3} in ascending order yields 1, 1, 3, 4, where the sum of the first three values is 5.
  • For i = 2, sorting A_i, A_{i+1}, A_{i+2}, A_{i+3} in ascending order yields 1, 1, 4, 5, where the sum of the first three values is 6.
  • For i = 3, sorting A_i, A_{i+1}, A_{i+2}, A_{i+3} in ascending order yields 1, 4, 5, 9, where the sum of the first three values is 10.

Sample Input 2

10 6 3
12 2 17 11 19 8 4 3 6 20

Sample Output 2

21 14 15 13 13
F - Xor Minimization

Time Limit: 2.5 sec / Memory Limit: 1024 MB

配点 : 500

問題文

非負整数列 A=(a_1,\ldots,a_N) が与えられます。

A に対して次の操作をちょうど 1 回行います。

  • 非負整数 x を選ぶ。そして、i=1,\ldots,N すべてに対し、a_i の値を「a_ix のビット単位 xor」に置き換える。

操作後の A に含まれる値の最大値を M とします。M の最小値を求めてください。

ビット単位 xor とは 非負整数 A, B のビット単位 xor 、A \oplus B は、以下のように定義されます。
  • A \oplus B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
例えば、3 \oplus 5 = 6 となります (二進表記すると: 011 \oplus 101 = 110)。

制約

  • 1 \leq N \leq 1.5 \times 10^5
  • 0 \leq a_i \lt 2^{30}
  • 入力はすべて整数

入力

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

N
a_1 \ldots a_N

出力

答えを出力せよ。


入力例 1

3
12 18 11

出力例 1

16

x=2 として操作をすると、操作後の数列は (12 \oplus 2,18 \oplus 2, 11 \oplus 2) = (14,16,9) となり、最大値 M16 となります。
M16 より小さくすることは出来ないため、この値が答えです。


入力例 2

10
0 0 0 0 0 0 0 0 0 0

出力例 2

0

入力例 3

5
324097321 555675086 304655177 991244276 9980291

出力例 3

805306368

Score : 500 points

Problem Statement

You are given a sequence of non-negative integers A=(a_1,\ldots,a_N).

Let us perform the following operation on A just once.

  • Choose a non-negative integer x. Then, for every i=1, \ldots, N, replace the value of a_i with the bitwise XOR of a_i and x.

Let M be the maximum value in A after the operation. Find the minimum possible value of M.

What is bitwise XOR? The bitwise XOR of non-negative integers A and B, A \oplus B, is defined as follows.
  • When A \oplus B is written in binary, the k-th lowest bit (k \geq 0) is 1 if exactly one of the k-th lowest bits of A and B is 1, and 0 otherwise.
For instance, 3 \oplus 5 = 6 (in binary: 011 \oplus 101 = 110).

Constraints

  • 1 \leq N \leq 1.5 \times 10^5
  • 0 \leq a_i \lt 2^{30}
  • All values in the input are integers.

Input

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

N
a_1 \ldots a_N

Output

Print the answer.


Sample Input 1

3
12 18 11

Sample Output 1

16

If we do the operation with x=2, the sequence becomes (12 \oplus 2,18 \oplus 2, 11 \oplus 2) = (14,16,9), where the maximum value M is 16.
We cannot make M smaller than 16, so this value is the answer.


Sample Input 2

10
0 0 0 0 0 0 0 0 0 0

Sample Output 2

0

Sample Input 3

5
324097321 555675086 304655177 991244276 9980291

Sample Output 3

805306368
G - Farthest City

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 600

問題文

正整数 N, M が与えられます。
頂点に 1, \dots, N の番号が付けられた N 頂点の単純連結無向グラフであって、以下の条件を満たすものの総数を M で割った余りを求めてください。

  • 全ての u = 2, \dots, N-1 について、頂点 1 から頂点 u までの最短距離は、頂点 1 から頂点 N までの最短距離より真に小さい。

ただし、頂点 u から頂点 v までの最短距離とは、頂点 u, v を結ぶ単純パスに含まれる辺の本数の最小値を指します。
また、2 つのグラフが異なるとは、ある 2 頂点 u, v が存在して、これらの頂点を結ぶ辺が一方のグラフにのみ存在することを指します。

制約

  • 3 \leq N \leq 500
  • 10^8 \leq M \leq 10^9
  • N, M は整数

入力

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

N M

出力

答えを出力せよ。


入力例 1

4 1000000000

出力例 1

8

以下の 8 通りが条件を満たします。

example_00


入力例 2

3 100000000

出力例 2

1

入力例 3

500 987654321

出力例 3

610860515

M で割った余りを求めることに注意してください。

Score : 600 points

Problem Statement

You are given positive integers N and M.
Find the number, modulo M, of simple connected undirected graphs with N vertices numbered 1, \dots, N that satisfy the following condition.

  • For every u = 2, \dots, N-1, the shortest distance from vertex 1 to vertex u is strictly smaller than the shortest distance from vertex 1 to vertex N.

Here, the shortest distance from vertex u to vertex v is the minimum number of edges in a simple path connecting vertices u and v.
Two graphs are considered different if and only if there are two vertices u and v that are connected by an edge in exactly one of those graphs.

Constraints

  • 3 \leq N \leq 500
  • 10^8 \leq M \leq 10^9
  • N and M are integers.

Input

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

N M

Output

Print the answer.


Sample Input 1

4 1000000000

Sample Output 1

8

The following eight graphs satisfy the condition.

example_00


Sample Input 2

3 100000000

Sample Output 2

1

Sample Input 3

500 987654321

Sample Output 3

610860515

Be sure to find the number modulo M.

Ex - Alchemy

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 600

問題文

高橋君は「レベル 1 の宝石」を A 種類、それぞれ 10^{10^{100}} 個ずつ持っています。
また、2 以上の整数 n に対し、以下の条件すべてを満たす n 個の宝石を鍋に入れることで代わりに「レベル n の宝石」を 1 個生成できます。

  • どの 2 個の宝石も種類が異なる。
  • どの宝石のレベルも n 未満である。
  • 2 以上の整数 x すべてについて、「レベル x の宝石」は高々 1 個である。

高橋君が手に入れられる可能性がある「レベル N の宝石」が何種類あるかを \text{mod } 998244353 で求めてください。

ただし、レベルが 2 以上の宝石同士は、それぞれを生成するときに用いた宝石の集合が一致しているときに同じ種類であるとみなします。

  • ここで、宝石の集合同士は、ある一方の集合に含まれる宝石であってもう一方の集合に同じ種類の宝石が含まれないようなものが存在するときに区別されます。

また、任意の「レベル 1 の宝石」と任意のレベルが 2 以上の宝石は種類が異なります。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A \leq 10^9
  • N,A は整数

入力

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

N A

出力

答えを出力せよ。


入力例 1

3 3

出力例 1

10

10 種類の「レベル 3 の宝石」を手に入れる方法を以下に示します。

  • 3 種類の「レベル 1 の宝石」を鍋に入れる。
    • 高橋君は「レベル 1 の宝石」を 3 種類持っているため、「レベル 1 の宝石」を 3 種類選ぶ方法は 1 通りです。このため、この方法で手に入れられる「レベル 3 の宝石」は 1 種類です。
  • 1 種類の「レベル 2 の宝石」と 2 種類の「レベル 1 の宝石」を鍋に入れる。
    • 「レベル 2 の宝石」は 2 種類の「レベル 1 の宝石」を鍋に入れることで手に入れられます。
      • 高橋君は「レベル 1 の宝石」を 3 種類持っているため、「レベル 1 の宝石」を 2 種類選ぶ方法は 3 通りです。このため、ここで使われる「レベル 2 の宝石」として考えられるものは 3 種類です。
    • 「レベル 2 の宝石」として考えられるものが 3 種類、2 種類の「レベル 1 の宝石」を選ぶ方法が 3 通りあるため、この方法で手に入れられる「レベル 3 の宝石」は 3 \times 3 = 9 種類です。

入力例 2

1 100

出力例 2

100

入力例 3

200000 1000000000

出力例 3

797585162

Score : 600 points

Problem Statement

Takahashi has A kinds of level-1 gems, and 10^{10^{100}} gems of each of those kinds.
For an integer n greater than or equal to 2, he can put n gems that satisfy all of the following conditions into a cauldron to generate a level-n gem in return.

  • No two gems are of the same kind.
  • Every gem's level is less than n.
  • For every integer x greater than or equal to 2, there is at most one level-x gem.

Find the number of kinds of level-N gems that Takahashi can obtain, modulo 998244353.

Here, two level-2 or higher gems are considered to be of the same kind if and only if they are generated from the same set of gems.

  • Two sets of gems are distinguished if and only if there is a gem in one of those sets such that the other set does not contain a gem of the same kind.

Any level-1 gem and any level-2 or higher gem are of different kinds.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A \leq 10^9
  • N and A are integers.

Input

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

N A

Output

Print the answer.


Sample Input 1

3 3

Sample Output 1

10

Here are ten ways to obtain a level-3 gem.

  • Put three kinds of level-1 gems into the cauldron.
    • Takahashi has three kinds of level-1 gems, so there is one way to choose three kinds of level-1 gems. Thus, he can obtain one kind of level-3 gem in this way.
  • Put one kind of level-2 gem and two kinds of level-1 gems into the cauldron.
    • A level-2 gem can be obtained by putting two kinds of level-1 gems into the cauldron.
      • Takahashi has three kinds of level-1 gems, so there are three ways to choose two kinds of level-1 gems. Thus, three kinds of level-2 gems are available here.
    • There are three kinds of level-2 gems, and three ways to choose two kinds of level-1 gems, so he can obtain 3 \times 3 = 9 kinds of level-3 gems in this way.

Sample Input 2

1 100

Sample Output 2

100

Sample Input 3

200000 1000000000

Sample Output 3

797585162