A - Arithmetic Sequence

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

配点 : 300

問題文

3 項からなる整数列 A = (A_1, A_2, A_3) が与えられます。あなたはこの数列に対して、次の操作を何回でも行うことができます:

  • i\in \{1,2,3\} をひとつ選び、A_i1 を加える。

数列 A を等差数列にするために必要な操作回数の最小値を求めてください。ただし、数列 A = (A_1, A_2, A_3) が等差数列であるとは、A_2 - A_1 = A_3 - A_2 が成り立つことを意味します。

制約

  • 1\leq A_1, A_2, A_3\leq 10^{15}

入力

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

A_1 A_2 A_3

出力

答えを出力してください。


入力例 1

4 8 10

出力例 1

2

i = 1i = 3 に対して 1 回ずつ操作を行うと、等差数列 (5, 8, 11) が得られます。


入力例 2

10 3 4

出力例 2

4

i = 2 に対して 4 回の操作を行うと、等差数列 (10, 7, 4) が得られます。


入力例 3

1 2 3

出力例 3

0

数列 A ははじめから等差数列なので、最小の操作回数は 0 回となります。


入力例 4

1000000000000000 1 1000000000000000

出力例 4

999999999999999

Score : 300 points

Problem Statement

Given is a sequence of three integers A = (A_1, A_2, A_3). On this sequence, you can do the following operation any number of times:

  • choose i\in \{1,2,3\} and add 1 to A_i.

Find the minimum number of operations needed to make A arithmetic. Here, the sequence A = (A_1, A_2, A_3) is arithmetic when A_2 - A_1 = A_3 - A_2 holds.

Constraints

  • 1\leq A_1, A_2, A_3\leq 10^{15}

Input

Input is given from Standard Input in the following format:

A_1 A_2 A_3

Output

Print the answer.


Sample Input 1

4 8 10

Sample Output 1

2

One operation with i = 1 and then one operation with i = 3 yield an arithmetic sequence (5, 8, 11).


Sample Input 2

10 3 4

Sample Output 2

4

Four operations with i = 2 yield an arithmetic sequence (10, 7, 4).


Sample Input 3

1 2 3

Sample Output 3

0

The sequence A is already arithmetic from the beginning, so we need zero operations.


Sample Input 4

1000000000000000 1 1000000000000000

Sample Output 4

999999999999999
B - Increasing Triples

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

配点 : 400

問題文

N 項からなる整数列 A = (A_1, \ldots, A_N),\,B = (B_1, \ldots, B_N),\,C = (C_1, \ldots, C_N) が与えられます。

あなたはそれぞれの数列を、自由に並べ替えることができます。 並べ替えた結果、A_i < B_i < C_i を満たす i の個数が最大でいくつになるかを答えてください。

制約

  • 1\leq N\leq 10^5
  • 1\leq A_i, B_i, C_i\leq 10^9

入力

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

N
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N
C_1 C_2 \ldots C_N

出力

答えを出力してください。


入力例 1

5
9 6 14 1 8
2 10 3 12 11
15 13 5 7 4

出力例 1

3

次のように並べ替えます:

  • A = (1,6,8,9,14)
  • B = (3,2,10,12,11)
  • C = (4,7,15,13,5)

このとき 3 つの ii = 1, 3, 4)に対して A_i < B_i < C_i が成り立ちます。


入力例 2

1
10
20
30

出力例 2

1

入力例 3

3
1 1 1
1 1 2
2 2 2

出力例 3

0

Score : 400 points

Problem Statement

Given are three sequences of N integers each: A = (A_1, \ldots, A_N),\,B = (B_1, \ldots, B_N),\,C = (C_1, \ldots, C_N).

You can permute each of these sequences in any way you like. Find the maximum possible number of indices i such that A_i < B_i < C_i after permuting them.

Constraints

  • 1\leq N\leq 10^5
  • 1\leq A_i, B_i, C_i\leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N
C_1 C_2 \ldots C_N

Output

Print the answer.


Sample Input 1

5
9 6 14 1 8
2 10 3 12 11
15 13 5 7 4

Sample Output 1

3

We should permute them as follows:

  • A = (1,6,8,9,14),
  • B = (3,2,10,12,11),
  • C = (4,7,15,13,5).

Then, we will have three indices i (i = 1, 3, 4) such that A_i < B_i < C_i.


Sample Input 2

1
10
20
30

Sample Output 2

1

Sample Input 3

3
1 1 1
1 1 2
2 2 2

Sample Output 3

0
C - 1, 2, 3 - Decomposition

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

