A - +-x

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 100

問題文

整数 A, B があります。

A + B, \ A - B, \ A \times B の中で最大の数を出力してください。

制約

  • 入力は全て整数である。
  • -100 \leq A,\ B \leq 100

入力

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

A B

出力

A + B, \ A - B, \ A \times B の中で最大の数を出力せよ。


入力例 1

-13 3

出力例 1

-10

A + B = -10, \ A - B = -16, \ A \times B = -39 の中で最大の数は -10 です。


入力例 2

1 -33

出力例 2

34

A + B = -32, \ A - B = 34, \ A \times B = -33 の中で最大の数は 34 です。


入力例 3

13 3

出力例 3

39

A + B = 16, \ A - B = 10, \ A \times B = 39 の中で最大の数は 39 です。

Score : 100 points

Problem Statement

We have two integers: A and B.

Print the largest number among A + B, A - B, and A \times B.

Constraints

  • All values in input are integers.
  • -100 \leq A,\ B \leq 100

Input

Input is given from Standard Input in the following format:

A B

Output

Print the largest number among A + B, A - B, and A \times B.


Sample Input 1

-13 3

Sample Output 1

-10

The largest number among A + B = -10, A - B = -16, and A \times B = -39 is -10.


Sample Input 2

1 -33

Sample Output 2

34

The largest number among A + B = -32, A - B = 34, and A \times B = -33 is 34.


Sample Input 3

13 3

Sample Output 3

39

The largest number among A + B = 16, A - B = 10, and A \times B = 39 is 39.

B - One Clue

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 200

問題文

数直線上に 2000001 個の石が置かれています。これらの石の座標は -1000000, -999999, -999998, \ldots, 999999, 1000000 です。

これらの石のうち、ある連続する K 個の石が黒で塗られており、それ以外の石は白で塗られています。

また、座標 X にある石は黒で塗られていることが分かっています。

黒で塗られている石が置かれている可能性のある座標をすべて、小さい順に出力してください。

制約

  • 1 \leq K \leq 100
  • 0 \leq X \leq 100
  • 入力中の値はすべて整数である。

入力

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

K X

出力

黒で塗られている石が置かれている可能性のある座標をすべて、空白で区切って小さい順に出力せよ。


入力例 1

3 7

出力例 1

5 6 7 8 9

黒で塗られた石の数が 3 個であることと、座標 7 の石が黒で塗られていることが分かっています。このとき、次の 3 通りの可能性が考えられます。

  • 黒で塗られた 3 個の石は座標 5,6,7 に置かれている。
  • 黒で塗られた 3 個の石は座標 6,7,8 に置かれている。
  • 黒で塗られた 3 個の石は座標 7,8,9 に置かれている。

よって、黒で塗られている石が置かれている可能性のある座標は 5,6,7,8,95 つです。


入力例 2

4 0

出力例 2

-3 -2 -1 0 1 2 3

負の座標に黒で塗られている石が置かれている可能性もあります。


入力例 3

1 100

出力例 3

100

Score : 200 points

Problem Statement

There are 2000001 stones placed on a number line. The coordinates of these stones are -1000000, -999999, -999998, \ldots, 999999, 1000000.

Among them, some K consecutive stones are painted black, and the others are painted white.

Additionally, we know that the stone at coordinate X is painted black.

Print all coordinates that potentially contain a stone painted black, in ascending order.

Constraints

  • 1 \leq K \leq 100
  • 0 \leq X \leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

K X

Output

Print all coordinates that potentially contain a stone painted black, in ascending order, with spaces in between.


Sample Input 1

3 7

Sample Output 1

5 6 7 8 9

We know that there are three stones painted black, and the stone at coordinate 7 is painted black. There are three possible cases:

  • The three stones painted black are placed at coordinates 5, 6, and 7.
  • The three stones painted black are placed at coordinates 6, 7, and 8.
  • The three stones painted black are placed at coordinates 7, 8, and 9.

