E - ABC conjecture

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

配点 : 300 点

問題文

正の整数 N が与えられます。

A\leq B\leq C かつ ABC\leq N であるような正の整数の組 (A,B,C) の個数を求めてください。

なお、制約の条件下で答えは 2^{63} 未満であることが保証されます。

制約

  • 1 \leq N \leq 10^{11}
  • N は整数である

入力

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

N

出力

答えを出力せよ。


入力例 1

4

出力例 1

5

条件を満たす組は (1,1,1),(1,1,2),(1,1,3),(1,1,4),(1,2,2) の 5 つです。


入力例 2

100

出力例 2

323

入力例 3

100000000000

出力例 3

5745290566750

Score : 300 points

Problem Statement

You are given a positive integer N.

Find the number of triples of positive integers (A, B, C) such that A\leq B\leq C and ABC\leq N.

The Constraints guarantee that the answer is less than 2^{63}.

Constraints

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

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.


Sample Input 1

4

Sample Output 1

5

There are five such triples: (1,1,1),(1,1,2),(1,1,3),(1,1,4),(1,2,2).


Sample Input 2

100

Sample Output 2

323

Sample Input 3

100000000000

Sample Output 3

5745290566750
F - Sierpinski carpet

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

配点 : 250 点

問題文

非負整数 K に対して、以下のようにレベル K のカーペットを定義します。

  • レベル 0 のカーペットは黒いマス 1 個のみからなる 1\times 1 のグリッドである。
  • K>0 のとき、レベル K のカーペットは 3^K\times 3^K のグリッドである。 このグリッドを 3^{K-1}\times 3^{K-1} のブロック 9 個に分割したとき、
    • 中央のブロックはすべて白いマスからなる。
    • 他の 8 個のブロックは、レベル (K-1) のカーペットである。

非負整数 N が与えられます。
レベル N のカーペットを出力の形式に従って出力してください。

制約

  • 0\leq N \leq 6
  • N は整数

入力

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

N

出力

3^N 行出力せよ。
i 行目 (1\leq i\leq 3^N) には、. と # からなる長さ 3^N の文字列 S_i を出力せよ。
S_i の j 文字目 (1\leq j\leq 3^N) は、レベル N のカーペットの上から i 行目かつ左から j 列目のマスが黒いとき # とし、白いとき . とせよ。


入力例 1

1

出力例 1

###
#.#
###

レベル 1 のカーペットは次のような 3\times 3 のグリッドです。

これを出力形式にしたがって出力すると出力例のようになります。


入力例 2

2

出力例 2

#########
#.##.##.#
#########
###...###
#.#...#.#
###...###
#########
#.##.##.#
#########

レベル 2 のカーペットは 9\times 9 のグリッドとなります。

Score : 250 points

Problem Statement

For a non-negative integer K, we define a level-K carpet as follows:

  • A level-0 carpet is a 1 \times 1 grid consisting of a single black cell.
  • For K > 0, a level-K carpet is a 3^K \times 3^K grid. When this grid is divided into nine 3^{K-1} \times 3^{K-1} blocks:
    • The central block consists entirely of white cells.
    • The other eight blocks are level-(K-1) carpets.

You are given a non-negative integer N.
Print a level-N carpet according to the specified format.

Constraints

  • 0 \leq N \leq 6
  • N is an integer.

Input

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

N

Output

Print 3^N lines.
The i-th line (1 \leq i \leq 3^N) should contain a string S_i of length 3^N consisting of . and #.
The j-th character of S_i (1 \leq j \leq 3^N) should be # if the cell at the i-th row from the top and j-th column from the left of a level-N carpet is black, and . if it is white.


Sample Input 1

1

Sample Output 1

###
#.#
###

A level-1 carpet is a 3 \times 3 grid as follows:

When output according to the specified format, it looks like the sample output.


Sample Input 2

2

Sample Output 2

#########
#.##.##.#
#########
###...###
#.#...#.#
###...###
#########
#.##.##.#
#########

A level-2 carpet is a 9 \times 9 grid.

G - Draw Your Cards

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

配点 : 400 点

問題文

1 から N が書かれた N 枚のカードが裏向きで積まれた山札があり、上から i 枚目のカードには整数 P_i が書かれています。

この山札を使って、以下の行動を N ターン繰り返します。

  • 山札の一番上のカードを引いて、そこに書かれた整数を X とする。
  • 場に見えている表向きのカードであって書かれた整数が X 以上であるもののうち、書かれた整数が最小のものの上に、引いたカードを表向きで重ねる。もし場にそのようなカードがなければ、引いたカードをどれにも重ねずに表向きで場に置く。
  • その後、表向きのカードが K 枚重ねられた山が場にあればその山のカードを全て食べる。食べられたカードは全て場から消滅する。

各カードについて、何ターン目に食べられるか、あるいは最後まで食べられないかを求めてください。

制約

  • 入力は全て整数
  • 1 \le K \le N \le 2 \times 10^5
  • P は (1,2,\dots,N) の順列 ( (1,2,\dots,N) を並べ替えて得られる列 ) である

