UVALive 4877 Non-Decreasing Digits 数位DP

 4877 Non-Decreasing Digits

A number is said to be made up ofnon-decreasing digitsif all the digits to theleftof any digit is lessthan or equal to that digit. For example, the four-digit number 1234 is composed of digits that arenon-decreasing. Some other four-digit numbers that are composed of non-decreasingdigits are 0011, 1111,1112, 1122, 2223. As it turns out, there are exactly 715 four-digit numbers composed of non-decreasingdigits.Notice that leading zeroes are required: 0000, 0001, 0002 are all valid four-digit numbers withnon-decreasingdigits.For this problem, you will write a program that determines how many such numbers there are witha specified number of digits.

Input

The first line of input contains a single integerP(1P1000), which is the number of data sets thatfollow. Each data set is a single line that contains the data set number, followed by a space, followedby a decimal integer giving the number of digitsN(1N64).

Output

For each data set there is one line of output. It contains the data set number followed by a single space,followed by the number of N digit values that are composed entirely ofnon-decreasingdigits.

Sample Input
3
1 2
2 3
3 4

Sample Output
1 55
2 220
3 715

题目意思:求n位非递减数列的个数,可以有相同的数。

解题思路:数位DP,dp[i][j]表示i位,最高位是j的符合题意的个数。

之前博客参考 :https://www.cnblogs.com/wkfvawl/p/9438921.html

#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long int
using namespace std;
ll dp[66][11];
void DPlist()
{
   ll i,j,k;
    memset(dp,0,sizeof(0));
    for(i=0;i<=9;i++)
    {
        dp[1][i]=1;
    }
    for(i=1;i<=65;i++)///dp[i][j]表示i位,最高位是j的符合题意的个数
    {
        for(j=0;j<=9;j++)
        {
            for(k=0;k<=j;k++)///把上一个位数小于等于j的dp全加起来
            {
                dp[i+1][j]+=dp[i][k];
            }
        }
        dp[i][10]=0;///用来存总和
        for(j=0;j<=9;j++)
        {
            dp[i][10]+=dp[i][j];
        }
    }
}
int main()
{
    ll t,e,n,i;
    scanf("%d",&t);
    DPlist();
    while(t--)
    {
        scanf("%lld%lld",&e,&n);
        printf("%lld %lld\n",e,dp[n][10]);
    }
}

 

posted @ 2019-04-01 20:26  王陸  阅读(304)  评论(0编辑  收藏  举报