Thus, five coordinates potentially contain a stone painted black: 5, 6, 7, 8, and 9.


Sample Input 2

4 0

Sample Output 2

-3 -2 -1 0 1 2 3

Negative coordinates can also contain a stone painted black.


Sample Input 3

1 100

Sample Output 3

100
C - Green Bin

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 300

問題文

文字列 a に含まれる文字を何らかの順序で並べることで得られる文字列を aアナグラム と呼びます。

例えば、greenbinbeginner のアナグラムです。このように、同じ文字が複数回現れるときはその文字をちょうどその回数だけ使わなければなりません。

N 個の文字列 s_1, s_2, \ldots, s_N が与えられます。それぞれの文字列は長さが 10 で英小文字からなり、またこれらの文字列はすべて異なります。二つの整数 i, j (1 \leq i < j \leq N) の組であって、s_is_j のアナグラムであるようなものの個数を求めてください。

制約

  • 2 \leq N \leq 10^5
  • s_i は長さ 10 の文字列である。
  • s_i の各文字は英小文字である。
  • s_1, s_2, \ldots, s_N はすべて異なる。

入力

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

N
s_1
s_2
:
s_N

出力

二つの整数 i, j (1 \leq i < j \leq N) の組であって、s_is_j のアナグラムであるようなものの個数を出力せよ。


入力例 1

3
acornistnt
peanutbomb
constraint

出力例 1

1

s_1 = acornistnts_3 = constraint のアナグラムです。他に s_is_j のアナグラムであるような i, j の組はないため、答えは 1 です。


入力例 2

2
oneplustwo
ninemodsix

出力例 2

0

s_is_j のアナグラムであるような i, j の組がないときは 0 と出力してください。


入力例 3

5
abaaaaaaaa
oneplustwo
aaaaaaaaba
twoplusone
aaaabaaaaa

出力例 3

4

ここにそのようなケースを置くことはできませんが、答えは 32 bit 整数型に収まらない可能性があるので注意してください。

Score : 300 points

Problem Statement

We will call a string obtained by arranging the characters contained in a string a in some order, an anagram of a.

For example, greenbin is an anagram of beginner. As seen here, when the same character occurs multiple times, that character must be used that number of times.

Given are N strings s_1, s_2, \ldots, s_N. Each of these strings has a length of 10 and consists of lowercase English characters. Additionally, all of these strings are distinct. Find the number of pairs of integers i, j (1 \leq i < j \leq N) such that s_i is an anagram of s_j.

Constraints

  • 2 \leq N \leq 10^5
  • s_i is a string of length 10.
  • Each character in s_i is a lowercase English letter.
  • s_1, s_2, \ldots, s_N are all distinct.

Input

Input is given from Standard Input in the following format:

N
s_1
s_2
:
s_N

Output

Print the number of pairs of integers i, j (1 \leq i < j \leq N) such that s_i is an anagram of s_j.


Sample Input 1

3
acornistnt
peanutbomb
constraint

Sample Output 1

1

s_1 = acornistnt is an anagram of s_3 = constraint. There are no other pairs i, j such that s_i is an anagram of s_j, so the answer is 1.


Sample Input 2

2
oneplustwo
ninemodsix

Sample Output 2

0

If there is no pair i, j such that s_i is an anagram of s_j, print 0.


Sample Input 3

5
abaaaaaaaa
oneplustwo
aaaaaaaaba
twoplusone
aaaabaaaaa

Sample Output 3

4

Note that the answer may not fit into a 32-bit integer type, though we cannot put such a case here.

D - Summer Vacation

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 400

問題文

N 件の日雇いアルバイトがあり、i 件目の日雇いアルバイトを請けて働くと、その A_i 日後に報酬 B_i が得られます。

あなたは、これらの中から 1 日に 1 件まで選んで請け、働くことができます。

ただし、請けたことのある日雇いアルバイトは選べません。

