The principle of common type parent function and the detailed template code

Source: Internet
Author: User

There are many kinds of female functions, the most commonly used are common type and exponential parent function. The difference between the two is: the normal type of the main function is to find a combination of the number of schemes, and the exponential female function is to find multiple permutations. The following is only an explanation of the common type parent function.


definition :

If the function g (x) is =a0+a1*x+a2*x^2+......+an*x^n, then the function g (x) is the parent function of the sequence A0, A1, A2 、...... an.


For example: (1+x) ^n=1+c (n,1) *x+c (n,2) *x^2+......+c (n,n) *x^n is the parent function of sequence C (n,1), C (n,2) 、......、 C (n,n), where C (n,m) is the combined number.


Classic Examples :

The weights for 1,2,4 are 1,3,2, respectively, and Q:

1. How many different kinds of quality can be weighed.

2. What are some possible ways to weigh 3 items?


It is possible to construct the function g (x) = (1+x) * (1+x^2+x^4+x^6) * (1+x^4+x^8), where 1 of the first parenthesis represents 0 weights for a weight of 1, X for weights of 1, 1 for weights of 1 in the second bracket, and 2 for weight with a quality of + x^ 2 for the weight of 2 with 1 pieces, X^4 said the weight of 2 of the weights with 2 pieces, namely x^ (2*2), X^6 said the weight of 2 of the weights used 3, that is x^ (2*3). You don't have to understand why you're doing this, just know what to do.


To summarize, each parenthesis represents a usage of a weight. X^n said that the weight of the mass of n was used for 1 pieces. You can use at least 0 pieces, which is 1 of the formula. You can use up to M, which is x^ (n*m).


Knowing the correspondence, how should the code be written? In fact, it is simple to simulate the process of manually calculating the multiplication of the above polynomial. Like the above.

G (x) = (1+x) * (1+x^2+x^4+x^6) * (1+x^4+x^8)

= (1+x^2+x^4+x^6 + x+x^3+x^5+x^7) * (1+x^4+x^8)

= (1+x+x^2+x^3+x^4+x^5+x^6+x^7) * (1+x^4+x^8)

= (1+x+x^2+x^3+x^4+x^5+x^6+x^7 + x^4+x^5+x^6+x^7+x^8+x^9+x^10+x^11 + x^8+x^9+x^10+x^11+x^12+x^13+x^14+x^15)

=1+x+x^2+x^3+2*x^4+2*x^5+2*x^6+2*x^7+2*x^8+2*x^9+2*x^10+2*x^11+x^12+x^13+x^14+x^15


Because I'm not too familiar with how to insert a mathematical formula, the above equation looks a little uncomfortable ... The above process is the result of constant multiplication of the first I polynomial. In the final formula, M*x^n indicates that an item of mass n can be weighed in M mode. There are x~x^15 of different quality, that is, the maximum can weigh 15 of different quality items.


The following first gives the template code, and explains the meaning of the code:


#define MAXN
int A[MAXN],B[MAXN];
int S[MAXN],E[MAXN],V[MAXN];

void mu (int n)
{//n is the number of factors 
	int i,j,k;
	memset (A,0,sizeof (a));
	A[0]=1;
	for (i=1;i<=n;i++)
	{//Pre-I multiplies 
		memset (b,0,sizeof (b));
		J for the number of possible items of article I, J*v[i] that is, the factor for the J in the I Bracket for
		(j=s[i];j<=e[i]&&j*v[i]<=maxn;j++) for
			(k=0;k+j*v [i]<=maxn;k++)//Calculate the effect on the coefficients of the k term of the result of the i-1 Xiangxiang multiplier 
				b[k+j*v[i]]+=a[k];
		memcpy (a,b,sizeof (b)); Save B Array back to array a 
	}	
}

The MAXN in the above code is the maximum value of the exponent that results in the possible generation of X.

A array stores the final result, and the B array stores the intermediate results
S[i] (start array) is the minimum number of the first variable, generally 0,e[i] (end array) is the maximum number of the first variable
V[i] denotes the value of the first unknown quantity, i.e. X^v[i]
A[i] stores the coefficients in front of an unknown amount x^i


This is the result of the multiplication of the i-1 item before a store, and then iterates through each of the variables in item I, multiplying it with each item in a, temporarily storing the result in the B array, and finally saving the array of a.


For example: to find different numbers of different weights can be composed of the mass of the number of I
V[i] Deposit I weight mass, s[i] The minimum number of deposit I weight, e[i] The maximum number of deposit I weight, a[i] deposit can be composed of the number of species I

Finally give a case and I write the analysis: HDU 1028

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.