A - Long Loong

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

正の整数 X について、レベル X龍文字列 とは、1 個の L, X 個の o, 1 個の n, 1 個の g をこの順に並べた長さ (X+3) の文字列です。

正の整数 N が与えられるので、レベル N の龍文字列を出力してください。
大文字と小文字は区別されることに注意してください。

制約

  • 1\leq N\leq 2024
  • N は整数

入力

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

N

出力

レベル N の龍文字列を出力せよ。


入力例 1

3

出力例 1

Looong

1 個の L, 3 個の o, 1 個の n, 1 個の g をこの順に並べた文字列は Looong です。


入力例 2

1

出力例 2

Long

Score: 100 points

Problem Statement

For a positive integer X, the Dragon String of level X is a string of length (X+3) formed by one L, X occurrences of o, one n, and one g arranged in this order.

You are given a positive integer N. Print the Dragon String of level N.
Note that uppercase and lowercase letters are distinguished.

Constraints

  • 1 \leq N \leq 2024
  • N is an integer.

Input

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

N

Output

Print the Dragon String of level N.


Sample Input 1

3

Sample Output 1

Looong

Arranging one L, three os, one n, and one g in this order yields Looong.


Sample Input 2

1

Sample Output 2

Long
B - CTZ

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

正の整数 X に対して、X2 進表記したときに 末尾 に連続する 0 の個数(の最大値)を \text{ctz}(X) で表します。
ただし、X2 進表記したとき末尾が 1 ならば \text{ctz}(X)=0 です。

正の整数 N が与えられるので、\text{ctz}(N) を出力してください。

制約

  • 1\leq N\leq 10^9
  • N は整数

入力

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

N

出力

\text{ctz}(N) を出力せよ。


入力例 1

2024

出力例 1

3

20242 進表記すると 11111101000 であり、末尾から 03 個連続しているため、\text{ctz}(2024)=3 です。
よって、3 を出力します。


入力例 2

18

出力例 2

1

182 進表記すると 10010 であるため、\text{ctz}(18)=1 です。
0 は末尾から連続する必要があることに注意してください。


入力例 3

5

出力例 3

0

Score: 200 points

Problem Statement

For a positive integer X, let \text{ctz}(X) be the (maximal) number of consecutive zeros at the end of the binary notation of X.
If the binary notation of X ends with a 1, then \text{ctz}(X)=0.

You are given a positive integer N. Print \text{ctz}(N).

Constraints

  • 1\leq N\leq 10^9
  • N is an integer.

Input

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

N

Output

Print \text{ctz}(N).


Sample Input 1

2024

Sample Output 1

3

2024 is 11111101000 in binary, with three consecutive 0s from the end, so \text{ctz}(2024)=3.
Thus, print 3.


Sample Input 2

18

Sample Output 2

1

18 is 10010 in binary, so \text{ctz}(18)=1.
Note that we count the trailing zeros.


Sample Input 3

5

Sample Output 3

0
C - Even Digits

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

非負整数 n が次の条件を満たすとき、n良い整数 と呼びます。

  • n10 進法で表したときに、偶数の数字 (0, 2, 4, 6, 8) のみが登場する。

例えば 068 および 2024 は良い整数です。

整数 N が与えられます。良い整数のうち小さい方から N 番目の整数を求めてください。

制約

  • 1 \leq N \leq 10^{12}
  • N は整数

入力

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

N

出力

小さい方から N 番目の良い整数を出力せよ。


入力例 1

8

出力例 1

24

良い整数を小さい方から順に並べると 0, 2, 4, 6, 8, 20, 22, 24, 26, 28, \dots となります。
小さい方から 8 番目の良い整数は 24 なので、これを出力します。


入力例 2

133

出力例 2

2024

入力例 3

31415926535

出力例 3

2006628868244228

Score: 300 points

Problem Statement

A non-negative integer n is called a good integer when it satisfies the following condition:

  • All digits in the decimal notation of n are even numbers (0, 2, 4, 6, and 8).

For example, 0, 68, and 2024 are good integers.

You are given an integer N. Find the N-th smallest good integer.

