[Description]
Given a positive integer k (3 ≤ k ≤ 15), the sum of the power of all K and the power of all finite k not equal to each other forms an ascending sequence. For example, when K = 3, the sequence is:
1, 3, 4, 9, 10, 12, 13 ,...
(The sequence is actually: 3 ^ 0 + 3 ^ 0 + 3 ^ 2 ^ 1 + 3 ^ 2 ^ 0 + 3 ^ 1 + 3 ^ 2 ,...)
Please obtain the value of the nth entry of the sequence (expressed in the decimal number ).
For example, for k = 3, n = 100, the correct answer should be 981.
[Input format]
The input file contains only one line, which is a positive integer separated by a space:
K N
(The meanings of K and N are the same as those described above, and 3 ≤ k ≤ 15, 10 ≤ n ≤ 1000 ).
[Output format]
The output file is the calculation result and is a positive integer (in all test data, the result cannot exceed 2.1*10 ^ 9 ). (Do not enter spaces or other symbols before integers ).
[Example input]
3 100
[Sample output]
981
[Analysis]
The specific operation method is. Two arrays A, B, A [I] are K ^ I, and B [I] Are the sequences we construct.
The two pointers P1 and P2 correspond to A and B respectively.
Initialize P2 = 0, P1 = 1.
For the currently constructed B [I], there are two situations.
- A [P1] ≠ B [P2]: B [I] = A [P1] + B [P2 ++]
- A [P1] = B [P2]: B [I] = A [++ P1]; P2 = 1
Then output B [N.
# Include <stdio. h> # define maxn 2000int A [maxn], B [maxn]; int K, N, P1, P2; int main () {scanf ("% d ", & K, & N); A [1] = 1; P1 = 1; for (INT I = 1; I <= N; ++ I) if (B [P2] = A [P1]) {++ P1; A [P1] = A [P1-1] * K; B [I] = A [P1]; P2 = 1;} else {B [I] = A [P1] + B [P2 ++];} printf ("% d \ n", B [N]); Return 0 ;}