A - Number of Multiples

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

L 以上 R 以下の整数のうち、d の倍数であるようなものはいくつありますか?

制約

  • 与えられる入力は全て整数
  • 1 \leq L \leq R \leq 100
  • 1 \leq d \leq 100

入力

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

L R d

出力

L 以上 R 以下の整数のうち、d の倍数であるような整数の個数を出力せよ。


入力例 1

5 10 2

出力例 1

3
  • 5 以上 10 以下の整数のうち、2 の倍数であるようなものは、6,8,103 つです。

入力例 2

6 20 7

出力例 2

2
  • 6 以上 20 以下の整数のうち、7 の倍数であるようなものは、7,142 つです。

入力例 3

1 100 1

出力例 3

100

Score : 100 points

Problem Statement

How many multiples of d are there among the integers between L and R (inclusive)?

Constraints

  • All values in input are integers.
  • 1 \leq L \leq R \leq 100
  • 1 \leq d \leq 100

Input

Input is given from Standard Input in the following format:

L R d

Output

Print the number of multiples of d among the integers between L and R (inclusive).


Sample Input 1

5 10 2

Sample Output 1

3
  • Among the integers between 5 and 10, there are three multiples of 2: 6, 8, and 10.

Sample Input 2

6 20 7

Sample Output 2

2
  • Among the integers between 6 and 20, there are two multiples of 7: 7 and 14.

Sample Input 3

1 100 1

Sample Output 3

100
B - An Odd Problem

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

1,2,3,\ldots,N の番号がついた N 個のマスがあります。各マスには整数が書かれており、マス i には a_i が書かれています。

以下の 2 つの条件の両方を満たすマスはいくつありますか?

  • マスの番号は奇数
  • マスに書かれた整数は奇数

制約

  • 与えられる入力は全て整数
  • 1 \leq N, a_i \leq 100

入力

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

N
a_1 a_2 \cdots a_N

出力

問題文中の 2 つの条件の両方を満たすマスの個数を出力せよ。


入力例 1

5
1 3 4 5 7

出力例 1

2
  • 条件を満たすマスはマス 1,52 つです。
  • マス 2,4 はマスの番号は奇数、という条件に違反しています。
  • マス 3 はマスに書かれた整数は奇数、という条件に違反しています。

入力例 2

15
13 76 46 15 50 98 93 77 31 43 84 90 6 24 14

出力例 2

3

Score : 200 points

Problem Statement

We have N squares assigned the numbers 1,2,3,\ldots,N. Each square has an integer written on it, and the integer written on Square i is a_i.

How many squares i satisfy both of the following conditions?

  • The assigned number, i, is odd.
  • The written integer is odd.

Constraints

  • All values in input are integers.
  • 1 \leq N, a_i \leq 100

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \cdots a_N

Output

Print the number of squares that satisfy both of the conditions.


Sample Input 1

5
1 3 4 5 7

Sample Output 1

2
  • Two squares, Square 1 and 5, satisfy both of the conditions.
  • For Square 2 and 4, the assigned numbers are not odd.
  • For Square 3, the written integer is not odd.

Sample Input 2

15
13 76 46 15 50 98 93 77 31 43 84 90 6 24 14

Sample Output 2

3
C - XYZ Triplets

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

f(n) を以下の 2 つの条件の両方を満たすような 3 つの整数の組 (x,y,z) の個数とします。

  • 1 \leq x,y,z
  • x^2 + y^2 + z^2 + xy + yz + zx = n

整数 N が与えられるので、f(1),f(2),f(3),\ldots,f(N) をそれぞれ求めてください。

制約

  • 与えられる入力は全て整数
  • 1 \leq N \leq 10^4

入力

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

N

出力

N 行出力せよ。i 行目には f(i) の値を出力せよ。


入力例 1

20

出力例 1

0
0
0
0
0
1
0
0
0
0
3
0
0
0
0
0
3
3
0
0
  • n=6 において、(1,1,1) のみが問題文中の 2 つの条件の両方を満たします。よって f(6)1 です。
  • n=11 において、(1,1,2),(1,2,1),(2,1,1)3 つが問題文中の 2 つの条件の両方を満たします。よって f(11)3 です。
  • n=17 において、(1,2,2),(2,1,2),(2,2,1)3 つが問題文中の 2 つの条件の両方を満たします。よって f(17)3 です。
  • n=18 において、(1,1,3),(1,3,1),(3,1,1)3 つが問題文中の 2 つの条件の両方を満たします。よって f(18)3 です。

