HDU 1028 Ignatius and the Princess III Ignatius and Princess III (AC code) female functions

Source: Internet
Author: User

I heard the use of recursion, DP and other methods to do, but this problem is the introduction of the female function classic Oh ~ So I used the female function

1#include <iostream>2 #defineN 1203 using namespacestd;4 intans[n+1],sup[n+1];//ans Save the answer, SUP save temporary value5 voidMain ()6 {7     intnum=0, i,j,k;8      for(i=0; i<n+1; i++)//all initialized to 19ans[i]=1;Ten      for(i=2; i<=n;i++)//section I parentheses One     { A          for(j=0; j<=n;j+=i)//Item J of the bracketed paragraph I -              for(k=0; k+j<=n;k++)//the k number in the provisional answer -sup[j+k]+=Ans[k];  the          for(k=0; k<=n;k++)//each settlement of a parenthesis, the answer must be saved, the next parenthesis multiplied by the answer to the new answer -ans[k]=Sup[k]; -memset (SUP,0,sizeof(SUP));//Temporary array Clear 0 -     } +      while(cin>>k)//in C + +, that's the way to write. -cout<<ans[k]<<Endl; +}
1028

Test instructions: Enter a number n, print out how many of these numbers can be composed, and each method is not recorded in the order of arrangement. The numbers used to make up can be 1, 2, 3....N. For example, n 1 is composed of N, and an n is also composed of N. This is even two kinds. 1=1,2=1+1=2,3=3=1+2=1+1+1, while 1+2 and 2+1 can only be counted as one. n Maximum is 120.

Train of thought: Mother function looked for a day did not understand what to use things, online a bunch of people speak unclear, perhaps I did not find good textbooks. I can only solve this problem ah .... The principle of the parent function is not spoken. How to implement several parentheses to multiply.

It's better than talking a bunch of crap, for example:

We want to calculate the n is equal to 120, simplify it to 5, that is, set n maximum of 5, the same reason. 51 There are 7 ways to do it! Write your own handwriting.

If you want to produce a result, then a total of 5 parentheses are multiplied, respectively:

Why is 5 brackets?

The 1 brackets: Can be composed of up to 5 1 .

2 brackets: There can be at most two 2 , right? If 3 2 is already 6 ,6 is over 5.

3rd to 5th Bracket : Well, you 've learned the analogy!

How to multiply?

The way to teach a junior high school teacher is to multiply the 2nd bracket by the 1th parenthesis, and then the 3rd parenthesis multiplied by the last one ....

Altogether 4 steps, namely n-1 step, then i=2to n is n-1 step? such as i=2, 3, 4, 5, is 4 steps.

Then, what about multiplying the two parentheses?

To add a loop to the I loop, multiply each number in the right parenthesis of each step by the left parenthesis once. What is the upper and lower limit of j? The lower limit is 0, and the upper limit is 5, or N. But the process is not just a self-increment, but a unit of self-increment, why? The value of the 1th step J will be 0, 2, 4, the interval is 2, and the 2nd step in the value of J will be 0, 3, the interval is 3, if n>5, the 2nd step of the value of J will be 0, 3, 6. So! J=0 to n increment mode (j+=i).

Is it over now? No, it's not!

The previous step simply splits the right parenthesis in each step by the left parenthesis, but the left parenthesis has not been removed. Each value in each J, that is, the right parenthesis, is multiplied by each value in the left parenthesis. Then we have to add a layer of loop, the lower limit is 0, the upper limit is 5, which is N. And why? There is not 6 in the 1th parenthesis, and there is a 1 in the front of each parenthesis, do not fall. There is no way to talk about it.

Speaker Code:

The ANS array in the code first holds each of the 1th brackets, which is all 1, such as ans[0]=1,ans[1]=1 ...

This array can not be changed in the same I value, I originally wanted to use an array to solve, but later found to be uncertain, so opened an auxiliary array sup.

When a number in the closing parenthesis is multiplied by an opening parenthesis, the multiplied result is accumulated in the SUP array, the number of the entire closing parenthesis is multiplied once, and the value in the SUP array is stored in the ANS array as a temporary answer.

In this way the last preserved in the ANS is the desired coefficient, and the subscript of ans represents several sides of X, for example, Ans[4] represents the 4-square factor of X.

The whole process of multiplication to realize its essence, and a lot of attention to the place, look at the code is not a good habit.

HDU 1028 Ignatius and the Princess III Ignatius and Princess III (AC code) female functions

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.