今日から M 日後まで(M 日後を含む)に得られる報酬の合計の最大値を求めてください。

なお、日雇いアルバイトは今日から請けて働くことができます。

制約

  • 入力は全て整数である。
  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq A_i \leq 10^5
  • 1 \leq B_i \leq 10^4

入力

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

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

M 日後までに得られる報酬の合計の最大値を出力せよ。


入力例 1

3 4
4 3
4 1
2 2

出力例 1

5

以下のように日雇いアルバイトを請けて働くと、報酬の合計は 5 となり、このときが最大です。

  • 今日、1 件目の日雇いアルバイトを請けて働き、今日から 4 日後に報酬 3 を得ます。
  • 明日(今日から 1 日後)、3 件目の日雇いアルバイトを請けて働き、今日から 1+2 = 3 日後に報酬 2 を得ます。

入力例 2

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

出力例 2

10

入力例 3

1 1
2 1

出力例 3

0

Score : 400 points

Problem Statement

There are N one-off jobs available. If you take the i-th job and complete it, you will earn the reward of B_i after A_i days from the day you do it.

You can take and complete at most one of these jobs in a day.

However, you cannot retake a job that you have already done.

Find the maximum total reward that you can earn no later than M days from today.

You can already start working today.

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq A_i \leq 10^5
  • 1 \leq B_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the maximum total reward that you can earn no later than M days from today.


Sample Input 1

3 4
4 3
4 1
2 2

Sample Output 1

5

You can earn the total reward of 5 by taking the jobs as follows:

  • Take and complete the first job today. You will earn the reward of 3 after four days from today.
  • Take and complete the third job tomorrow. You will earn the reward of 2 after two days from tomorrow, that is, after three days from today.

Sample Input 2

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

Sample Output 2

10

Sample Input 3

1 1
2 1

Sample Output 3

0
E - Coins Respawn

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 500

問題文

1 から N までの番号がつけられた N 頂点と M 辺からなる有向グラフがあります。 i 番目の辺は頂点 A_i から頂点 B_i へと向かい、この辺の上には C_i 枚のコインが置かれています。 また、頂点 N にはボタンが設置されています。

このグラフ上でゲームを行います。 あなたは頂点 1 でコインを 0 枚持ってゲームを開始し、辺をたどってコインを拾いながら頂点 N を目指します。 1 本の辺を通るには 1 分の時間がかかり、辺を通るたびにその辺の上に置かれているすべてのコインを拾うことができます。 ゲームの世界ではよくあるように、ある辺を通ってその上のコインを拾っても、その辺を次に通る際には同じ枚数のコインが再び出現しており、それらを再び拾うことができます。

頂点 N に到着したとき、ボタンを押してゲームを終了することができます。(ボタンを押さずに移動を続けることもできます。) ただし、ゲームを終了する際に、ゲーム開始からの経過時間を T 分として T \times P 枚のコインの支払いが要求されます。持っているコインの枚数が T \times P 枚未満の場合は、代わりに持っているコインをすべて支払います。

この支払いの後に残ったコインの枚数があなたのスコアとなります。 獲得できるスコアの最大値が存在するか判定し、存在する場合はその最大値を求めてください。

制約

  • 2 \leq N \leq 2500
  • 1 \leq M \leq 5000
  • 1 \leq A_i, B_i \leq N
  • 1 \leq C_i \leq 10^5
  • 0 \leq P \leq 10^5
  • 入力中の値はすべて整数である。
  • 頂点 1 から頂点 N に到達することが可能である。

入力

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

N M P
A_1 B_1 C_1
:
A_M B_M C_M

出力

獲得できるスコアの最大値が存在する場合はその最大値を、存在しない場合は -1 を出力せよ。


入力例 1

3 3 10
1 2 20
2 3 30
1 3 45

出力例 1

35

入力例 1 で与えられるグラフの図

