A - Repdigit

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

配点 : 100

問題文

3 桁の正整数 N が与えられます。 N を十進法で表したとき、すべての桁の数字が同じであるかどうかを判定してください。

制約

  • 100 \leq N \leq 999
  • 入力される値は整数

入力

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

N  

出力

N を十進法で表したとき、すべての桁の数字が同じであるならば Yes を、同じでないならば No1 行で出力せよ。


入力例 1

444

出力例 1

Yes

444 の各桁の数字は 4,4,4 で同じなので Yes を出力してください。


入力例 2

160

出力例 2

No

160 の各桁の数字は 1,6,0 で同じではないので、No を出力してください。


入力例 3

999

出力例 3

Yes

Score : 100 points

Problem Statement

You are given a 3-digit positive integer N. Determine whether all digits are the same when N is represented in decimal.

Constraints

  • 100 \leq N \leq 999
  • The input value is an integer.

Input

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

N  

Output

If all digits are the same when N is represented in decimal, output Yes in one line; otherwise, output No.


Sample Input 1

444

Sample Output 1

Yes

The digits of 444 are 4,4,4, which are the same, so output Yes.


Sample Input 2

160

Sample Output 2

No

The digits of 160 are 1,6,0, which are not the same, so output No.


Sample Input 3

999

Sample Output 3

Yes
B - Digit Sum

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

配点 : 200

問題文

正整数 n桁和を、 n を十進法で表したときの各桁の和と定めます。例えば 2026 の桁和は 2+0+2+6=10 です。

N 以下の正整数のうち、桁和が K であるものの個数を求めてください。

制約

  • 1\leq N,K \leq 10^5
  • 入力は全て整数

入力

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

N K

出力

答えを出力せよ。


入力例 1

30 4

出力例 1

3

30 以下の正整数のうち、桁和が 4 であるものは、4,13,223 個です。


入力例 2

2026 10

出力例 2

121

入力例 3

99999 45

出力例 3

1

Score : 200 points

Problem Statement

The digit sum of a positive integer n is defined as the sum of the digits when n is represented in decimal. For example, the digit sum of 2026 is 2+0+2+6=10.

Find the number of positive integers not exceeding N whose digit sum is K.

Constraints

  • 1\leq N,K \leq 10^5
  • All input values are integers.

Input

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

N K

Output

Output the answer.


Sample Input 1

30 4

Sample Output 1

3

Among the positive integers not exceeding 30, there are three integers whose digit sum is 4: 4,13,22.


Sample Input 2

2026 10

Sample Output 2

121

Sample Input 3

99999 45

Sample Output 3

1
C - AtCoder Riko

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

配点 : 350

問題文

長さ N の正整数列 A=(A_1,A_2,\dots,A_N) が与えられます。

以下のようなことが起こりうる正整数 L をすべて求めてください。

AtCoder 社は棒状のスナック菓子「AtCoderりこ」を発売しました。 カップの中に長さ L の AtCoderりこが何本か入っています。 高橋君がこのカップをシェイクしたところ、それぞれの AtCoderりこは以下のいずれかの状態になりました。

  • 長さが L である 1 本の AtCoderりことしてそのまま残った。
  • 長さの和が L であるような 2 本の AtCoderりこに分かれた。ただし、各 AtCoderりこの長さは正整数である。
カップをシェイクした後、カップの中には N 本の AtCoderりこが入っており、i 本目の AtCoderりこの長さは A_i でした。

ただし、このようなことが起こりうる正整数 L が少なくとも 1 つ存在するような入力が与えられます。

制約

  • 1 \leq N \leq 3 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 条件を満たすような L が少なくとも 1 つ存在する
  • 入力される値は全て整数

入力

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

N  
A_1 A_2 \ldots A_N  

出力

条件を満たすような L を、空白区切りで昇順に 1 行で出力せよ。


入力例 1

4
10 5 5 10

出力例 1

10 15

最初、カップには長さ 10 の AtCoderりこが 3 本入っていて、そのうち 1 本が 長さ 52 本の AtCoderりこに分かれると、条件を満たします。
最初、カップには長さ 15 の AtCoderりこが 2 本入っていて、それぞれの AtCoderりこが長さ 5,102 本の AtCoderりこに分かれると、条件を満たします。
これ以外の L では条件を満たしません。