Score : 300 points

Problem Statement

Let f(n) be the number of triples of integers (x,y,z) that satisfy both of the following conditions:

  • 1 \leq x,y,z
  • x^2 + y^2 + z^2 + xy + yz + zx = n

Given an integer N, find each of f(1),f(2),f(3),\ldots,f(N).

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 10^4

Input

Input is given from Standard Input in the following format:

N

Output

Print N lines. The i-th line should contain the value f(i).


Sample Input 1

20

Sample Output 1

0
0
0
0
0
1
0
0
0
0
3
0
0
0
0
0
3
3
0
0
  • For n=6, only (1,1,1) satisfies both of the conditions. Thus, f(6) = 1.
  • For n=11, three triples, (1,1,2), (1,2,1), and (2,1,1), satisfy both of the conditions. Thus, f(6) = 3.
  • For n=17, three triples, (1,2,2), (2,1,2), and (2,2,1), satisfy both of the conditions. Thus, f(17) = 3.
  • For n=18, three triples, (1,1,3), (1,3,1), and (3,1,1), satisfy both of the conditions. Thus, f(18) = 3.
D - Anything Goes to Zero

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

\mathrm{popcount}(n)n2 進表記したときの 1 の個数とします。 例えば、\mathrm{popcount}(3) = 2, \mathrm{popcount}(7) = 3, \mathrm{popcount}(0) = 0 です。

f(n) を、「n\mathrm{popcount}(n) で割ったあまりに置き換える」という操作を繰り返した際に n0 になるまでの操作回数とします(この問題の制約下で n が有限回の操作後に必ず 0 になることが証明できます)。

以下は n=7 の例で、2 回の操作で n0 になります。

  • \mathrm{popcount}(7)=3 なので、73 で割ったあまりである 1 に置き換えます。
  • \mathrm{popcount}(1)=1 なので、11 で割ったあまりである 0 に置き換えます。

2 進表記で N 桁の整数 X が与えられます。 1 \leq i \leq N を満たす整数 i について、X の上から i 桁目のビットを反転した整数を X_i とします。 f(X_1), f(X_2), \ldots, f(X_N) を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • X2 進表記で N 桁の(先頭の桁が 1 とは限らない)整数

入力

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

N
X

出力

N 行出力せよ。i 行目には f(X_i) の値を出力せよ。


入力例 1

3
011

出力例 1

2
1
1
  • X_1 = 7 です。7 \rightarrow 1 \rightarrow 0 となるので f(7) = 2 です。
  • X_2 = 1 です。1 \rightarrow 0 となるので f(1) = 1 です。
  • X_3 = 2 です。2 \rightarrow 0 となるので f(2) = 1 です。

入力例 2

23
00110111001011011001110

出力例 2

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

Score : 400 points

Problem Statement

Let \mathrm{popcount}(n) be the number of 1s in the binary representation of n. For example, \mathrm{popcount}(3) = 2, \mathrm{popcount}(7) = 3, and \mathrm{popcount}(0) = 0.

Let f(n) be the number of times the following operation will be done when we repeat it until n becomes 0: "replace n with the remainder when n is divided by \mathrm{popcount}(n)." (It can be proved that, under the constraints of this problem, n always becomes 0 after a finite number of operations.)

For example, when n=7, it becomes 0 after two operations, as follows:

  • \mathrm{popcount}(7)=3, so we divide 7 by 3 and replace it with the remainder, 1.
  • \mathrm{popcount}(1)=1, so we divide 1 by 1 and replace it with the remainder, 0.

You are given an integer X with N digits in binary. For each integer i such that 1 \leq i \leq N, let X_i be what X becomes when the i-th bit from the top is inverted. Find f(X_1), f(X_2), \ldots, f(X_N).

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • X is an integer with N digits in binary, possibly with leading zeros.

Input

Input is given from Standard Input in the following format:

N
X

Output

Print N lines. The i-th line should contain the value f(X_i).


Sample Input 1

3
011

Sample Output 1

2
1
1
  • X_1 = 7, which will change as follows: 7 \rightarrow 1 \rightarrow 0. Thus, f(7) = 2.
  • X_2 = 1, which will change as follows: 1 \rightarrow 0. Thus, f(1) = 1.
  • X_3 = 2, which will change as follows: 2 \rightarrow 0. Thus, f(2) = 1.

Sample Input 2

23
00110111001011011001110

Sample Output 2

