Female functions and related algorithm problems

Source: Internet
Author: User

The female function is a generating function, so a polynomial function g (x) is constructed, so that the N-square coefficient of x is f (n), which is an important theory and tool in combinatorial mathematics, especially in counting.

(1+a1x) (1+A2X) (1+a3x) ... (1+anx) =1+ (A1+a2+a3+...+an) x+ (a1a2+a1a3+...+an-1an) x2+...+ (a1a2a3*...*an) xn

From this we can see:

1. The coefficient of x is a1,a2,... an individual combination of the whole. Equivalent from A1,a2,... an select 1 to combine, then add together

2. The coefficient of X2 is the A1,A2,... an of two combinations of the whole. Equivalent from A1,a2,... an select 2 to combine, then add together

.........

N. The coefficient of Xn is the total of n combinations of A1,a2,.... an (only 1). Equivalent from A1,A2,... an select N to combine, then add together

If we set the value of A1,a2,... an to 1,

The original is turned into

(1+x) n=1+c (n,1) x+c (n,2) x2+c (n,3) x3+...+c (n,n) xn

Using this relationship, we can solve some algorithmic problems

Example 1, a coin with a face value of three coins each, asked how many combinations?

We think of all the combinations:

(Do not take a face value of 1, take a face value of 1) * (do not take a face value of 2, take a face value of 2) * (do not take a face value of 3, take a face value of 3)

= = (the number of face value is 0, the face value is 1, the face value is 2, the face value is 3, the face value is 4, the face value is 5, the face value is 6 of the number)

We use X to denote the coin, and the index of x indicates the value of the coin, so:

1 grams of coin combinations can be expressed in function x0+x1, x0 means not to take a face value of 1, X1 to take a face value of 1

2 grams of coin combinations can be expressed in function x0+x2, x0 means not to take a face value of 2, X2 to take a face value of 2

3 grams of coin combinations can be expressed in function x0+x3, x0 means not to take a face value of 3, X3 to take a face value of 3

All the combinations can be expressed as follows:

(1+x) (1+x2) (1+X3) ==1+x+x2+2x3+x4+x5+x6

The coefficient before X is the number of combinations that weigh the denomination of the corresponding coin.

For example, a face value of 3 has two combinations, (take a face value of 3, and that a 1 face value of 1 and face value of 2)

Example 2, with a face value of 1 coins 3, face value of 2 coins 2, 3 coins 1, asked how many combinations?

1 grams of coin combination can be used function x0+x1 +x2+x3, X0 said not to take a face value of 1, X1 to take 1 face value 1, X2 to take 2 face value 1, x3 to take 3 face value 1

2 grams of the coin combination can be represented by the function x0+x2+x4, X0 said not to take a face value of 2, X2 to take a face value of 2, X4 said to take 3 face value 2

3 grams of coin combinations can be represented by the function x0+x3, X0 said not to take a face value of 3, X3 to take 1 face value 3, X6 said to take 2 face value 3

All the combinations can be expressed as follows:

(X0+x1 +x2+x3) * (x0+x2+x4) * (X0+X3)

Example 3, there are hundreds of coins with a face value of three-way coin, ask how many combinations?

1 grams of the coin combination can be represented by the function x0+x1 +x2+...+xn, respectively, 0, 1, 2 ... n cases.

The 2 gram coin combination can be represented by a function x0+x2+x4+...+x2n, representing 0, 1, 2, n cases respectively.

The 3 gram coin combination can be represented by a function x0+x3+x6+...+x3n, representing 0, 1, 2, n cases respectively.

All the combinations can be expressed as follows:

(X0+x1 +x2+...+xn) * (x0+x2+x4+...+x2n) * (x0+x3+x6+...+x3n)

Now for example 2 examples of code and Analysis

There are 3 coins with a face value of 1, 2 coins with a face value of 2, 3 coins of 1, q How many combinations?

//(x0+x1 +x2+x3) * (x0+x2+x4) * (X0+X3)structcoin{intvalue; intNumber ;};Const intMAX = -;intMain () {Coin coin[3]; coin[0].value =1; coin[0].number =3; coin[1].value =2; coin[1].number =2; coin[2].value =3; coin[2].number =1; intResult[max] = {0 }; intTemp[max] = {0 }; intNumber ;  while(Cin >>Number ) {         for(inti =0; i < MAX; i++) {Result[i]=0; Temp[i]=0; }         for(inti =0; I <=coin[0].number;i++) {Result[i*coin[0].value] =1; }         for(inti =1; I <3; i++)        {             for(intj =0; J <=number; J + +)            {                 for(intK =0, index =0; Index<=coin[i].number && (k + j) <=Number ; Index+ +, K + =coin[i].value) {Temp[j+ K] + =Result[j]; }            }             for(intj =0; J <=number; J + +) {Result[j]=Temp[j]; TEMP[J]=0; }} cout<< Result[number] <<Endl; }}

We analyze the ②.

Its cyclic invariant is result[0]-result[number] records the number of all combinations of each new denomination as Coin[i].value, with the face value of I in result[i] combinations.
initialization: First, it is proved that it was established before the first iteration. In ①, the combination of the first denomination coins is assigned to result,for (int i = 0; I <=coin[0].number;i++) ①
{
Result[i*coin[0].value] = 1;
}

At this point, result records all combinations of the first denomination coins and their number. So the loop invariant is correct before the loop starts.

hold : Here I represents the formula for the current calculation, because ① has already computed the first formula, so I start from 1, to 2 cutoff (the subscript of the array is starting from 0)

J Each need to update the face value, so starting from 0 to number cutoff,

K represents the increment of the I formula, and the first iteration of the index i clause. Therefore, for the I formula, K is coin[i].value,index from 0 to Coin[i].number Cutoff; When index has been coin[i].number or the current face value J plus increment K has exceeded the required face value to stop the loop

Each iteration has a value of J, and now the face value becomes j+k, so the face value is j+k for the original face value of J plus the number of the new face value of j+k

The temp is then assigned to result, and the temp is zeroed to begin the next iteration.

termination : For this algorithm, when I is greater than the length of the array (that is, all denomination coins are counted), the For loop ends. At this point Result[0]-result[number] records the number of all combinations with denominations of 0 to numbers

Reference article:

explanation of the parent function (generating functions) http://www.wutianqi.com/?p=596

What is a build function? http://www.matrix67.com/blog/archives/120

Female functions and related algorithm problems

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.