Constraints

  • 1 \leq N \leq 10^{12}
  • N is an integer.

Input

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

N

Output

Print the N-th smallest good integer.


Sample Input 1

8

Sample Output 1

24

The good integers in ascending order are 0, 2, 4, 6, 8, 20, 22, 24, 26, 28, \dots.
The eighth smallest is 24, which should be printed.


Sample Input 2

133

Sample Output 2

2024

Sample Input 3

31415926535

Sample Output 3

2006628868244228
D - Pyramid

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

正の整数 k について、サイズ kピラミッド数列 とは、長さ (2k-1) の数列であって各項の値が順に 1,2,\ldots,k-1,k,k-1,\ldots,2,1 であるようなものをさします。

長さ N の数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
A に対して、次の操作のうち一方を選んで行うことを繰り返して (0 回でも良い) 得ることのできるピラミッド数列のサイズの最大値を求めてください。

  • 数列の項を 1 つ選び、その項の値を 1 減少させる。
  • 先頭または末尾の項を削除する。

なお、問題の制約のもとで、操作を繰り返すことで必ず 1 種類以上のピラミッド数列を得ることができることが証明できます。

制約

  • 1\leq N\leq 2\times 10^5
  • 1\leq A_i\leq 10^9
  • 入力はすべて整数

入力

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

N
A_1 A_2 \ldots A_N

出力

数列 A に問題文の操作を繰り返して得ることのできるピラミッド数列のサイズの最大値を出力せよ。


入力例 1

5
2 2 3 1 1

出力例 1

2

A=(2,2,3,1,1) から始めて、 次のようにして数列 A からサイズ 2 のピラミッド数列を作る事ができます。

  • 3 項を選び、1 減少させる。数列は A=(2,2,2,1,1) となる。
  • 先頭を削除する。数列は A=(2,2,1,1) となる。
  • 末尾を削除する。数列は A=(2,2,1) となる。
  • 1 項を選び、1 減少させる。数列は A=(1,2,1) となる。

(1,2,1) はサイズ 2 のピラミッド数列です。
一方、どのように操作を行ってもサイズ 3 以上のピラミッド数列を作ることはできないため 2 を出力します。


入力例 2

5
1 2 3 4 5

出力例 2

3

入力例 3

1
1000000000

出力例 3

1

Score: 400 points

Problem Statement

For a positive integer k, the Pyramid Sequence of size k is a sequence of length (2k-1) where the terms of the sequence have the values 1,2,\ldots,k-1,k,k-1,\ldots,2,1 in this order.

You are given a sequence A=(A_1,A_2,\ldots,A_N) of length N.
Find the maximum size of a Pyramid Sequence that can be obtained by repeatedly choosing and performing one of the following operations on A (possibly zero times).

  • Choose one term of the sequence and decrease its value by 1.
  • Remove the first or last term.

It can be proved that the constraints of the problem guarantee that at least one Pyramid Sequence can be obtained by repeating the operations.

Constraints

  • 1\leq N\leq 2\times 10^5
  • 1\leq A_i\leq 10^9
  • 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

Print the maximum size of the Pyramid Sequence that can be obtained by repeatedly performing the operations described in the problem statement on the sequence A.


Sample Input 1

5
2 2 3 1 1

Sample Output 1

2

Starting with A=(2,2,3,1,1), you can create a Pyramid Sequence of size 2 as follows:

  • Choose the third term and decrease it by 1. The sequence becomes A=(2,2,2,1,1).
  • Remove the first term. The sequence becomes A=(2,2,1,1).
  • Remove the last term. The sequence becomes A=(2,2,1).
  • Choose the first term and decrease it by 1. The sequence becomes A=(1,2,1).

(1,2,1) is a Pyramid Sequence of size 2.
On the other hand, there is no way to perform the operations to create a Pyramid Sequence of size 3 or larger, so you should print 2.


Sample Input 2

5
1 2 3 4 5

Sample Output 2

3

Sample Input 3

1
1000000000

Sample Output 3

1
E - Digit Sum Divisible

Time Limit: 10 sec / Memory Limit: 1024 MB