2
1
2
2
1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
1
3
E - Camel Train

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

1,2,\ldots,N の番号がついた N 頭のラクダがいます。 すぬけ君はラクダたちを一列に並べることにしました。

ラクダ i が先頭から K_i 番目以内にいるときのうれしさは L_i です。 そうでない場合のうれしさは R_i です。

すぬけ君はラクダたちのうれしさの総和を最大化したいです。 ラクダたちのうれしさの総和としてありうる値のうち最大値を求めてください。

テストケースは T 個与えられるので、それぞれについて答えを求めてください。

制約

  • 与えられる入力は全て整数
  • 1 \leq T \leq 10^5
  • 1 \leq N \leq 2 \times 10^{5}
  • 1 \leq K_i \leq N
  • 1 \leq L_i, R_i \leq 10^9
  • 1 つの入力ファイルにおいて、N の総和は 2 \times 10^5 を超えない。

入力

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

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

各ケースは以下の形式で与えられる。

N
K_1 L_1 R_1
\vdots
K_N L_N R_N

出力

T 行出力せよ。i 行目には i 番目のテストケースに対する答えを出力せよ。


入力例 1

3
2
1 5 10
2 15 5
3
2 93 78
1 71 59
3 57 96
19
19 23 16
5 90 13
12 85 70
19 67 78
12 16 60
18 48 28
5 4 24
12 97 97
4 57 87
19 91 74
18 100 76
7 86 46
9 100 57
3 76 73
6 84 93
1 6 84
11 75 94
19 15 3
12 11 34

出力例 1

25
221
1354
  • 1 番目のテストケースにおいて、ラクダ 2,1 の順で並べるのが最適です。
    • ラクダ 1 は先頭から 1 番目以内にいないのでうれしさは 10 です。
    • ラクダ 2 は先頭から 2 番目以内にいるのでうれしさは 15 です。
  • 2 番目のテストケースにおいて、ラクダ 2,1,3 の順で並べるのが最適です。
    • ラクダ 1 は先頭から 2 番目以内にいるのでうれしさは 93 です。
    • ラクダ 2 は先頭から 1 番目以内にいるのでうれしさは 71 です。
    • ラクダ 3 は先頭から 3 番目以内にいるのでうれしさは 57 です。

Score : 500 points

Problem Statement

We have N camels numbered 1,2,\ldots,N. Snuke has decided to make them line up in a row.

The happiness of Camel i will be L_i if it is among the K_i frontmost camels, and R_i otherwise.

Snuke wants to maximize the total happiness of the camels. Find the maximum possible total happiness of the camel.

Solve this problem for each of the T test cases given.

Constraints

  • All values in input are integers.
  • 1 \leq T \leq 10^5
  • 1 \leq N \leq 2 \times 10^{5}
  • 1 \leq K_i \leq N
  • 1 \leq L_i, R_i \leq 10^9
  • The sum of values of N in each input file is at most 2 \times 10^5.

Input

Input is given from Standard Input in the following format:

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

Each case is given in the following format:

N
K_1 L_1 R_1
\vdots
K_N L_N R_N

Output

Print T lines. The i-th line should contain the answer to the i-th test case.


Sample Input 1

3
2
1 5 10
2 15 5
3
2 93 78
1 71 59
3 57 96
19
19 23 16
5 90 13
12 85 70
19 67 78
12 16 60
18 48 28
5 4 24
12 97 97
4 57 87
19 91 74
18 100 76
7 86 46
9 100 57
3 76 73
6 84 93
1 6 84
11 75 94
19 15 3
12 11 34

Sample Output 1

25
221
1354
  • In the first test case, it is optimal to line up the camels in the order 2, 1.
    • Camel 1 is not the frontmost camel, so its happiness will be 10.
    • Camel 2 is among the two frontmost camels, so its happiness will be 15.
  • In the second test case, it is optimal to line up the camels in the order 2, 1, 3.
    • Camel 1 is among the two frontmost camels, so its happiness will be 93.
    • Camel 2 is the frontmost camel, so its happiness will be 71.
    • Camel 3 is among the three frontmost camels, so its happiness will be 57.
F - Two Snuke

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

整数 N が与えられます。すぬけ君は整数 s_1,s_2,n_1,n_2,u_1,u_2,k_1,k_2,e_1,e_2 を以下の 6 つの条件の全てを満たすように選びます。

  • 0 \leq s_1 < s_2
  • 0 \leq n_1 < n_2
  • 0 \leq u_1 < u_2
  • 0 \leq k_1 < k_2
  • 0 \leq e_1 < e_2
  • s_1 + s_2 + n_1 + n_2 + u_1 + u_2 + k_1 + k_2 + e_1 + e_2 \leq N

