A - Calc

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

整数 a が入力されます。値 a + a^2 + a^3 を出力してください。

制約

  • 1 \leq a \leq 10
  • a は整数である。

入力

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

a

出力

a + a^2 + a^3 を整数として出力せよ。


入力例 1

2

出力例 1

14

a = 2 のとき、a + a^2 + a^3 = 2 + 2^2 + 2^3 = 2 + 4 + 8 = 14 となります。

解答は整数として出力してください。14.0 などの出力は不正解と判定されます。


入力例 2

10

出力例 2

1110

Score : 100 points

Problem Statement

Given an integer a as input, print the value a + a^2 + a^3.

Constraints

  • 1 \leq a \leq 10
  • a is an integer.

Input

Input is given from Standard Input in the following format:

a

Output

Print the value a + a^2 + a^3 as an integer.


Sample Input 1

2

Sample Output 1

14

When a = 2, we have a + a^2 + a^3 = 2 + 2^2 + 2^3 = 2 + 4 + 8 = 14.

Print the answer as an input. Outputs such as 14.0 will be judged as incorrect.


Sample Input 2

10

Sample Output 2

1110
B - Minor Change

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

文字列 S, T が与えられます。次の操作を繰り返して ST に変更するとき、操作回数の最小値を求めてください。

操作:S1 文字を選んで別の文字に書き換える

制約

  • S, T は長さ 1 以上 2\times 10^5 以下
  • S, T は英小文字のみからなる
  • ST の長さは等しい

入力

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

S
T

出力

答えを出力せよ。


入力例 1

cupofcoffee
cupofhottea

出力例 1

4

例えば、次のような 4 回の操作で達成できます。

  • 1 回目:6 文字目の ch に書き換える
  • 2 回目:8 文字目の ft に書き換える
  • 3 回目:9 文字目の ft に書き換える
  • 4 回目:11 文字目の ea に書き換える

入力例 2

abcde
bcdea

出力例 2

5

入力例 3

apple
apple

出力例 3

0

1 度も操作をしなくてもよいこともあります。

Score : 200 points

Problem Statement

Given are strings S and T. Consider changing S to T by repeating the operation below. Find the minimum number of operations required to do so.

Operation: Choose one character of S and replace it with a different character.

Constraints

  • S and T have lengths between 1 and 2\times 10^5 (inclusive).
  • S and T consists of lowercase English letters.
  • S and T have equal lengths.

Input

Input is given from Standard Input in the following format:

S
T

Output

Print the answer.


Sample Input 1

cupofcoffee
cupofhottea

Sample Output 1

4

We can achieve the objective in four operations, such as the following:

  • First, replace the sixth character c with h.
  • Second, replace the eighth character f with t.
  • Third, replace the ninth character f with t.
  • Fourth, replace the eleventh character e with a.

Sample Input 2

abcde
bcdea

Sample Output 2

5

Sample Input 3

apple
apple

Sample Output 3

0

No operations may be needed to achieve the objective.

C - Tsundoku

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

二台の机 A, B があります。机 A には N 冊の本が、机 B には M 冊の本が、それぞれ縦に積まれています。

机 A に現在上から i 番目に積まれている本 (1 \leq i \leq N) は読むのに A_i 分を要し、机 B に現在上から i 番目に積まれている本 (1 \leq i \leq M) は読むのに B_i 分を要します。

次の行為を考えます。

  • 本が残っている机を選び、その机の最も上に積まれた本を読んで机から取り除く。

合計所要時間が K 分を超えないようにこの行為を繰り返すとき、最大で何冊の本を読むことができるでしょうか。本を読むこと以外に要する時間は無視します。

制約

  • 1 \leq N, M \leq 200000
  • 1 \leq K \leq 10^9
  • 1 \leq A_i, B_i \leq 10^9
  • 入力中の値はすべて整数である。

入力

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

N M K
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

出力

読むことのできる本の最大数を表す整数を出力せよ。


入力例 1

3 4 240
60 90 120
80 150 80 150

出力例 1

3

この場合、机 A の上から 1,2,3 番目の本はそれぞれ読むのに 60 分、90 分、120 分を要し、机 B の上から 1,2,3,4 番目の本はそれぞれ読むのに 80 分、150 分、80 分、150 分を要します。

以下のようにすることで 230 分で 3 冊の本を読むことができ、これが 240 分以内に読むことのできる本の最大数です。

  • 机 A の最も上に積まれている本を 60 分かけて読み、この本を机から取り除く。
  • 机 B の最も上に積まれている本を 80 分かけて読み、この本を机から取り除く。
  • 机 A の最も上に積まれている本を 90 分かけて読み、この本を机から取り除く。

