Subject address: Ural 1353
Define DP [I] [J], indicating that when the current number of digits is I, the sum of the numbers is J.
For the I-digit, it can always be seen as adding a 0 ~ after the front I-1 bit ~ 9, so the state transition equation is easy to come out:
DP [I] [J] = DP [I] [J] + dp [I] [J-1] + dp [I] [J-2] + ....... + dp [I] [j-9];
The final statistics are enough.
The Code is as follows:
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <set>#include <algorithm>using namespace std;const int INF=0x3f3f3f3f;#define LL long longint dp[10][90];int main(){ int n, i, j, sum, s, k; memset(dp,0,sizeof(dp)); for(i=1;i<=9;i++) dp[1][i]=1; for(i=2;i<=9;i++) { s=0; for(j=1;j<=81;j++) { for(k=0;k<j&&k<=9;k++) { dp[i][j]+=dp[i-1][j-k]; } } } while(scanf("%d",&n)!=EOF) { if(n==1) { puts("10"); continue ; } sum=0; for(i=1;i<=9;i++) { sum+=dp[i][n]; } printf("%d\n",sum); } return 0;}
Ural 1353 milliard Vasya's function (DP)