頂点 1 から頂点 3 に移動する方法は以下の 2 通りです。

  • 頂点 1 \rightarrow 2 \rightarrow 3: 道中でコインを 20 + 30 = 50 枚拾う。ゲーム開始から 2 分後に頂点 3 に着き、ボタンを押してコインを 2 \times 10 = 20 枚支払い、50 - 20 = 30 枚残る。
  • 頂点 1 \rightarrow 3: 道中でコインを 45 枚拾う。ゲーム開始から 1 分後に頂点 3 に着き、ボタンを押してコインを 1 \times 10 = 10 枚支払い、45 - 10 = 35 枚残る。

よって、獲得できるスコアの最大値は 35 です。


入力例 2

2 2 10
1 2 100
2 2 100

出力例 2

-1

入力例 2 で与えられるグラフの図

頂点 1 から伸びる辺を通ると頂点 2 に着き、ここで頂点 2 から自分自身へと向かう辺を t 回通ってからボタンを押すとスコアは 90 + 90t となります。よってスコアは無限に高めることができ、獲得できるスコアの最大値は存在しません。


入力例 3

4 5 10
1 2 1
1 4 1
3 4 1
2 2 100
3 3 100

出力例 3

0

入力例 3 で与えられるグラフの図

頂点 1 から頂点 4 へと直接向かう辺を通ること以外に頂点 1 から頂点 4 に移動する方法はありません。この辺の上で 1 枚のコインを拾いますが、ゲーム終了時に 10 枚のコインの支払いを要求されてスコアは 0 となります。

なお、頂点 1 から頂点 2 へと向かう辺を通るとその後コインを無限に拾えますが、頂点 4 に到達してゲームを終了することができなくなるため無意味です。

Score : 500 points

Problem Statement

There is a directed graph with N vertices numbered 1 to N and M edges. The i-th edge is directed from Vertex A_i to Vertex B_i, and there are C_i coins placed along that edge. Additionally, there is a button on Vertex N.

We will play a game on this graph. You start the game on Vertex 1 with zero coins, and head for Vertex N by traversing the edges while collecting coins. It takes one minute to traverse an edge, and you can collect the coins placed along the edge each time you traverse it. As usual in games, even if you traverse an edge once and collect the coins, the same number of coins will reappear next time you traverse that edge, which you can collect again.

When you reach Vertex N, you can end the game by pressing the button. (You can also choose to leave Vertex N without pressing the button and continue traveling.) However, when you end the game, you will be asked to pay T \times P coins, where T is the number of minutes elapsed since the start of the game. If you have less than T \times P coins, you will have to pay all of your coins instead.

Your score will be the number of coins you have after this payment. Determine if there exists a maximum value of the score that can be obtained. If the answer is yes, find that maximum value.

Constraints

  • 2 \leq N \leq 2500
  • 1 \leq M \leq 5000
  • 1 \leq A_i, B_i \leq N
  • 1 \leq C_i \leq 10^5
  • 0 \leq P \leq 10^5
  • All values in input are integers.
  • Vertex N can be reached from Vertex 1.

Input

Input is given from Standard Input in the following format:

N M P
A_1 B_1 C_1
:
A_M B_M C_M

Output

If there exists a maximum value of the score that can be obtained, print that maximum value; otherwise, print -1.


Sample Input 1

3 3 10
1 2 20
2 3 30
1 3 45

Sample Output 1

35

Figure of the graph given in Sample Input 1

There are two ways to travel from Vertex 1 to Vertex 3:

  • Vertex 1 \rightarrow 2 \rightarrow 3: You collect 20 + 30 = 50 coins on the way. After two minutes from the start of the game, you press the button, pay 2 \times 10 = 20 coins, and you have 50 - 20 = 30 coins left.
  • Vertex 1 \rightarrow 2: You collect 45 coins on the way. After one minute from the start of the game, you press the button, pay 1 \times 10 = 10 coins, and you have 45 - 10 = 35 coins left.

Thus, the maximum score that can be obtained is 35.