入力例 2

3 4 730
60 90 120
80 150 80 150

出力例 2

7

入力例 3

5 4 1
1000000000 1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000

出力例 3

0

整数のオーバーフローに注意してください。

Score : 300 points

Problem Statement

We have two desks: A and B. Desk A has a vertical stack of N books on it, and Desk B similarly has M books on it.

It takes us A_i minutes to read the i-th book from the top on Desk A (1 \leq i \leq N), and B_i minutes to read the i-th book from the top on Desk B (1 \leq i \leq M).

Consider the following action:

  • Choose a desk with a book remaining, read the topmost book on that desk, and remove it from the desk.

How many books can we read at most by repeating this action so that it takes us at most K minutes in total? We ignore the time it takes to do anything other than reading.

Constraints

  • 1 \leq N, M \leq 200000
  • 1 \leq K \leq 10^9
  • 1 \leq A_i, B_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

Output

Print an integer representing the maximum number of books that can be read.


Sample Input 1

3 4 240
60 90 120
80 150 80 150

Sample Output 1

3

In this case, it takes us 60, 90, 120 minutes to read the 1-st, 2-nd, 3-rd books from the top on Desk A, and 80, 150, 80, 150 minutes to read the 1-st, 2-nd, 3-rd, 4-th books from the top on Desk B, respectively.

We can read three books in 230 minutes, as shown below, and this is the maximum number of books we can read within 240 minutes.

  • Read the topmost book on Desk A in 60 minutes, and remove that book from the desk.
  • Read the topmost book on Desk B in 80 minutes, and remove that book from the desk.
  • Read the topmost book on Desk A in 90 minutes, and remove that book from the desk.

Sample Input 2

3 4 730
60 90 120
80 150 80 150

Sample Output 2

7

Sample Input 3

5 4 1
1000000000 1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000

Sample Output 3

0

Watch out for integer overflows.

D - Sum of Divisors

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 400

問題文

正整数 X に対し、X の正の約数の個数を f(X) とします。

正整数 N が与えられるので、\sum_{K=1}^N K\times f(K) を求めてください。

制約

  • 1 \leq N \leq 10^7

入力

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

N

出力

\sum_{K=1}^N K\times f(K) を出力せよ。


入力例 1

4

出力例 1

23

f(1)=1, f(2)=2, f(3)=2, f(4)=3 なので、答えは 1\times 1 + 2\times 2 + 3\times 2 + 4\times 3 =23 となります。


入力例 2

100

出力例 2

26879

入力例 3

10000000

出力例 3

838627288460105

オーバーフローに注意してください。

Score : 400 points

Problem Statement

For a positive integer X, let f(X) be the number of positive divisors of X.

Given a positive integer N, find \sum_{K=1}^N K\times f(K).

Constraints

  • 1 \leq N \leq 10^7

Input

Input is given from Standard Input in the following format:

N

Output

Print the value \sum_{K=1}^N K\times f(K).


Sample Input 1

4

Sample Output 1

23

We have f(1)=1, f(2)=2, f(3)=2, and f(4)=3, so the answer is 1\times 1 + 2\times 2 + 3\times 2 + 4\times 3 =23.


Sample Input 2

100

Sample Output 2

26879

Sample Input 3

10000000

Sample Output 3

838627288460105

Watch out for overflows.

E - NEQ

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

1 以上 M 以下の整数からなる長さ N の数列 A_1,A_2,\cdots, A_{N}B_1,B_2,\cdots, B_{N} の組であって、以下の条件をすべて満たすものの個数を求めてください。

  • 1\leq i\leq N なる任意の i について A_i \neq B_i
  • 1\leq i < j\leq N なる任意の (i,j) について A_i \neq A_j かつ B_i \neq B_j

ただし、答えは非常に大きくなる可能性があるので、(10^9+7) で割ったあまりを出力してください。

制約

  • 1\leq N \leq M \leq 5\times10^5
  • 入力はすべて整数

入力

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

N M

出力

答えを (10^9+7) で割ったあまりを出力せよ。


入力例 1

2 2

出力例 1

2

A_1=1,A_2=2,B_1=2,B_2=1 のときと A_1=2,A_2=1,B_1=1,B_2=2 のとき条件が満たされます。


入力例 2

2 3

出力例 2

18

入力例 3

141421 356237

出力例 3

881613484

Score : 500 points

