Polynomial multiplication to obtain the number of combinations RC (n, r) -- linked list implementation

Source: Internet
Author: User

Set the set S to {N1 * E1, N2 * E2 ,... nt * et}, N1 + N2 +... + Nt = N, take R from S, and obtain the number of combinations RC (n, R ).

Polynomial

 

Then RC (n, R) is the coefficient of XR In the polynomial, that is, RC (n, R) = ar.

Based on the above method, programming implementation: obtain any number of R combinations RC (n, R) from S, and output AI (I = 1, 2... r ).

Solution:

1. Each polynomial is represented by a linked list. The coefficients and power of polynomials are represented by the nodes in the linked list.

2. polynomial multiplication is the traversal and calculation of each node in the linked list.

The programming implementation is as follows:

# Include <stdio. h>
# Include <stdlib. h>

Typedef int datatype;

Typedef struct Node
{
Datatype COE; // polynomial Coefficient
Datatype power; // polynomial power
Struct node * next;
} Listnode, * linklist;

Linklist linklist_create ()
{
Linklist L;
If (L = (linklist) malloc (sizeof (listnode) = NULL)
{
Printf ("malloc error \ n ");
Return NULL;
}
L-> next = NULL;
Return L;
}
Int linklist_insert (linklist L, datatype X, datatype y)
{
Linklist p, q = L;

While (Q-> next! = NULL)
{
Q = Q-> next;
}
P = (linklist) malloc (sizeof (listnode ));
P-> COE = x; // Coefficient
P-> power = y; // power
P-> next = Q-> next;
Q-> next = P;
Return 0;
}
Int linklist_is_empty (linklist L)
{
Linklist P = L;

If (p-> next = NULL)
Return 1;
Else
Return 0;
}
Int linklist_show (linklist L)
{
Linklist P = L-> next;
While (P! = NULL)
{
Printf ("% d * x <% d>", p-> Coe, p-> power );
P = p-> next;
}
Printf ("\ n ");
Return 0;
}
Int linklist_show_r (linklist L, int K)
{
Linklist P = L-> next;
Int I = 0;
For (I = 0; I <K; I ++)
{
P = p-> next;
}
Printf ("% d \ n", p-> COE );
Return 0;
}
Int linklist_clear (linklist L)
{
Linklist P = L;
While (L-> next)
{
P = L-> next;
L-> next = p-> next;
Free (P );
P = NULL;
}
Return 0;
}
Int linklist_length (linklist L)
{
Linklist P = L-> next;
Int Len = 0;
While (P! = NULL)
{
Len ++;
P = p-> next;
}
Return -- Len;
}
Int multinomial_multipty (linklist L, linklist Mul)
{
Int Len = 0;
Len = linklist_length (l) + linklist_length (Mul );
Int tempcoe [100] = {0}, temppower [100] = {0 };
Linklist P = mul-> next, q = L-> next;
Int temp = 0;
Int I = 0;

I = 0;
While (I <= Len)
{
Tempcoe [I] = 0;
Temppower [I] = I;
I ++;
}
While (P! = NULL) // polynomial Multiplication
{
Q = L-> next;
While (Q! = NULL)
{
Temp = Q-> power + P-> power;
Tempcoe [temp] + = Q-> COE * P-> COE;

Q = Q-> next;
}
P = p-> next;
}
P = q = NULL;
Linklist_clear (L );
For (I = 0; I <= Len; I ++) // re-assign the value to the linked list l
{
Linklist_insert (L, tempcoe [I], temppower [I]);
}
Return 0;
}

# Define n 100

Int main (void)
{
Int T = 0; // Number of Element Types
Int X [N] = {0}; // Element
Int M [N] = {0}; // number of elements
Int I = 0, j = 0;
Int r = 0, n = 0;
Linklist L, Tl;
L = linklist_create ();
TL = linklist_create ();
Linklist_insert (L, 1, 0 );
Printf ("Program Calculation Result \ n ");
Printf ("Enter the number of element types :");
Scanf ("% d", & T );
Printf ("\ n please input each element :");
For (I = 0; I <t; I ++)
{
Scanf ("% d", & X [I]);
}
Printf ("\ n enter the number of corresponding elements :");
For (I = 0; I <t; I ++)
{
Scanf ("% d", & M [I]);
N + = m [I];
}
Printf ("\ n enter R :");
Scanf ("% d", & R );
Printf ("\ N input polynomial: \ n ");
For (I = 0; I <t; I ++)
{
For (j = 0; j <= m [I]; j ++)
{
Linklist_insert (TL, 1, J );
}
Linklist_show (TL );
Multinomial_multipty (L, Tl );
Linklist_clear (TL );
}
Printf ("The Polynomial After \ n multiplication is :");
Linklist_show (L );
Printf ("Combination RC (n, R) = RC (% d, % d) =", N, R );
Linklist_show_r (L, R );
Printf ("\ n ");
System ("Pause ()");
Return 0;
}

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.