Sample Input 2

2 2 10
1 2 100
2 2 100

Sample Output 2

-1

Figure of the graph given in Sample Input 2

The edge extending from Vertex 1 takes you to Vertex 2. If you then traverse the edge extending from Vertex 2 to itself t times and press the button, your score will be 90 + 90t. Thus, you can infinitely increase your score, which means there is no maximum value of the score that can be obtained.


Sample Input 3

4 5 10
1 2 1
1 4 1
3 4 1
2 2 100
3 3 100

Sample Output 3

0

Figure of the graph given in Sample Input 3

There is no way to travel from Vertex 1 to Vertex 4 other than traversing the edge leading from Vertex 1 to Vertex 4 directly. You will pick up one coin along this edge, but after being asked to paying 10 coins, your score will be 0.

Note that you can collect an infinite number of coins if you traverse the edge leading from Vertex 1 to Vertex 2, but this is pointless since you can no longer reach Vertex 4 and end the game.

F - Polynomial Construction

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 600

問題文

素数 p と、長さ p01 からなる整数列 a_0, \ldots, a_{p-1} が与えられます。

以下の条件を満たす p-1 次以下の多項式 f(x) = b_{p-1} x^{p-1} + b_{p-2} x^{p-2} + \ldots + b_0 を一つ求めてください。

  • i (0 \leq i \leq p-1) に対し、b_i0 \leq b_i \leq p-1 なる整数
  • i (0 \leq i \leq p-1) に対し、f(i) \equiv a_i \pmod p

制約

  • 2 \leq p \leq 2999
  • p は素数である。
  • 0 \leq a_i \leq 1

入力

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

p
a_0 a_1 \ldots a_{p-1}

出力

条件を満たす多項式 f(x) の一つにおける b_0, b_1, \ldots, b_{p-1} の値をこの順に空白区切りで出力せよ。

なお、解は必ず存在することが示せる。複数の解が存在する場合は、そのうちのどれを出力してもよい。


入力例 1

2
1 0

出力例 1

1 1

f(x) = x + 1 は以下のように条件を満たします。

  • f(0) = 0 + 1 = 1 \equiv 1 \pmod 2
  • f(1) = 1 + 1 = 2 \equiv 0 \pmod 2

入力例 2

3
0 0 0

出力例 2

0 0 0

f(x) = 0 も有効な出力です。


入力例 3

5
0 1 0 1 0

出力例 3

0 2 0 1 3

Score : 600 points

Problem Statement

Given are a prime number p and a sequence of p integers a_0, \ldots, a_{p-1} consisting of zeros and ones.

Find a polynomial of degree at most p-1, f(x) = b_{p-1} x^{p-1} + b_{p-2} x^{p-2} + \ldots + b_0, satisfying the following conditions:

  • For each i (0 \leq i \leq p-1), b_i is an integer such that 0 \leq b_i \leq p-1.
  • For each i (0 \leq i \leq p-1), f(i) \equiv a_i \pmod p.

Constraints

  • 2 \leq p \leq 2999
  • p is a prime number.
  • 0 \leq a_i \leq 1

Input

Input is given from Standard Input in the following format:

p
a_0 a_1 \ldots a_{p-1}

Output

Print b_0, b_1, \ldots, b_{p-1} of a polynomial f(x) satisfying the conditions, in this order, with spaces in between.

It can be proved that a solution always exists. If multiple solutions exist, any of them will be accepted.


Sample Input 1

2
1 0

Sample Output 1

1 1

f(x) = x + 1 satisfies the conditions, as follows:

  • f(0) = 0 + 1 = 1 \equiv 1 \pmod 2
  • f(1) = 1 + 1 = 2 \equiv 0 \pmod 2

Sample Input 2

3
0 0 0

Sample Output 2

0 0 0

f(x) = 0 is also valid.


Sample Input 3

5
0 1 0 1 0

Sample Output 3

0 2 0 1 3