入力

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

N K
P_1 P_2 \dots P_N

出力

N 行出力せよ。
そのうち i (1 \le i \le N) 行目には、整数 i が書かれたカードについて、以下のように出力せよ。

  • もし i が書かれたカードが食べられるなら、それが何ターン目であるかを整数として出力せよ。
  • そうでないなら -1 と出力せよ。

入力例 1

5 2
3 5 2 1 4

出力例 1

4
3
3
-1
4

この入力では、 P=(3,5,2,1,4),K=2 です。

  • 1 ターン目に、 3 が書かれたカードが他のカードに重ねられずに表向きで場に置かれます。
  • 2 ターン目に、 5 が書かれたカードが他のカードに重ねられずに表向きで場に置かれます。
  • 3 ターン目に、 2 が書かれたカードが 3 が書かれたカードの上に表向きで重ねられます。
    • この時点で上から 2,3 が書かれたカードが表向きで重ねられた山が K=2 枚に達したので、両カードは食べられます。
  • 4 ターン目に、 1 が書かれたカードが 5 が書かれたカードの上に表向きで重ねられます。
    • この時点で上から 1,5 が書かれたカードが表向きで重ねられた山が K=2 枚に達したので、両カードは食べられます。
  • 5 ターン目に、 4 が書かれたカードが他のカードに重ねられずに表向きで場に置かれます。
  • 4 が書かれたカードは、最後まで食べられませんでした。

入力例 2

5 1
1 2 3 4 5

出力例 2

1
2
3
4
5

K=1 である場合、全てのカードは場に置かれたターンに食べられます。


入力例 3

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

出力例 3

9
9
9
15
15
6
-1
-1
6
-1
-1
-1
-1
6
15

Score : 400 points

Problem Statement

There is a deck consisting of N face-down cards with an integer from 1 through N written on them. The integer on the i-th card from the top is P_i.

Using this deck, you will perform N moves, each consisting of the following steps:

  • Draw the topmost card from the deck. Let X be the integer written on it.
  • Stack the drawn card, face up, onto the card with the smallest integer among the face-up topmost cards on the table with an integer greater than or equal to X written on them. If there is no such card on the table, put the drawn card on the table, face up, without stacking it onto any card.
  • Then, if there is a pile consisting of K face-up cards on the table, eat all those cards. The eaten cards all disappear from the table.

For each card, find which of the N moves eats it. If the card is not eaten until the end, report that fact.

Constraints

  • All values in input are integers.
  • 1 \le K \le N \le 2 \times 10^5
  • P is a permutation of (1,2,\dots,N) (i.e. a sequence obtained by rearranging (1,2,\dots,N)).

Input

Input is given from Standard Input in the following format:

N K
P_1 P_2 \dots P_N

Output

Print N lines.
The i-th line (1 \le i \le N) should describe the card with the integer i written on it. Specifically,

  • if the card with i written on it is eaten in the x-th move, print x;
  • if that card is not eaten in any move, print -1.

Sample Input 1

5 2
3 5 2 1 4

Sample Output 1

4
3
3
-1
4

In this input, P=(3,5,2,1,4) and K=2.

  • In the 1-st move, the card with 3 written on it is put on the table, face up, without stacked onto any card.
  • In the 2-nd move, the card with 5 written on it is put on the table, face up, without stacked onto any card.
  • In the 3-rd move, the card with 2 written on it is stacked, face up, onto the card with 3 written on it.
    • Now there is a pile consisting of K=2 face-up cards, on which 2 and 3 from the top are written, so these cards are eaten.
  • In the 4-th move, the card with 1 written on it is stacked, face up, onto the card with 5 written on it.
    • Now there is a pile consisting of K=2 face-up cards, on which 1 and 5 from the top are written, so these cards are eaten.
  • In the 5-th move, the card with 4 written on it is put on the table, face up, without stacked onto any card.
  • The card with 4 written on it was not eaten until the end.

Sample Input 2

5 1
1 2 3 4 5

Sample Output 2

1
2
3
4
5

If K=1, every card is eaten immediately after put on the table within a single move.


Sample Input 3

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

Sample Output 3

9
9
9
15
15
6
-1
-1
6
-1
-1
-1
-1
6
15
H - Kth Number

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

配点 : 500 点

問題文

0 以上 M 以下の整数からなる長さ N の数列 A=(A_1,A_2,\dots,A_N) があります。

今からすぬけくんが以下の操作 1, 2 を順に行います。

  1. A_i=0 を満たすそれぞれの i について、1 以上 M 以下の整数を独立かつ一様ランダムに選び、A_i をその整数で置き換える。
  2. A を昇順に並び替える。

すぬけくんが操作 1, 2 を行ったあとの A_K の期待値を \text{mod } 998244353 で出力してください。

「期待値を \text{mod } 998244353 で出力」とは 求める期待値は必ず有理数となることが証明できます。 またこの問題の制約下では、その値を互いに素な 2 つの整数 P, Q を用いて \frac{P}{Q} と表したとき、 R \times Q \equiv P\pmod{998244353} かつ 0 \leq R \lt 998244353 を満たす整数 R がただ 1 つ存在することが証明できます。この R を出力してください。