Problem Statement

Count the pairs of length-N sequences consisting of integers between 1 and M (inclusive), A_1, A_2, \cdots, A_{N} and B_1, B_2, \cdots, B_{N}, that satisfy all of the following conditions:

  • A_i \neq B_i, for every i such that 1\leq i\leq N.
  • A_i \neq A_j and B_i \neq B_j, for every (i, j) such that 1\leq i < j\leq N.

Since the count can be enormous, print it modulo (10^9+7).

Constraints

  • 1\leq N \leq M \leq 5\times10^5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the count modulo (10^9+7).


Sample Input 1

2 2

Sample Output 1

2

A_1=1,A_2=2,B_1=2,B_2=1 and A_1=2,A_2=1,B_1=1,B_2=2 satisfy the conditions.


Sample Input 2

2 3

Sample Output 2

18

Sample Input 3

141421 356237

Sample Output 3

881613484
F - Unfair Nim

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

石の山が N 個あり、i 番目の山には A_i 個の石があります。

これを使って青木君と高橋君が次のようなゲームをしようとしています。

  • 青木君から交互に、次の操作を繰り返す
    • 操作:石の山を 1 つ選び、そこから 1 個以上の石を取り除く
  • 先に操作ができなくなった方の負け

このゲームは、両者が最適に行動する場合、ゲーム開始時の各山の石の個数のみによって、先手必勝か後手必勝かが決まります。

そこで高橋君は、ゲームを始める前に、 1 番目の山から 0 個以上 A_1 個未満の石を 2 番目の山に移すことで、後手の高橋君が必勝となるようにしようとしています。

そのようなことが可能なら移動する石の個数の最小値を、不可能ならかわりに -1 を出力してください。

制約

  • 2 \leq N \leq 300
  • 1 \leq A_i \leq 10^{12}

入力

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

N
A_1 \ldots A_N

出力

移動する石の個数の最小値を出力せよ。不可能ならかわりに -1 を出力せよ。


入力例 1

2
5 3

出力例 1

1

石の移動をしなかった場合、青木君が最初に 1 番目の山から 2 個の石を取り除くと、高橋君はその後どのように行動しても負けてしまいます。

ゲームを始める前に 1 番目の山から 1 個の石を移動して、石の個数を 4,4 とした場合、適切な行動を取ることで、高橋君は必ず勝つことができます。


入力例 2

2
3 5

出力例 2

-1

2 番目の山から 1 番目の山へ石を移すことはできません。


入力例 3

3
1 1 2

出力例 3

-1

1 番目の山のすべての石を移すことはできません。


入力例 4

8
10 9 8 7 6 5 4 3

出力例 4

3

入力例 5

3
4294967297 8589934593 12884901890

出力例 5

1

オーバーフローに注意してください。

Score : 600 points

Problem Statement

There are N piles of stones. The i-th pile has A_i stones.

Aoki and Takahashi are about to use them to play the following game:

  • Starting with Aoki, the two players alternately do the following operation:
    • Operation: Choose one pile of stones, and remove one or more stones from it.
  • When a player is unable to do the operation, he loses, and the other player wins.

When the two players play optimally, there are two possibilities in this game: the player who moves first always wins, or the player who moves second always wins, only depending on the initial number of stones in each pile.

In such a situation, Takahashi, the second player to act, is trying to guarantee his win by moving at least zero and at most (A_1 - 1) stones from the 1-st pile to the 2-nd pile before the game begins.

If this is possible, print the minimum number of stones to move to guarantee his victory; otherwise, print -1 instead.

Constraints

  • 2 \leq N \leq 300
  • 1 \leq A_i \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

Print the minimum number of stones to move to guarantee Takahashi's win; otherwise, print -1 instead.


Sample Input 1

2
5 3

Sample Output 1

1

Without moving stones, if Aoki first removes 2 stones from the 1-st pile, Takahashi cannot win in any way.

If Takahashi moves 1 stone from the 1-st pile to the 2-nd before the game begins so that both piles have 4 stones, Takahashi can always win by properly choosing his actions.


Sample Input 2

2
3 5

Sample Output 2

-1

It is not allowed to move stones from the 2-nd pile to the 1-st.


Sample Input 3

3
1 1 2

Sample Output 3

-1

It is not allowed to move all stones from the 1-st pile.


Sample Input 4

8
10 9 8 7 6 5 4 3

Sample Output 4

3

Sample Input 5

3
4294967297 8589934593 12884901890

Sample Output 5

1

Watch out for overflows.