ありうる (s_1,s_2,n_1,n_2,u_1,u_2,k_1,k_2,e_1,e_2) の組すべてについて (s_2 − s_1)(n_2 − n_1)(u_2 − u_1)(k_2 - k_1)(e_2 - e_1) を計算し、その総和を (10^{9} +7) で割ったあまりを求めてください。

テストケースは T 個与えられるので、それぞれについて答えを求めてください。

制約

  • 与えられる入力は全て整数
  • 1 \leq T \leq 100
  • 1 \leq N \leq 10^{9}

入力

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

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

各ケースは以下の形式で与えられる。

N

出力

T 行出力せよ。i 行目には i 番目のテストケースに対する答えを出力せよ。


入力例 1

4
4
6
10
1000000000

出力例 1

0
11
4598
257255556
  • N=4 のとき (s_1,s_2,n_1,n_2,u_1,u_2,k_1,k_2,e_1,e_2) としてありうる組は存在しません。よって、答えは 0 です。
  • N=6 のとき (s_1,s_2,n_1,n_2,u_1,u_2,k_1,k_2,e_1,e_2) としてありうる組は以下の 6 通りです。
    • (0,1,0,1,0,1,0,1,0,1)
    • (0,2,0,1,0,1,0,1,0,1)
    • (0,1,0,2,0,1,0,1,0,1)
    • (0,1,0,1,0,2,0,1,0,1)
    • (0,1,0,1,0,1,0,2,0,1)
    • (0,1,0,1,0,1,0,1,0,2)
  • (s_2 − s_1)(n_2 − n_1)(u_2 − u_1)(k_2 - k_1)(e_2 - e_1) の値が 1 となるものが 1 通り、2 となるものが 5 通りあるので、答えは 11 です。
  • (10^{9} +7) で割ったあまりを求める必要があることに注意してください。

Score : 600 points

Problem Statement

Given is an integer N. Snuke will choose integers s_1, s_2, n_1, n_2, u_1, u_2, k_1, k_2, e_1, and e_2 so that all of the following six conditions will be satisfied:

  • 0 \leq s_1 < s_2
  • 0 \leq n_1 < n_2
  • 0 \leq u_1 < u_2
  • 0 \leq k_1 < k_2
  • 0 \leq e_1 < e_2
  • s_1 + s_2 + n_1 + n_2 + u_1 + u_2 + k_1 + k_2 + e_1 + e_2 \leq N

For every possible choice (s_1,s_2,n_1,n_2,u_1,u_2,k_1,k_2,e_1,e_2), compute (s_2 − s_1)(n_2 − n_1)(u_2 − u_1)(k_2 - k_1)(e_2 - e_1), and find the sum of all computed values, modulo (10^{9} +7).

Solve this problem for each of the T test cases given.

Constraints

  • All values in input are integers.
  • 1 \leq T \leq 100
  • 1 \leq N \leq 10^{9}

Input

Input is given from Standard Input in the following format:

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

Each case is given in the following format:

N

Output

Print T lines. The i-th line should contain the answer to the i-th test case.


Sample Input 1

4
4
6
10
1000000000

Sample Output 1

0
11
4598
257255556
  • When N=4, there is no possible choice (s_1,s_2,n_1,n_2,u_1,u_2,k_1,k_2,e_1,e_2). Thus, the answer is 0.
  • When N=6, there are six possible choices (s_1,s_2,n_1,n_2,u_1,u_2,k_1,k_2,e_1,e_2) as follows:
    • (0,1,0,1,0,1,0,1,0,1)
    • (0,2,0,1,0,1,0,1,0,1)
    • (0,1,0,2,0,1,0,1,0,1)
    • (0,1,0,1,0,2,0,1,0,1)
    • (0,1,0,1,0,1,0,2,0,1)
    • (0,1,0,1,0,1,0,1,0,2)
  • We have one choice where (s_2 − s_1)(n_2 − n_1)(u_2 − u_1)(k_2 - k_1)(e_2 - e_1) is 1 and five choices where (s_2 − s_1)(n_2 − n_1)(u_2 − u_1)(k_2 - k_1)(e_2 - e_1) is 2, so the answer is 11.
  • Be sure to find the sum modulo (10^{9} +7).