POJ 3181 Dollar Dayz DP

Source: Internet
Author: User
Tags dayz

Test instructions: give you a N, and K, ask how many combinations of numbers, can make the sum of the numbers of N, the range of these numbers is 1 to K.

Like, give you n=4, k=2. So 1+1+1+1=4, 1+1+2=4,2+2=4, four kinds of combinations.

Idea: A complete backpack, can be set D[I][J] represents the number of combinations from the I number and the sum of J.

Then, you can consider the combination of the number of numbers I and no number I, then no number i combination number is d[i-1][j], the number i is a combination of d[i][j-i] (can be added in these combinations 1 i).

Therefore, the transfer equation can be written as d[i][j] = D[i-1][j] + d[i][j-i] (if j<i, then 0);

After encoding these equations, input 1000 100 (maximum), the result is overflow, obviously this number of combinations is too large.

Then consider experimenting with longlong, and then divide the number of combinations into the first half and the last half of the digital records.

AC Code:

#include <cstdio> #include <iostream> #include <cstring>using namespace std;const int N = 1005;const  Long Long MOD = 1000000000000000000;long long a[105][n], b[105][n];int n,k;void solve () {memset (b, 0, sizeof (b)); memset (A, 0, sizeof (a)); for (int i = 0; I <= k; i++) b[i][0] = 1;for (int i = 1; I <= K; i++) {for (int j = 1; J <= N; j + +) {if ( J >= i) {b[i][j] = (B[i-1][j]+b[i][j-i])%mod;a[i][j] = (A[i-1][j]+a[i][j-i]) + (b[i-1][j]+b[i][j-i])/MOD;} ELSE{B[I][J] = b[i-1][j];a[i][j] = A[i-1][j];}}} if (a[k][n]>0)        Cout<<a[k][n];cout<<b[k][n]<<endl;} int main () {        //freopen ("OUT.txt", "w", stdout), while (~SCANF ("%d%d", &n, &k)) {Solve ()}; return 0;}

  

Scrolling array AC code:

#include <cstdio> #include <iostream> #include <cstring>using namespace std;const int N = 1005;const Long Long MOD = 1000000000000000000;long long a[2][n], b[2][n];int n,k;void solve () {int f = 1;memset (b, 0, sizeof (b)); MEMS ET (A, 0, sizeof (a)), for (int i = 0; I <= 1; i++) b[i][0] = 1;for (int i = 1; I <= K; i++) {for (int j = 1; J <= N; j + +) {if (J >= i) {b[f][j] = (B[!f][j]+b[f][j-i])%mod;a[f][j] = (A[!f][j]+a[f][j-i]) + (b[!f][j]+b[f][j-i])/mod;} ELSE{B[F][J] = b[!f][j];a[f][j] = A[!f][j];}} f =!f;b[f][0] = 1;for (int j = 1; J <= N; j + +) B[f][j] = 0;} if (a[!f][n]>0)        Cout<<a[!f][n];cout<<b[!f][n]<<endl;} int main () {        //freopen ("OUT.txt", "w", stdout), while (~SCANF ("%d%d", &n, &k)) {Solve ()}; return 0;}

  

Procedure 2: Refer to the web, basically the same as the first approach, except that the state transfer equation is a little different.

Equation d[i][j]--The number of integers I, the largest integer in the division is less than J

Thus the classification is discussed, if I, the division of Integer I, the largest integer must be less than or equal to I, so d[i][j] = D[i][i].

If i = j, the division of Integer I, the individual i is a division, so, the division of Integer I can be divided into a separate I and no I division, then d[i][j] = d[i][j-1]+1

If I > J, the division of Integer I can be divided into an integer j or no integer j, thereby d[i][j] = D[i][j-1] + d[i-j][j]

Procedure 2 AC Code:

#include <cstdio> #include <iostream> #include <cstring>using namespace std;const int N = 1005;const  Long Long MOD = 1000000000000000000;long long a[n][105], b[n][105];int n,k;void solve () {memset (b, 0, sizeof (b)); memset (A, 0, sizeof (a)); for (int i = 0; I <= k; i++) B[1][i] = 1;for (int i = 2; I <= n; i++) {for (int j = 1; j <= K; j + +) {if ( I > J) {b[i][j] = (B[i][j-1] + b[i-j][j])%mod;a[i][j] = A[i][j-1] + a[i-j][j]+ (b[i][j-1] + b[i-j][j])/mod;} else if (i = = j) {B[i][j] = (b[i][j-1]+1)%mod;a[i][j] = A[i][j-1] + (b[i][j-1]+1)/mod;//if (B[i][j-1]+1>mod) a[i][j]++;} ELSE{B[I][J] = b[i][i];a[i][j] = A[i][i];}}} if (a[n][k]>0)        Cout<<a[n][k];cout<<b[n][k]<<endl;} int main () {        //freopen ("OUT.txt", "w", stdout), while (~SCANF ("%d%d", &n, &k)) {Solve ()}; return 0;}

  

POJ 3181 Dollar Dayz DP

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.