制約

  • 1\leq K \leq N \leq 2000
  • 1\leq M \leq 2000
  • 0\leq A_i \leq M
  • 入力は全て整数

入力

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

N M K
A_1 A_2 \dots A_N

出力

すぬけくんが操作 1, 2 を行ったあとの A_K の期待値を \text{mod } 998244353 で出力せよ。


入力例 1

3 5 2
2 0 4

出力例 1

3

すぬけくんは操作 1 において A_2 を 1 以上 5 以下の整数で置き換えます。この整数を x とすると、

  • x=1,2 のとき、すぬけくんが操作 1, 2 を行ったあと A_2=2 です。
  • x=3 のとき、すぬけくんが操作 1, 2 を行ったあと A_2=3 です。
  • x=4,5 のとき、すぬけくんが操作 1, 2 を行ったあと A_2=4 です。

よって、A_2 の期待値は \frac{2+2+3+4+4}{5}=3 です。


入力例 2

2 3 1
0 0

出力例 2

221832080

期待値は \frac{14}{9} です。


入力例 3

10 20 7
6 5 0 2 0 0 0 15 0 0

出力例 3

617586310

Score : 500 points

Problem Statement

We have a sequence of length N consisting of integers between 0 and M, inclusive: A=(A_1,A_2,\dots,A_N).

Snuke will perform the following operations 1 and 2 in order.

  1. For each i such that A_i=0, independently choose a uniform random integer between 1 and M, inclusive, and replace A_i with that integer.
  2. Sort A in ascending order.

Print the expected value of A_K after the two operations, modulo 998244353.

How to print a number modulo 998244353? It can be proved that the sought expected value is always rational. Additionally, under the Constraints of this problem, when that value is represented as \frac{P}{Q} using two coprime integers P and Q, it can be proved that there is a unique integer R such that R \times Q \equiv P\pmod{998244353} and 0 \leq R \lt 998244353. Print this R.

Constraints

  • 1\leq K \leq N \leq 2000
  • 1\leq M \leq 2000
  • 0\leq A_i \leq M
  • All values in the input are integers.

Input

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

N M K
A_1 A_2 \dots A_N

Output

Print the expected value of A_K after the two operations, modulo 998244353.


Sample Input 1

3 5 2
2 0 4

Sample Output 1

3

In the operation 1, Snuke will replace A_2 with an integer between 1 and 5. Let x be this integer.

  • If x=1 or 2, we will have A_2=2 after the two operations.
  • If x=3, we will have A_2=3 after the two operations.
  • If x=4 or 5, we will have A_2=4 after the two operations.

Thus, the expected value of A_2 is \frac{2+2+3+4+4}{5}=3.


Sample Input 2

2 3 1
0 0

Sample Output 2

221832080

The expected value is \frac{14}{9}.


Sample Input 3

10 20 7
6 5 0 2 0 0 0 15 0 0

Sample Output 3

617586310
I - Subsequence LCM

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

配点 : 525 点

問題文

長さ N の正整数列 A=(A_1,A_2,\dots,A_N) と正整数 M が与えられます。A の空でない連続とは限らない部分列であって、列に含まれる要素の最小公倍数が M になるようなものの個数を 998244353 で割った余りを求めてください。ただし、2 つの部分列が列として同じでも、取り出す位置が異なるならば区別するものとします。また、列の要素の個数が 1 のときはその要素自身を最小公倍数とします。

制約

  • 1\leq N\leq 2\times 10^5
  • 1\leq M\leq 10^{16}
  • 1\leq A_i\leq 10^{16}
  • 入力は全て整数

入力

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

N M
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

4 6
2 3 4 6

出力例 1

5

A の部分列であって,列に含まれる要素の最小公倍数が 6 となるものは (2,3),(2,3,6),(2,6),(3,6),(6) の 5 つです。


入力例 2

5 349
1 1 1 1 349

出力例 2

16

部分列が列として同じでも、取り出す位置が異なるならば区別することに注意してください。


入力例 3

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

出力例 3

2688

Score: 525 points

Problem Statement

You are given a sequence of positive integers A=(A_1,A_2,\dots,A_N) of length N and a positive integer M. Find the number, modulo 998244353, of non-empty and not necessarily contiguous subsequences of A such that the least common multiple (LCM) of the elements in the subsequence is M. Two subsequences are distinguished if they are taken from different positions in the sequence, even if they coincide as sequences. Also, the LCM of a sequence with a single element is that element itself.

Constraints

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

Input

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

N M
A_1 A_2 \ldots A_N

Output

Print the answer.


Sample Input 1

4 6
2 3 4 6

Sample Output 1

5

The subsequences of A whose elements have an LCM of 6 are (2,3),(2,3,6),(2,6),(3,6),(6); there are five of them.


Sample Input 2

5 349
1 1 1 1 349

Sample Output 2

16

Note that even if some subsequences coincide as sequences, they are distinguished if they are taken from different positions.


Sample Input 3

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

Sample Output 3

2688