#P5040. [SCOI2006] k进制集合的映射

[SCOI2006] k进制集合的映射

Description

Let A(N,K)A(N,K) be the set of all NN-digit base-KK integers aa (the most significant digit of aa may be 00; for example, 00230023 can be regarded as a 44-digit base-88 number or a 44-digit base-55 number. Under the conditions specified in the problem, it can be uniquely determined), where 2K60002 \le K \le 6000 and N=2,3,4N = 2, 3, 4. That is,

$$A(N,K)={a|a=a_1a_2a_3\cdots a_N,0≤a_i≤K-1,i=1,\cdots,N}$$

Let D(N1,K)D(N-1,K) be a subset of A(N1,K)A(N-1,K). It is the set of (N1)(N-1)-digit base-KK integers dd generated from A(N,K)A(N,K). The generation rule is as follows:

For any dD(N1,K)d \in D(N-1,K), there exists aA(N,K)a \in A(N,K) such that d=Image(a)d = Image(a), where d=d1d2dN1d = d_1d_2\cdots d_{N-1} and di=min(ai,ai+1)d_i = min(a_i,a_{i+1}). That is, did_i is the smaller one of aia_i and ai+1a_{i+1}.

Note 1: We call this rule a mapping from A(N,K)A(N,K) to A(N1,K)A(N-1,K), written as d=Image(a)d = Image(a). It can be proven that this mapping is many-to-one. That is, if d,eD(N1,k)d,e \in D(N-1,k) and ded \not= e, then for any elements a,cA(N,K)a,c \in A(N,K) satisfying d=Image(a)d = Image(a) and e=Image(c)e = Image(c), we have aca \not= c.

Note 2: For some K,NK,N, D(N1,K)D(N-1,K) is a proper subset of A(N1,K)A(N-1,K). For example, when K=4,N=4K=4,N=4, there does not exist any aA(4,4)a \in A(4,4) such that Image(a)=(323)Image(a)=(323).

Task: Read two integers N,KN,K separated by a space from the input text file, and then output the value of the following expression to the specified output text file:

$$f(N,K)=\sum_{a\in A(N,K),Image(a)=d}(\prod_{i=1}^{N-1}(d_i+1))$$

The expression above means: for all elements aa in A(N,K)A(N,K), let its image be d=Image(a)=d1d2dN1d = Image(a) = d_1d_2\cdots d_{N-1}, add 11 to each digit of dd, take the product, and then sum these products.

Where

$$\prod^{N-1}_{i=1}(d_i+1)=(d_1+1)(d_2+1)\cdots(d_{N-1}+1)$$

Example: Let N=2,K=3N=2,K=3. Then A(N,K)=00,01,02,11,10,12,20,21,22A(N,K)={00,01,02,11,10,12,20,21,22}, and the correct output should be 1414.

Hint: You should first build a suitable method for computation. Directly computing using the expression of f(N,K)f(N,K) will cause most testdata to time out.

Input Format

The input file contains only one line: two integers NN and KK separated by a space.

Output Format

Output a single big integer, which is the computed result.

2  3
14

Hint

About the tests:

Full marks are given if the number is completely correct. When the number of digits of the output exceeds 1515, if only the last two digits are incorrect, half of the score is given. (Each required result does not exceed 101910^{19}.)

Translated by ChatGPT 5