[Finally, I can write the series freely]

Source: Internet
Author: User

[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.

    1. A [P1] ≠ B [P2]: B [I] = A [P1] + B [P2 ++]
    2. 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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.