HDU 2082 female function template problem

Source: Internet
Author: User
Tags integer division power of power

Generate functions, English is generating function. I am sorry, this article only describes one of the usage of the generated function.

The generating function is to say, construct such a polynomial function g (x), so that the N-square coefficient of x is f (n).

For the parent function, I see the most of the two words:

1. "The addition rule of combinatorial problems corresponds to the power of power series." ”

2. "The Discrete series and power series one by one correspond to the relationship between the discrete series of the interaction between the power series of the operation relationship, and finally from the form of power series to determine the structure of discrete series." “

In fact, these two words I am not too understand. Put it here first, maybe you will understand it later.

Let's start with an example from Daniel's blog:

There are 1 grams, 2 grams, 3 grams, 4 grams weight each one, ask you can weigh what kinds of weight? How many options are there for each weight?

Here is the idea of solving this problem with a parent function:

First, we use X to represent weights, and the exponent for x indicates weight of weights. So, if you use a function to represent the weight that each weight can weigh,

A 1 gram weight can be represented by the function x^0 + x^1,

A 2 gram weight can be represented by the function x^0 + x^2,

In turn.

If we multiply the above 2 polynomial, we can get x^0 + x^1 + x^2 + x^3. Continue multiplying it with x^0 + x^3, get x^0 + x^1 + x^2 + 2*x^3 + x^4 + x^5 + x^6.

Clever you, is not found what?

If not, then multiply it with x^0+x^4, and finally get x^0 + x^1 + x^2 + 2*x^3 + 2*x^4 + 2*x^5 + 2*x^6 + 2*x^7 + x^8 + x^9 + x^10.

Since the exponent of x represents the weight, the result is that all the schemes are obtained when multiplying by the algorithm of the power (multiplied by the same power and exponential). Also, the coefficients in front of each x represent several scenarios for it.

It's amazing ....

It should be noted that if there are 2 weights of 1 grams, it should be indicated with x^0 + x^1 + x^2 instead of X^0 + 2*x^1.

The parent function can also solve other problems, such as Integer partitioning.

Integer division is a very classical problem, the division of Rules is no longer detailed, direct talk about ideas. Compared with the above problem, the number of each weight is no longer 1, but unlimited. So

1 grams of weight can be used x^0 + x^1 + x^2 + x^3 ... Said

2 grams of weight can be used x^0 + x^2 + x^4 + x^6 ... Said

3 grams of weight can be used x^0 + x^3 + x^6 + x^9 ... Said

In turn.

After multiplying, the coefficients of x^n are obtained.

All in all, to solve this kind of problem, as long as the simulation of 2 polynomial multiplication is good.

The idea is to open 2 arrays, c1[] Save the currently obtained polynomial coefficients, c2[] Save the temporary results of each calculation, when each calculation is complete, assign it to C1, and then C2 zero.

When calculating, open a 3-layer for loop. Outermost, recording that it is multiplying with the first polynomial. The second layer, which represents each item in the C1, and the third layer represents each item in the subsequent multiplicative polynomial.

Code comments are written in a very detailed and easy to understand.

#include <iostream>#include<cstdio>#include<cstring>using namespacestd;Const intn= -;intc1[n+Ten],c2[n+Ten],num[ -];intMain () {//freopen ("Input.txt", "R", stdin);    intT; scanf ("%d",&t);  while(t--) {memset (C1,0,sizeof(c1));//c1[] Save the currently obtained polynomial coefficientsmemset (C2,0,sizeof(C2));//c2[] Save temporary results for each calculation         for(intI=1; i<= -; i++) scanf ("%d",&Num[i]); c1[0]=1;//It 's equivalent to multiplying the x^0 by the polynomial of the back.         for(intI=1; i<= -; i++) {//to multiply by 26 polynomial             for(intj=0; j<=n;j++)//The index of the C1                 for(intk=0; K<=num[i] && j+k*i<=n;k++)//K*i represents the exponent of the polynomial, (x^0*i + x^1*i + x^2*i + ...)C2[J+K*I]+=C1[J];//The index is j+k*i, and the number of additions depends only on the coefficients of c1[j], since the coefficients of the multiplicative polynomial are all 1             for(intj=0; j<=n;j++) {C1[j]=C2[j]; C2[J]=0; }        }        intans=0;  for(intI=1; i<=n;i++) ans+=C1[i]; printf ("%d\n", ans); }    return 0;}

HDU 2082 female function template problem

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.