Ural_1353. Milliard Vasya's Function(DP)

  /*题意:求【1 to 10^9】范围内各位数字之和为s的数的个数;
  思路:定义dp[i][j] (i = 1 to 9, j = 1 to 81),表示位数为i的数各位数之和为j的数的个
数。dp[i][j] = (i - 1位数最低位全部补0) + (i - 1位数最高位补j - k {k| 1 <= k <= 9} )。所以转
移方程就是
  dp[i][j] = dp[i-1][j] + sum(dp[i-1][j - 1] , dp[i-1][j-2] , ... , dp[i-1][j-9]);

ps:注意s = 1 的时候是10而不是9,因为10^9也算在s = 1里边

My Code:
*/

#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

int dp[15][85];

int main(){
//fstream cin("data.in");

int i, j, k;
memset(dp, 0, sizeof(dp));
for(i = 1; i <= 9; i++){
dp[1][i] = 1;
}
for(i = 2; i < 10; i++){
for(j = 1; j <= 81; j++){
dp[i][j] = dp[i-1][j];
for(k = 1; k <= 9 && j - k >= 0; k++){
dp[i][j] += dp[i-1][j-k];
}
}
}
int s, ans;
while(cin >> s){
if(s == 1) {cout << "10\n"; continue;}
for(ans = 0, i = 1; i < 10; i++){
ans += dp[i][s];
}
cout << ans << endl;
}
return 0;
}
posted @ 2011-10-09 10:00  AC_Von  阅读(483)  评论(0编辑  收藏  举报