The meaning is roughly to find the number of digits and the number of s from the number 1-10^9
Analysis: Using dynamic programming ideas, one-by-one consideration, and the range of s is 1-81
State definition: dp[i][j] = the current sum of all i digits is the number of j
Except the value of the highest bit is 1-9 (the highest bit cannot be 0), the values of the other bits are 0-9, so we can initialize dp[1][j](1 <= j <= 9 ) = 1. If we find the number of dp[5][9] where the sum of all current 5-digit numbers is 9, then we need to consider the case of 10 numbers 0-9,
If the value of the ones digit (ie, the 5th digit) is 6 at this time, then we need to know the value of dp[4][9-6], because the sum is 9, and the ones digit (the fifth digit) is 6 at this time , then the sum of the first 4 numbers must be 3 to satisfy the sum of 9, then dp[5][9] += d[5-1][9-6];
From this, it is easy to get the state transition equation: dp[i][j] = dp[i-1][jk];
Notice! ! ! : 1000000000 cannot be ignored, and the final sum of 1 must add 1;
Code:
#include<bits/stdc++.h> using namespace std; int dp[10][81]; int main() { memset(dp, 0, sizeof(dp)); for(int i = 1; i <= 9; i++) dp[1][i] = 1; for(int i = 2; i < 10; i++) { int c = i*9; for(int j = 1; j <= c; j++) { for(int k = 0; k < j && k <= 9; k++) dp[i][j] += dp[i-1][j-k]; } } for(int i = 2; i <= 9; i++) { for(int j = 1; j <= 81; j++) dp[i][j] += dp[i-1][j]; } dp[9][1]++;//1000000000的情况 int s; while(scanf("%d", &s) == 1) printf("%d\n", dp[9][s]); return 0; }