入力例 2

3
4 4 4

出力例 2

4

入力例 3

6
10 187 344 100 434 257

出力例 3

444

Score : 350 points

Problem Statement

You are given a sequence of N positive integers A=(A_1,A_2,\dots,A_N).

Find all positive integers L for which the following can occur:

AtCoder Inc. has released a stick-shaped snack called "AtCoderiko." A cup contains one or more AtCoderikos, each of length L. When Takahashi shook the cup, each AtCoderiko ended up in one of the following states:

  • It remained as one AtCoderiko of length L.
  • It broke into two AtCoderikos whose lengths sum to L. Here, the length of each AtCoderiko is a positive integer.
After shaking the cup, there were N AtCoderikos in the cup, and the length of the i-th AtCoderiko was A_i.

The given input guarantees that there exists at least one positive integer L for which this can occur.

Constraints

  • 1 \leq N \leq 3 \times 10^5
  • 1 \leq A_i \leq 10^9
  • There exists at least one L satisfying the condition.
  • All input values are integers.

Input

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

N  
A_1 A_2 \ldots A_N  

Output

Output all values of L satisfying the condition in ascending order, separated by spaces, in one line.


Sample Input 1

4
10 5 5 10

Sample Output 1

10 15

If the cup initially contained three AtCoderikos of length 10, and one of them broke into two AtCoderikos of length 5, the condition is satisfied.
If the cup initially contained two AtCoderikos of length 15, and each of them broke into two AtCoderikos of lengths 5 and 10, the condition is satisfied.
No other values of L satisfy the condition.


Sample Input 2

3
4 4 4

Sample Output 2

4

Sample Input 3

6
10 187 344 100 434 257

Sample Output 3

444
D - Many Repunit Sum

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

配点 : 400

問題文

i=1,2,\dots,N に対して、1A_i 個つなげた整数を B_i と表します。
より厳密には、B_i=\sum_{j=0}^{A_i-1}{10^j} と表します。
\sum_{i=1}^{N}{B_i} を求めてください。

制約

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

入力

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

N  
A_1 A_2 \ldots A_N  

出力

答えを 1 行で出力せよ。


入力例 1

4
3 3 3 3

出力例 1

444

B_1=B_2=B_3=B_4=111 なので、B_1+B_2+B_3+B_4=444 です。


入力例 2

3
30 10 20

出力例 2

111111111122222222223333333333

答えは非常に大きくなる可能性があります。


入力例 3

10
1 2 3 4 5 6 7 8 9 10

出力例 3

1234567900

Score : 400 points

Problem Statement

For i=1,2,\dots,N, let B_i denote the integer formed by concatenating A_i ones.
More formally, B_i=\sum_{j=0}^{A_i-1}{10^j}.
Find \sum_{i=1}^{N}{B_i}.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 2 \times 10^5
  • All input values are integers.

Input

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

N  
A_1 A_2 \ldots A_N  

Output

Output the answer in one line.


Sample Input 1

4
3 3 3 3

Sample Output 1

444

B_1=B_2=B_3=B_4=111, so B_1+B_2+B_3+B_4=444.


Sample Input 2

3
30 10 20

Sample Output 2

111111111122222222223333333333

The answer may be very large.


Sample Input 3

10
1 2 3 4 5 6 7 8 9 10

Sample Output 3

1234567900
E - Sparse Range

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

配点 : 450

問題文

長さ N の整数列 (A_1,\dots,A_N) と正整数 D が与えられます。

以下の条件をともに満たす整数の組 (L,R) の個数を求めてください。

  • 1 \leq L \leq R \leq N
  • (A_L,A_{L+1},\dots,A_R) のどの 2 つの要素も差が D 以上である
    • すなわち、 L \leq i < j \leq R を満たす全ての整数の組 (i,j) について、 |A_i-A_j|\geq D である

制約

  • 2\leq N \leq 4\times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq D \leq 10^9

入力

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

N D
A_1 \dots A_N

出力

答えを出力せよ。


入力例 1

5 3
3 1 4 1 5

出力例 1

8

(1,1),(2,2),(3,3),(4,4),(5,5),(2,3),(3,4),(4,5)8 組が条件を満たします。


入力例 2