配点 : 525

問題文

正整数 n桁和 を、n10 進法で表したときの各桁の和として定義します。例えば 2024 の桁和は 2+0+2+4=8 です。
正整数 nn の桁和で割り切れる時、n良い整数 と呼びます。例えば 2024 はその桁和である 8 で割り切れるので良い整数です。
正整数 N が与えられます。N 以下の良い整数は全部で何個ありますか?

制約

  • 1 \leq N \leq 10^{14}
  • N は整数

入力

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

N

出力

N 以下の良い整数の個数を出力せよ。


入力例 1

20

出力例 1

13

20 以下の良い整数は 1,2,3,4,5,6,7,8,9,10,12,18,2013 個です。


入力例 2

2024

出力例 2

409

入力例 3

9876543210

出力例 3

547452239

Score: 525 points

Problem Statement

The digit sum of a positive integer n is defined as the sum of the digits in the decimal notation of n. For example, the digit sum of 2024 is 2+0+2+4=8.
A positive integer n is called a good integer when n is divisible by its digit sum. For example, 2024 is a good integer because it is divisible by its digit sum of 8.
You are given a positive integer N. How many good integers are less than or equal to N?

Constraints

  • 1 \leq N \leq 10^{14}
  • N is an integer.

Input

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

N

Output

Print the number of good integers less than or equal to N.


Sample Input 1

20

Sample Output 1

13

There are 13 good integers less than or equal to 20: 1,2,3,4,5,6,7,8,9,10,12,18,20.


Sample Input 2

2024

Sample Output 2

409

Sample Input 3

9876543210

Sample Output 3

547452239
F - Rotation Puzzle

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 550

問題文

HW 列のマス目があり、最初、1 以上 (H\times W) 以下の整数がちょうど 1 つずつ書き込まれています。
具体的には、1\leq i\leq H, 1\leq j\leq W について、上から i 行目かつ左から j 列目のマスには S_{i,j} が書き込まれています。
以下、上から i 行目かつ左から j 列目のマスをマス (i,j) と呼びます。

次の操作を 高々 200 回でも良い)繰り返すことで、
任意の整数の組 (i,j) (1\leq i\leq H, 1\leq j\leq W) について、 マス (i,j)((i-1)\times W+j) が書き込まれている状態にできるか判定し、
できる場合は必要な操作回数の最小値を出力してください。
20 回以下でできない場合(何回操作を繰り返してもできない場合を含む)は -1 を出力してください。

操作:マス目から (H-1) \times (W-1) の長方形を選んで 180 度回転させる。
   より厳密には、整数 x,y (0 \leq x, y \leq 1) を選び、
   1 \leq i \leq H-1, 1 \leq j \leq W-1 をみたすすべての整数の組 (i,j) について同時に、
   マス (i+x,j+y) に書かれた整数をマス (H-i+x,W-j+y) に書かれた数に書き換える。

マス目に書き込まれている整数が条件をみたしていれば良く、書き込まれている向き等は考える必要がないことに注意してください。

制約

  • 3\leq H,W\leq 8
  • 1\leq S_{i,j}\leq H\times W
  • (i,j)\neq (i',j') ならば S_{i,j}\neq S_{i',j'}
  • 入力はすべて整数

入力

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

H W
S_{1,1} S_{1,2} \ldots S_{1,W}
S_{2,1} S_{2,2} \ldots S_{2,W}
\vdots
S_{H,1} S_{H,2} \ldots S_{H,W}

出力

問題文の条件をみたすために必要な操作回数の最小値を出力せよ。
ただし、20 回以下の操作で条件をみたすようにできないときは -1 を出力せよ。


入力例 1

3 3
9 4 3
2 1 8
7 6 5

出力例 1

2

次の順で操作を行うことで 2 回の操作で問題文の条件をみたすようにすることができます。

  • 左上の長方形を選び、操作を行う。すなわち x=0, y=0 を選んで操作を行う。
  • 右下の長方形を選び、操作を行う。すなわち x=1, y=1 を選んで操作を行う。