配点 : 600

問題文

正の整数 N が与えられます。整数列 A = (A_1, \ldots, A_K) であって以下の条件を満たすものを考えます:

  • \sum_{i=1}^K A_i = N
  • A_i は正の整数で、10 進法表記したときどの桁の値も 1, 2, 3 のいずれかである。

そのような A の要素数 K として考えられる最小の値を答えてください。

一つの入力ファイルにつき、T 個のテストケースに答えてください。

制約

  • 1\leq T\leq 1000
  • 1\leq N\leq 10^{18}

入力

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

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

各テストケースは以下の形式で与えられます。

N

出力

答えを出力してください。


入力例 1

5
456
10000
123
314
91

出力例 1

2
4
1
2
4

それぞれの N に対して、最適な A の一例は以下の通りです:

  • N = 456 の場合:A = (133, 323)
  • N = 10000 の場合:A = (323, 3132, 3232, 3313)
  • N = 123 の場合:A = (123)
  • N = 314 の場合:A = (312,2)
  • N = 91 の場合:A = (22,23,23,23)

Score : 600 points

Problem Statement

Given is a positive integer N. Consider a sequence of integers A = (A_1, \ldots, A_K) that satisfies the conditions below:

  • \sum_{i=1}^K A_i = N;
  • each A_i is a positive integer such that every digit in its decimal notation is 1, 2, or 3.

Find the minimum possible value of K, that is, the number of elements in such a sequence A.

Process T test cases per input file.

Constraints

  • 1\leq T\leq 1000
  • 1\leq N\leq 10^{18}

Input

Input is given from Standard Input in the following format:

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

Each case is in the following format:

N

Output

Print the answers.


Sample Input 1

5
456
10000
123
314
91

Sample Output 1

2
4
1
2
4

For each N, one optimal A is shown below.

  • For N = 456: A = (133, 323).
  • For N = 10000: A = (323, 3132, 3232, 3313).
  • For N = 123: A = (123).
  • For N = 314: A = (312,2).
  • For N = 91: A = (22,23,23,23).
D - Inc, Dec - Decomposition

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

配点 : 700

問題文

整数列 A = (A_1, \ldots, A_N) が与えられます。

整数列 B = (B_1, \ldots, B_N) および C = (C_1, \ldots, C_N) の組であって、以下の条件を満たすものを考えます:

  • 1\leq i\leq N に対して A_i = B_i + C_i が成り立つ。
  • B は広義単調増加である。つまり 1\leq i\leq N-1 に対して B_i\leq B_{i+1} が成り立つ。
  • C は広義単調減少である。つまり 1\leq i\leq N-1 に対して C_i\geq C_{i+1} が成り立つ。

\sum_{i=1}^N \bigl(\lvert B_i\rvert + \lvert C_i\rvert\bigr) としてありうる最小値を求めてください。

制約

  • 1\leq N\leq 2\times 10^5
  • -10^8\leq A_i\leq 10^8

入力

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

N
A_1 A_2 \ldots A_N

出力

答えを出力してください。


入力例 1

3
1 -2 3

出力例 1

10

最小値を与える整数列 B, C として、例えば次があります:

  • B = (0, 0, 5)
  • C = (1, -2, -2)

\sum_{i=1}^N \bigl(\lvert B_i\rvert + \lvert C_i\rvert\bigr) = (0+1) + (0+2) + (5+2) = 10 となっています。


入力例 2

4
5 4 3 5

出力例 2

17

最小値を与える整数列 B, C として、例えば次があります:

  • B = (0, 1, 2, 4)
  • C = (5, 3, 1, 1)

入力例 3

1
-10

出力例 3

10

最小値を与える整数列 B, C として、例えば次があります:

  • B = (-3)
  • C = (-7)

Score : 700 points

Problem Statement

Given is a sequence of integers A = (A_1, \ldots, A_N).

Consider a pair of sequences of integers B = (B_1, \ldots, B_N) and C = (C_1, \ldots, C_N) that satisfies the following conditions:

  • A_i = B_i + C_i holds for each 1\leq i\leq N;
  • B is non-decreasing, that is, B_i\leq B_{i+1} holds for each 1\leq i\leq N-1;
  • C is non-increasing, that is, C_i\geq C_{i+1} holds for each 1\leq i\leq N-1.

Find the minimum possible value of \sum_{i=1}^N \bigl(\lvert B_i\rvert + \lvert C_i\rvert\bigr).

Constraints

  • 1\leq N\leq 2\times 10^5
  • -10^8\leq A_i\leq 10^8

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N

