#P11220. 【MX-S4-T4】「yyOI R2」youyou 的三进制数

【MX-S4-T4】「yyOI R2」youyou 的三进制数

Background

Original link: https://oier.team/problems/S4D。

Problem Description

There are n+1n + 1 numbers from 00 to nn.

Let (x)3(x)_{3} denote the ternary representation of the decimal number xx. Unless explicitly stated otherwise, all numbers are in decimal form.

youyou wants to construct a non-negative integer sequence aa of length pp (p≥1p \ge 1), such that:

  • ai∈[0,n]a_i \in [0,n]。
  • There do not exist i,ji,j (1≤i<j≤p1 \le i < j \le p) such that ai=aja_i = a_j。
  • For any 1≤i<n1 \le i < n, aia_i and ai+1a_{i+1} satisfy at least one of the following four conditions:
    1. Remove the last digit of (ai)3(a_i)_3, and it becomes exactly (ai+1)3(a_{i+1})_3 (if there is only one digit, then after removing it the number becomes 00)。
    2. Append a digit t(0≤t≤2)t(0 \le t \le 2) to the end of (ai)3(a_i)_3, and it becomes exactly (ai+1)3(a_{i+1})_3 (if ai=0a_i = 0, then after appending, leading 00 is discarded)。
    3. ai≤wa_i \le w, the last digit of (ai)3(a_i)_3 is not 00, and moving the last digit to the front makes it equal to (ai+1)3(a_{i + 1})_3。
    4. When the length of (ai)3(a_i)_3 is ≥2\ge 2 and the second highest digit of (ai)3(a_i)_3 is non-zero, move the first digit of (ai)3(a_i)_3 to the end; the decimal value of the resulting number is ≤w\le w, and it is exactly equal to (ai+1)3(a_{i+1})_3。

Such a sequence aa is called "perfect".

youyou believes that a decimal triple (x,y,z)(x,y,z) is good if and only if it satisfies all of the following conditions:

  • 0≤x,y,z≤n0 \le x,y,z \le n, and x≠yx \neq y。
  • There exists at least one "perfect" sequence bb such that in decimal we have b1=xb_1 = x and bs=yb_s = y, where ss is the length of the sequence。
  • There exists at least one "perfect" sequence cc such that in decimal we have c1=zc_1 = z. At the same time, for any bb mentioned above, there is exactly one pair (i,j)(i, j) satisfying 1≤i≤∣b∣1 \le i \le |b| and 1≤j≤∣c∣1 \le j \le |c| such that bi=cjb_i = c_j。

For each 0≤z≤n0 \le z \le n, find the number of ordered pairs (x,y)(x,y) that can form a good triple (x,y,z)(x,y,z).

Input Format

One line containing a positive integer nn and a non-negative integer ww, whose meanings are given in the statement.

Output Format

Output n+1n + 1 lines. The (i+1)(i + 1)-th line contains one number, indicating that when z=iz = i, the number of ordered pairs (x,y)(x,y) that can form a good triple (x,y,z)(x,y,z).

4 3
20
20
20
20
20

Hint

[Sample Explanation #1]

There are 55 numbers, whose ternary representations are 0,1,2,10,110,1,2,10,11 respectively.

When z=0,1,2,3,4z = 0,1,2,3,4, all pairs (x,y)(x,y) with x≠yx \neq y satisfy the requirement.

Below is a proof that the triple (2,3,1)(2,3,1) is good.

When x=2,y=3,z=1x = 2, y = 3, z = 1, the sequence bb can be {2,0,1,3}\{2,0,1,3\}.

Here, b1,b2b_1,b_2 satisfy condition 11, b2,b3b_2,b_3 satisfy condition 22, and b3,b4b_3,b_4 satisfy condition 22.

It can be proved that only this sequence bb satisfies the requirement. Therefore, there exists c={1}c = \{1\} such that B∩C={1}B \cap C = \{1\}. Hence (2,3,1)(2,3,1) is a good triple.

[Sample #2]

See ternary/ternary2.in and ternary/ternary2.ans in the attachment.

This sample satisfies the constraints of test points 4∼64 \sim 6.

[Sample #3]

See ternary/ternary3.in and ternary/ternary3.ans in the attachment.

This sample satisfies the constraints of test points 7∼107 \sim 10.

[Sample #4]

See ternary/ternary4.in and ternary/ternary4.ans in the attachment.

This sample satisfies the constraints of test points 13∼1513 \sim 15.

[Sample #5]

See ternary/ternary5.in and ternary/ternary5.ans in the attachment.

This sample satisfies the constraints of test points 20∼2520 \sim 25.

[Constraints]

This problem has 2525 test points, 44 points each.

Test Point ID nn ww Special Property
1∼31 \sim 3 ≤18\le 18 None
4∼64 \sim 6 ≤242\le 242
7∼107 \sim 10 ≤6560\le 6560 ≤6560\le6560
11∼1211\sim12 ≤105\le 10^5 ≤105\le 10^5
13∼1513\sim15 ≤3×105\le 3 \times 10^5 Yes
16∼1716\sim 17 =0=0 None
18∼1918\sim 19 =n=n
20∼2520\sim25 ≤3×105\le 3 \times 10^5

Special property: w≥104w \ge 10^4。

For all data, it is guaranteed that 1≤n≤3×1051 \le n \le 3 \times 10^5 and 0≤w≤n0 \le w \le n。

Translated by ChatGPT 5