一方で 1 回以下の操作でみたすようにすることは不可能であるため、2 を出力します。


入力例 2

4 6
15 18 1 14 3 4
23 24 19 8 9 12
13 2 17 6 5 16
21 22 7 20 11 10

出力例 2

-1

20 回以下の操作で条件をみたすようにすることができないため、-1 を出力します。


入力例 3

4 6
1 4 13 16 15 18
21 20 9 12 23 10
17 14 5 6 3 2
11 22 7 24 19 8

出力例 3

20

入力例 4

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

出力例 4

0

Score: 550 points

Problem Statement

There is a grid with H rows and W columns. Initially, each integer from 1 to (H \times W) is written exactly once in the grid.
Specifically, for 1 \leq i \leq H and 1 \leq j \leq W, the cell at the i-th row from the top and the j-th column from the left contains S_{i,j}.
Below, let (i,j) denote the cell at the i-th row from the top and the j-th column from the left.

Determine if it is possible to reach a state where, for all pairs of integers (i,j) (1 \leq i \leq H, 1 \leq j \leq W),
the cell (i,j) contains the integer ((i-1) \times W + j) by repeating the following operation at most 20 times (possibly zero).
If it is possible, print the minimum number of operations required.
If it is impossible in 20 or fewer operations (including the case where it is impossible no matter how many times the operation is repeated), print -1.

Operation: Choose a rectangle of size (H-1) \times (W-1) from the grid and rotate it 180 degrees.
More precisely, choose integers x and y (0 \leq x, y \leq 1),
and for all pairs of integers (i,j) satisfying 1 \leq i \leq H-1 and 1 \leq j \leq W-1,
simultaneously replace the integer written in cell (i+x,j+y) with the number written in cell (H-i+x,W-j+y).

Note that it is only necessary for the integers written in the cells to satisfy the condition; the orientation in which they are written does not matter.

Constraints

  • 3 \leq H,W \leq 8
  • 1 \leq S_{i,j} \leq H \times W
  • If (i,j) \neq (i',j'), then S_{i,j} \neq S_{i',j'}
  • All input values are integers.

Input

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

H W
S_{1,1} S_{1,2} \ldots S_{1,W}
S_{2,1} S_{2,2} \ldots S_{2,W}
\vdots
S_{H,1} S_{H,2} \ldots S_{H,W}

Output

Print the minimum number of operations required to meet the conditions of the problem statement.
If it is impossible to satisfy the conditions with 20 or fewer operations, output -1.


Sample Input 1

3 3
9 4 3
2 1 8
7 6 5

Sample Output 1

2

Operating in the following order will satisfy the conditions of the problem statement in 2 operations.

  • Operate by choosing the rectangle in the top-left corner. That is, by choosing x=0 and y=0.
  • Operate by choosing the rectangle in the bottom-right corner. That is, by choosing x=1 and y=1.

On the other hand, it is impossible to satisfy the conditions with one or fewer operations, so print 2.


Sample Input 2

4 6
15 18 1 14 3 4
23 24 19 8 9 12
13 2 17 6 5 16
21 22 7 20 11 10

Sample Output 2

-1

It is impossible to satisfy the conditions with 20 or fewer operations, so print -1.


Sample Input 3

4 6
1 4 13 16 15 18
21 20 9 12 23 10
17 14 5 6 3 2
11 22 7 24 19 8

Sample Output 3

20

Sample Input 4

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

Sample Output 4

0
G - 16 Integers

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 650

問題文

16 個の非負整数 X_{i, j, k, l} (i, j, k, l \in \lbrace 0, 1 \rbrace)(i, j, k, l) の昇順に与えられます。
また、N = \displaystyle \sum_{i=0}^1 \sum_{j=0}^1 \sum_{k=0}^1 \sum_{l=0}^1 X_{i,j,k,l} とします。
0 または 1 からなる長さ N + 3 の数列 (A_1, A_2, ..., A_{N+3}) のうち、次の条件を満たすものの個数を 998244353 で割った余りを求めてください。

  • 整数の 4 つ組 (i, j, k, l) (i, j, k, l \in \lbrace 0, 1 \rbrace) 全てについて、次の条件を満たす 1 以上 N 以下の整数 s はちょうど X_{i,j,k,l} 個存在する。
    • A_s = i, A_{s + 1} = j, A_{s + 2} = k, A_{s + 3} = l である。