Output

Print the answer.


Sample Input 1

3
1 -2 3

Sample Output 1

10

One pair of sequences B = (B_1, \ldots, B_N) and C = (C_1, \ldots, C_N) that yields the minimum value is:

  • B = (0, 0, 5),
  • C = (1, -2, -2).

Here we have \sum_{i=1}^N \bigl(\lvert B_i\rvert + \lvert C_i\rvert\bigr) = (0+1) + (0+2) + (5+2) = 10.


Sample Input 2

4
5 4 3 5

Sample Output 2

17

One pair of sequences B and C that yields the minimum value is:

  • B = (0, 1, 2, 4),
  • C = (5, 3, 1, 1).

Sample Input 3

1
-10

Sample Output 3

10

One pair of sequences B and C that yields the minimum value is:

  • B = (-3),
  • C = (-7).
E - Training

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

配点 : 700

問題文

X さんと Y さんの 2 人のプログラマが、競技プログラミングを始めることになりました。

競技プログラミングの実力は、「レベル」と呼ばれる正の整数で表され、はじめ X さんのレベルは A_XY さんのレベルは A_Y です。2 人はこれから練習メニューをこなすことで、レベルを上げていきます。

2 人のレベルの上がり方について、次のことが分かっています:

  • X さんはちょうど B_X 個の練習メニューをこなすたびに、レベルがひとつ上がります。
  • Y さんはちょうど B_Y 個の練習メニューをこなすたびに、レベルがひとつ上がります。

n = 1, 2, \ldots, N のうちで次を満たすものはいくつあるかを答えてください。

  • 2 人がちょうど n 個ずつの練習メニューをこなした場合、2 人の最終的なレベルは等しくなる。

一つの入力ファイルにつき、T 個のテストケースに答えてください。

制約

  • 1\leq T\leq 2\times 10^5
  • 1\leq N\leq 10^{9}
  • 1\leq A_X, B_X, A_Y, B_Y \leq 10^6

入力

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

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

各テストケースは以下の形式で与えられます。

N A_X B_X A_Y B_Y

出力

答えを出力してください。


入力例 1

5
10 5 3 4 2
5 5 3 4 2
100 5 3 4 2
10 5 3 4 3
10 5 10 5 9

出力例 1

6
3
6
0
9

ひとつめのテストケースについて説明します。

n = 1, 2, \ldots, 10 に対して、n 個の練習メニューをこなした場合の 2 人のレベルは次のようになります:

  • X さんのレベル:5, 5, 6, 6, 6, 7, 7, 7, 8, 8
  • Y さんのレベル:4, 5, 5, 6, 6, 7, 7, 8, 8, 9

6 個の nn = 2, 4, 5, 6, 7, 9)の場合に 2 人のレベルが等しくなります。したがって答えは 6 となります。

Score : 700 points

Problem Statement

Two programmers, X and Y, are going to start competitive programming.

One's skill in competitive programming is represented by a positive integer called the level. Initially, X's level is A_X, and Y's level is A_Y. The two will do learning tasks to raise their levels.

We know that they level up as follows.

  • X's level raises by one after every B_X learning tasks.
  • Y's level raises by one after every B_Y learning tasks.

How many among n = 1, 2, \ldots, N satisfy the following?

  • X's level and Y's level are equal when each of them has done exactly n learning tasks.

Process T test cases per input file.

Constraints

  • 1\leq T\leq 2\times 10^5
  • 1\leq N\leq 10^{9}
  • 1\leq A_X, B_X, A_Y, B_Y \leq 10^6

Input

Input is given from Standard Input in the following format:

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

Each case is in the following format:

N A_X B_X A_Y B_Y

Output

Print the answers.


Sample Input 1

5
10 5 3 4 2
5 5 3 4 2
100 5 3 4 2
10 5 3 4 3
10 5 10 5 9

Sample Output 1

6
3
6
0
9

We will describe the first test case.

For each n = 1, 2, \ldots, 10, the two's levels after doing n learning tasks are as follows.

  • X's level: 5, 5, 6, 6, 6, 7, 7, 7, 8, 8.
  • Y's level: 4, 5, 5, 6, 6, 7, 7, 8, 8, 9.

There are six scenarios (n = 2, 4, 5, 6, 7, 9) where the two's levels are equal, so the answer is 6.

F - Insert Addition

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

配点 : 1000

問題文

