Back Gear | apple

Source: Internet
Author: User

Background: Tonight in TYVJ to participate in a game, did not expect a shameful kneeling. The first question of the Earth people know SPFA, the result I wrote a coordinate class DP also got 30 points ... Tragedy. The only consolation is that I used combinatorial mathematics to make the second question, laying the foundation for our school to have five people rushing into the first six. First, the second question is OK.

Title: Due to tyvj comparison pit, the title of the game is gone! Can only say the general meaning. Given n apples, the request is divided into 3 bags. The bag is disorderly, ask how many kinds of cases, and the K modulus.

Input interpretation: First line, enter two integers n,k

Output interpretation: The output is only one line, that is, the total number of scenarios to K modulo results.

Input Sample: 11 10000

Output Sample: 9525

Topic Analysis:

It is clear that each Apple has three possibilities, which are placed in the three-way bag. So the total number of n apples is 3^n.

We may wish to use the repulsion principle to classify all situations:

(1) When there are two bags for 0 o'clock. Then there will be 0, 0, all, 0, all, 0, all, 0, 3 cases. Obviously this should be considered a situation. The reason is three rather than six (see below), because when we put all the apples into a bag, it is obviously equivalent to placing an apple. There is no doubt that there is only one case for an apple.

(2) for the rest of the situation, we might as well put three bags according to the number of apples sorted. For example. It is clear that this set of numbers has six different sequences. That's what we're talking about in general.

So we can get the formula: ans= (3^n-3)/6+1. In fact, the use of division inverse can be further simplified, ask the students to think for themselves. (The explanation is here, the source code will not have to send it?) In fact, the code was swallowed by tyvj ... )

Back Gear | apple

Related Article

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.