Two versions and N implementation methods for finding the number of solutions for an indefinite equation of N yuan

Source: Internet
Author: User

Version 1:

Equation A1 * X1 + A2 * X2 +... an * xn = N, given n (1000 => N> = 1) coefficients AI (1000> = AI> = 0) and N (1000> = n> 0 ), calculate the non-negative integer solution (x1, x2... XN. (Result Modulo for 10007)

 

Implementation 1:

Constructor, (1 + x ^ 1 + x ^ 2 + .. x ^ (N/A1) ^ A1 * (1 + x ^ 1 + x ^ 2 + .. x ^ (N/A2) ^ a2... (1 + x ^ 1 + .. x ^ (N/AN) ^

^ AI indicates multiplying the item index retrieved from the first item by AI;

After this function is expanded, the coefficient before x ^ n is the number of solutions of the equation.

Time complexity O (N ^ 2 * n );

 

Implementation 2:

DP, F [N] + = f [n-A [I] (1 <= I <= N)

Time complexity O (N * n)

 

 

 

Version 2:

Equation X1 + X2 + ...... + Xn = M: The number of solutions that meet xi> = 1.

The input consists of multiple groups of data. Enter N and M (1 <=n <= 300, n <= m <= 100000) for each data group ).

Because the number of solutions of the equation exceeds the Integer Range, the output result is more than 10007.

 

Implementation 1:

Because it is a special case of the above example, make a [I] All = 1;

The preceding example shows that the DP time complexity is O (M * n), which is acceptable;

The time complexity of the primary function is O (M ^ 2 * n), which is too slow;

 

Implementation 2:

Using the partition method in composite mathematics, the result C (N-1 m-1) is obtained directly );

There are two solutions for calculating C (n-1, s-1:

1. C (n, m) = C (m-1) + C (N-1 m-1 );

It can be recursive, but M consumes a lot of memory, but it can be optimized using a rolling array, time complexity O (M * n );

2. C (n, m) = M * (S-1) * .. (m-n + 1)/n!

Shader A [1... n] = 1. N; A [M-n + 1. m] = m-n + 1. m;

You can first approximately the N numbers from a [1] to a [n], and the maximum public approx. Of the N numbers from a [M-n + 1] to a [m], because C (n, m) is an integer

The number of N numbers from a [1] to a [n] must be completely reduced, and the number of N numbers will change to 1 at the end, finally, we can take the N numbers from a [M-n + 1] to a [m] for multiplication.

Modulo removal;

Time complexity O (N ^ 2), irrelevant to M, optimal !!

The code for this method is attached:

# Include <iostream> <br/> using namespace STD; <br/> int n, m, I, j, a [100005]; <br/> int gcd (int A, int B) <br/>{< br/> if (a % B = 0) return B; <br/> else return gcd (B, A % B); <br/>}< br/> int main () <br/>{< br/> int ans, t, K; <br/> while (CIN> N> m) <br/> {<br/>/C (N-1 m-1) <br/> ans = 1; <br/> for (I = m-n + 1; I <= m-1; I ++) <br/> A [I] = I; <br/> for (I = 1; I <= n-1; I ++) <br/> {<br/> T = I; <br/> for (j = m-n + 1; j <= m-1; j ++) <br/>{< br/> If (t = 1) break; <br/> K = gcd (A [J], t ); <br/> A [J] = A [J]/K; <br/> T = T/K; <br/>}< br/> for (I = m-n + 1; I <= m-1; I ++) <br/> ans = ans * A [I] % 10007; <br/> cout <ans <Endl; <br/>}< br/>

 

 

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.