UVa 10943 (Math recursion) How does you add?

Source: Internet
Author: User

Add the non-negative integers of k not more than N, so that they are N and a total of how many methods.

Set D (I, j) to indicate the sum of J of a nonnegative integer not exceeding I is the number of methods for I.

D (i, j) = sum{D (k, j-1) | 0≤k≤i}, can be understood as the sum of the previous j-1 number is i-k, the last number is K

There is also a faster recursive approach that transforms the problem into a number of methods that put n balls into K-boxes, which can be empty.

It is equivalent to the number of nonnegative integer solutions of x1 + x2 +...+ xK = N, according to the knowledge of combinatorial mathematics it is easy to calculate the result as C (N+k-1, K-1).

So it can also be recursive: D (i, j) = d (I-1, J) + D (i, j-1)

1#include <cstdio>2 3 Const intM =1000000;4 Const intMAXN = -;5 intD[MAXN +Ten][MAXN +Ten];6 7 intMain ()8 {9      for(inti =1; I <= MAXN; i++)Tend[0][i] =1; One      for(inti =1; I <= MAXN; i++) A          for(intj =1; J <= Maxn; J + +) -D[I][J] = (d[i-1][J] + d[i][j-1]) %M; -  the     intN, K; -      while(SCANF ("%d%d", &n, &k) = =2&& n && k) printf ("%d\n", D[n][k]); -  -     return 0; +}
code June

UVa 10943 (Math recursion) How does you add?

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.