9 1
1 2 3 4 5 6 7 8 9

出力例 2

45

入力例 3

6 1000000000
123456789 234567891 987654321 321987654 1000000000 1

出力例 3

6

Score : 450 points

Problem Statement

You are given an integer sequence of length N, (A_1,\dots,A_N), and a positive integer D.

Find the number of pairs of integers (L,R) that satisfy both of the following conditions:

  • 1 \leq L \leq R \leq N
  • Any two elements of (A_L,A_{L+1},\dots,A_R) have a difference of at least D.
    • That is, |A_i-A_j|\geq D for all pairs of integers (i,j) satisfying L \leq i < j \leq R.

Constraints

  • 2\leq N \leq 4\times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq D \leq 10^9

Input

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

N D
A_1 \dots A_N

Output

Output the answer.


Sample Input 1

5 3
3 1 4 1 5

Sample Output 1

8

The eight pairs (1,1),(2,2),(3,3),(4,4),(5,5),(2,3),(3,4),(4,5) satisfy the conditions.


Sample Input 2

9 1
1 2 3 4 5 6 7 8 9

Sample Output 2

45

Sample Input 3

6 1000000000
123456789 234567891 987654321 321987654 1000000000 1

Sample Output 3

6
F - Half and Median

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

配点 : 550

問題文

N 本の棒があり、i 本目の棒の長さは A_i です。
以下の操作を M 回行います。

  • 長さが 2 以上の棒を 1 本選び、この棒の長さを l と置く。この棒を、長さが \left\lfloor\frac{l}{2}\right\rfloor\left\lceil\frac{l}{2}\right\rceil2 本の棒に分割する。

制約から、操作を M 回行うことは可能であることが証明できます。
M 回の操作後、N+M 本の棒が残りますが、これらの棒の長さの中央値としてありうる値の最大値を求めてください。

1 つの入力につき、T 個のテストケースを解いてください。

中央値とは N が偶数であるとき、N+1 個の数の中央値とは、それらを昇順に並べたときに (1+\frac{N}{2}) 番目の値です。
例えば、(1,3,4,5,8) の中央値は 4(2,2,2) の中央値は 2 です。
本問題において、長さの中央値を考える棒の本数は制約から常に奇数であることに注意してください。

制約

  • 1 \leq T \leq 10^5
  • 1 \leq N \leq 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq M \leq \sum_{i=1}^{N}{A_i} - N
  • N+M は奇数
  • 全てのテストケースにおける N の総和は 10^5 以下
  • 入力される値は全て整数

入力

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

T
case_1
case_2
\vdots
case_T

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

N M  
A_1 A_2 \ldots A_N

出力

答えを合計 T 行で出力せよ。 t 行目には、t 番目のテストケースの答えを出力せよ。


入力例 1

3
5 2
14 2 5 19 1
10 1
10 6 7 6 2 20 9 16 3 3
4 1
444 444 44 444

出力例 1

7
7
444

1 つ目のテストケースでは、1 回目の操作で長さ 14 の棒を選ぶとその棒は長さ 7 の棒 2 本に分割され、6 本の棒の長さは 7,7,2,5,19,1 になります。 2 回目の操作で長さ 19 の棒を選ぶとその棒は長さ 9,10 の棒 2 本に分割され、7 本の棒の長さは 7,7,2,5,9,10,1 になります。 このとき、棒の長さの中央値は 7 になります。

Score : 550 points

Problem Statement

There are N sticks, and the length of the i-th stick is A_i.
You will perform the following operation M times:

  • Choose one stick of length at least 2, and let l be the length of this stick. Divide this stick into two sticks of lengths \left\lfloor\frac{l}{2}\right\rfloor and \left\lceil\frac{l}{2}\right\rceil.

From the constraints, it can be proved that it is possible to perform the operation M times.
After M operations, N+M sticks will remain. Find the maximum possible value of the median of the lengths of these sticks.

Solve T test cases per input.

What is the median? When N is even, the median of N+1 numbers is the (1+\frac{N}{2})-th value when they are sorted in ascending order.
For example, the median of (1,3,4,5,8) is 4, and the median of (2,2,2) is 2.
Note that in this problem, from the constraints, the number of sticks to consider for the median of lengths is always odd.