制約

  • X_{i, j, k, l} は全て非負整数
  • 1 \leq \displaystyle \sum_{i=0}^1 \sum_{j=0}^1 \sum_{k=0}^1 \sum_{l=0}^1 X_{i,j,k,l} \leq 10^6

入力

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

X_{0,0,0,0} X_{0,0,0,1} X_{0,0,1,0} X_{0,0,1,1} X_{0,1,0,0} X_{0,1,0,1} X_{0,1,1,0} X_{0,1,1,1} X_{1,0,0,0} X_{1,0,0,1} X_{1,0,1,0} X_{1,0,1,1} X_{1,1,0,0} X_{1,1,0,1} X_{1,1,1,0} X_{1,1,1,1} 

出力

問題文の条件を満たす数列の個数を 998244353 で割った余りを出力せよ。


入力例 1

0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0

出力例 1

1

この入力は、 X_{1, 0, 1, 0}X_{1, 1, 0, 1}1 でそれ以外は 0 であるような入力です。
このとき、条件を満たす数列は (1, 1, 0, 1, 0)1 通りのみです。


入力例 2

1 1 2 0 1 2 1 1 1 1 1 2 1 0 1 0

出力例 2

16

入力例 3

21 3 3 0 3 0 0 0 4 0 0 0 0 0 0 0

出力例 3

2024

入力例 4

62 67 59 58 58 69 57 66 67 50 68 65 59 64 67 61

出力例 4

741536606

Score: 650 points

Problem Statement

You are given 16 non-negative integers X_{i, j, k, l} (i, j, k, l \in \lbrace 0, 1 \rbrace) in ascending order of (i, j, k, l).
Let N = \displaystyle \sum_{i=0}^1 \sum_{j=0}^1 \sum_{k=0}^1 \sum_{l=0}^1 X_{i,j,k,l}.
Find the number, modulo 998244353, of sequences (A_1, A_2, ..., A_{N+3}) of length N + 3 consisting of 0s and 1s that satisfy the following condition.

  • For every quadruple of integers (i, j, k, l) (i, j, k, l \in \lbrace 0, 1 \rbrace), there are exactly X_{i,j,k,l} integers s between 1 and N, inclusive, that satisfy:
    • A_s = i, A_{s + 1} = j, A_{s + 2} = k, and A_{s + 3} = l.

Constraints

  • X_{i, j, k, l} are all non-negative integers.
  • 1 \leq \displaystyle \sum_{i=0}^1 \sum_{j=0}^1 \sum_{k=0}^1 \sum_{l=0}^1 X_{i,j,k,l} \leq 10^6

Input

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

X_{0,0,0,0} X_{0,0,0,1} X_{0,0,1,0} X_{0,0,1,1} X_{0,1,0,0} X_{0,1,0,1} X_{0,1,1,0} X_{0,1,1,1} X_{1,0,0,0} X_{1,0,0,1} X_{1,0,1,0} X_{1,0,1,1} X_{1,1,0,0} X_{1,1,0,1} X_{1,1,1,0} X_{1,1,1,1} 

Output

Print the number, modulo 998244353, of sequences that satisfy the condition in the problem statement.


Sample Input 1

0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0

Sample Output 1

1

This input corresponds to the case where X_{1, 0, 1, 0} and X_{1, 1, 0, 1} are 1 and all others are 0.
In this case, only one sequence satisfies the condition, which is (1, 1, 0, 1, 0).


Sample Input 2

1 1 2 0 1 2 1 1 1 1 1 2 1 0 1 0

Sample Output 2

16

Sample Input 3

21 3 3 0 3 0 0 0 4 0 0 0 0 0 0 0

Sample Output 3

2024

Sample Input 4

62 67 59 58 58 69 57 66 67 50 68 65 59 64 67 61

Sample Output 4

741536606