UVA 10943 How does you add?

Source: Internet
Author: User

Original question:
Larry is very bad at Math-he usually uses a calculator, which
worked well throughout college. Unforunately, he is now struck in
A deserted and his good buddy Ryan after a snowboarding
Accident.
They ' re now trying to spend some time figuring out some good
Problems, and Ryan would eat Larry if he cannot answer, so he fate
is up to you!
It's a very simple problem-given a number N, how many ways
Can K numbers less than n add up to n?
For example, for N = 2 and K = +, there is ways:
0+20
1+19
2+18
3+17
4+16
5+15
...
18+2
19+1
20+0
Input
Each line would contain a pair of numbers n and K. N and K would both is an integer from 1 to 100,
Inclusive. The input would terminate on 2 0 ' s.
Output
Since Larry is only interested in the last few digits of the answer, for each pair of numbers N and K,
Print a single number mod 1,000,000 to a single line.
Sample Input
20 2
20 2
0 0
Sample Output
21st
21st
English:
Give you two numbers, N and K, and ask you how many ways to make up n with the number of K, in which the number of components is in order. For example, when n=20,k=2
0+20
1+19
2+18
3+17
4+16
5+15
...
18+2
19+1
20+0
There are 21 kinds

#include <bits/stdc++.h>
using namespace std;
int dp[101][101];
const int mod=1000000;
int main ()
{
    Ios::sync_with_stdio (false);
    int n,k;
    Memset (Dp,0,sizeof (DP));
    for (int i=1;i<=100;i++)
        dp[0][i]=dp[i][1]=1;
    for (int i=1;i<=100;i++) for
        (int j=1;j<=100;j++)
            dp[i][j]= (dp[i-1][j]%mod+dp[i][j-1]%mod)%mod;
    while (cin>>n>>k,n+k)
        cout<<dp[n][k]<<endl;
    return 0;
}

Answer:
A simple combination of numbers or dynamic programming is a topic that is pushed together.
Set state Dp[n][k] means useful K number composition n How many ways, then the formula is
DP[N][K]=DP[N-1][K]+DP[N][K-1]
For example, n=5,k=2 can be added by n=4,k=2 plus n=5,k=1 and get,
N=4,k=2 when the
0 4
1 3
2 2
3 1
4 0
Then the number on the right column, plus 1, gets 5.
But there is a lack of a
5 0
Plus, the equivalent of Dp[5][1] adds another 0

You can also consider the idea of putting the ball inside the box.

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.