Constraints

  • 1 \leq T \leq 10^5
  • 1 \leq N \leq 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq M \leq \sum_{i=1}^{N}{A_i} - N
  • N+M is odd.
  • The sum of N over all test cases is at most 10^5.
  • All input values are integers.

Input

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

T
case_1
case_2
\vdots
case_T

Each case is given in the following format:

N M  
A_1 A_2 \ldots A_N

Output

Output the answers in T lines in total. The t-th line should contain the answer for the t-th test case.


Sample Input 1

3
5 2
14 2 5 19 1
10 1
10 6 7 6 2 20 9 16 3 3
4 1
444 444 44 444

Sample Output 1

7
7
444

In the first test case, if you choose the stick of length 14 in the first operation, it is divided into two sticks of length 7, and now you have six sticks of lengths 7,7,2,5,19,1. If you choose the stick of length 19 in the second operation, it is divided into two sticks of lengths 9,10, and now you have seven sticks of lengths 7,7,2,5,9,10,1. In this case, the median of the stick lengths is 7.

G - Kyoen

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

配点 : 675

問題文

2 次元座標平面に、 点 (\frac{A}{C}, \frac{B}{C}) を中心とする半径 \frac{\sqrt{N}}{C} の円があります。 この円の円周上にある格子点の個数を 998244353 で割ったあまりを求めてください。

なお、 NN=\prod_{i=1}^{M} P_i^{E_i} と素因数分解された形で与えられます。

制約

  • N \geq 1
  • 2 \leq P_i \leq 100
  • P_i は相異なる素数
  • 1 \leq E_i \leq 10^{18}
  • 1 \leq C \leq 50
  • 0 \leq A,B \lt C
  • 入力は全て整数

入力

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

A B C M
P_1 E_1
P_2 E_2
\vdots
P_M E_M

出力

答えを出力せよ。


入力例 1

1 1 2 2
2 1
5 2

出力例 1

12

下図に示す通り、円周上に 12 個の格子点があります。

図


入力例 2

1 5 14 4
2 1
5 1
13 1
17 1

出力例 2

4

下図に示すとおり、円周上に 4 個の格子点があります。

図


入力例 3

1 1 6 3
2 1
5 2
13 1

出力例 3

6

下図に示すとおり、円周上に 6 個の格子点があります。

図


入力例 4

0 0 1 10
97 1000000000000000000
89 1000000000000000000
73 1000000000000000000
61 1000000000000000000
59 1000000000000000000
47 1000000000000000000
31 1000000000000000000
23 1000000000000000000
11 1000000000000000000
5 1000000000000000000

出力例 4

7885876

998244353 で割った余りを求めてください。

Score : 675 points

Problem Statement

On a two-dimensional coordinate plane, there is a circle with center at point (\frac{A}{C}, \frac{B}{C}) and radius \frac{\sqrt{N}}{C}. Find the number, modulo 998244353, of lattice points on the circumference of this circle.

N is given in its prime factorization form as N=\prod_{i=1}^{M} P_i^{E_i}.

Constraints

  • N \geq 1
  • 2 \leq P_i \leq 100
  • P_i are distinct primes.
  • 1 \leq E_i \leq 10^{18}
  • 1 \leq C \leq 50
  • 0 \leq A,B \lt C
  • All input values are integers.

Input

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

A B C M
P_1 E_1
P_2 E_2
\vdots
P_M E_M

Output

Output the answer.


Sample Input 1

1 1 2 2
2 1
5 2

Sample Output 1

12

As shown in the figure below, there are 12 lattice points on the circumference.

Figure


Sample Input 2

1 5 14 4
2 1
5 1
13 1
17 1

Sample Output 2

4

As shown in the figure below, there are 4 lattice points on the circumference.

Figure


Sample Input 3

1 1 6 3
2 1
5 2
13 1

Sample Output 3

6

As shown in the figure below, there are 6 lattice points on the circumference.

Figure


Sample Input 4

0 0 1 10
97 1000000000000000000
89 1000000000000000000
73 1000000000000000000
61 1000000000000000000
59 1000000000000000000
47 1000000000000000000
31 1000000000000000000
23 1000000000000000000
11 1000000000000000000
5 1000000000000000000

Sample Output 4

7885876

Find the number modulo 998244353.