整数列 P = (P_1, \ldots, P_M) に対して、各 1\leq i\leq M-1 に対して P_iP_{i+1} の間に P_i + P_{i+1} を挿入することで得られる数列を f(P) と書くことにします。より形式的には次の通りです:

  • 1\leq i\leq M - 1 に対して Q_i = P_i + P_{i+1} とする。
  • 2M-1 項からなる数列 f(P)f(P) = (P_1, Q_1, P_2, Q_2, \ldots, P_{M-1}, Q_{M-1}, P_M) により定める。

正の整数 a, b, N(ただし a, b\leq N)が与えられます。数列 A = (a, b) から始めて、以下の手順によって数列 B = (B_1, B_2, \ldots) を定めます。

  • Af(A) に取り換えることを、N 回繰り返す。
  • その後、数列 A から N より大きな値を取り除いてできる数列を、数列 B とする。

B_L, \ldots, B_R を求めてください。

制約

  • 1\leq N\leq 3\times 10^5
  • 1\leq a, b\leq N
  • 1\leq L\leq R\leq 10^{18}
  • R - L < 3\times 10^5
  • R は数列 B の項数以下である

入力

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

a b N 
L R

出力

B_L, \ldots, B_R を空白区切りで 1 行で出力してください。


入力例 1

1 1 4
1 7

出力例 1

1 4 3 2 3 4 1

はじめ A = (1, 1) です。Af(A) を取り換える操作により、数列 A は以下のように変化します:

  • 1 回目の操作:A = (1, 2, 1)
  • 2 回目の操作:A = (1, 3, 2, 3, 1)
  • 3 回目の操作:A = (1, 4, 3, 5, 2, 5, 3, 4, 1)
  • 4 回目の操作:A = (1, 5, 4, 7, 3, 8, 5, 7, 2, 7, 5, 8, 3, 7, 4, 5, 1)

したがって B = (1, 4, 3, 2, 3, 4, 1) となります。この数列の第 1 項目から第 7 項目までが答えとなります。


入力例 2

1 1 4
2 5

出力例 2

4 3 2 3

この入力例でも、B = (1, 4, 3, 2, 3, 4, 1) となります。この数列の第 2 項目から第 5 項目までが答えとなります。


入力例 3

2 1 10
5 15

出力例 3

8 3 10 7 4 9 5 6 7 8 9

入力例 4

10 10 10
2 2

出力例 4

10

Score : 1000 points

Problem Statement

For a sequence of integers P = (P_1, \ldots, P_M), let f(P) denote the sequence obtained by inserting P_i + P_{i+1} between P_i and P_{i+1} for each 1\leq i\leq M-1. More formally:

  • Let Q_i = P_i + P_{i+1} for each 1\leq i\leq M - 1.
  • Let f(P) be defined as a sequence of 2M-1 integers f(P) = (P_1, Q_1, P_2, Q_2, \ldots, P_{M-1}, Q_{M-1}, P_M).

Given are three positive integers a, b, and N where a, b\leq N. Let us begin with a sequence A = (a, b) and define a sequence B = (B_1, B_2, \ldots) as follows.

  • Do the following N times: replace A with f(A).
  • Then, remove from A all values greater than N and let B be the resulting sequence.

Find B_L, \ldots, B_R.

Constraints

  • 1\leq N\leq 3\times 10^5
  • 1\leq a, b\leq N
  • 1\leq L\leq R\leq 10^{18}
  • R - L < 3\times 10^5
  • R is at most the number of elements in the sequence B.

Input

Input is given from Standard Input in the following format:

a b N 
L R

Output

Print a line containing B_L, \ldots, B_R, with a space in between.


Sample Input 1

1 1 4
1 7

Sample Output 1

1 4 3 2 3 4 1

Initially, A = (1, 1). The replacements of A with f(A) take place as follows.

  • After the first replacement: A = (1, 2, 1).
  • After the second replacement: A = (1, 3, 2, 3, 1).
  • After the third replacement: A = (1, 4, 3, 5, 2, 5, 3, 4, 1).
  • After the fourth replacement: A = (1, 5, 4, 7, 3, 8, 5, 7, 2, 7, 5, 8, 3, 7, 4, 5, 1).

Thus, we have B = (1, 4, 3, 2, 3, 4, 1). We should report the 1-st through 7-th elements of this sequence.


Sample Input 2

1 1 4
2 5

Sample Output 2

4 3 2 3

Again, we have B = (1, 4, 3, 2, 3, 4, 1). We should report the 2-nd through 5-th elements of this sequence.


Sample Input 3

2 1 10
5 15

Sample Output 3

8 3 10 7 4 9 5 6 7 8 9

Sample Input 4

10 10 10
2 2

Sample Output 4

10