NYOJ VF(デジタルdp)


説明
Vasya is the beginning mathematician. He decided to make an important contribution to the science and to become famous all over the world. But how can he do that if the most interesting facts such as Pythagor’s theorem are already proved? Correct! He is to think out something his own, original. So he thought out the Theory of Vasya’s Functions. Vasya’s Functions (VF) are rather simple: the value of the Nth VF in the point S is an amount of integers from 1 to N that have the sum of digits S. You seem to be great programmers, so Vasya gave you a task to find the milliard VF value (i.e. the VF with N = 109) because Vasya himself won’t cope with the task. Can you solve the problem?
入力
There are multiple test cases.
Integer S (1 ≤ S ≤ 81).
しゅつりょく
The milliard VF value in the point S.
サンプル入力
1
サンプル出力
10
この問題の数ビットdp,dp[i][j]は、この数字がiビットであり、各ビット上の数字の和がjであることを示す.
dp[i][j]=dp[i-1][j-k]+dp[i][j](k>=0&&k<=9)
しかし、いくつかの詳細は少し処理します.
(1)iは最大9まで列挙されていますが、10^9があるので、この数は実は10桁あるのですが、10桁のはちょうどこの数しかないので、特例処理するだけなので、s=1を入力すると単独で出力されます
(2)i=1は単独で判断するが,この数がトップであり,0は許されない.だから1~9
ACコード:
# include <cstdio>
# include <cstring>
using namespace std;
int dp[20][90];
int main(){
	int s, i, j, k, ans;
	memset(dp, 0, sizeof(dp));
	for(j=1; j<=9; j++){
		dp[1][j]=1;
	}
	for(i=2; i<=9; i++){
		for(j=1; j<=9*i; j++){
			for(k=0; k<=9; k++){
				if(j>=k)
				dp[i][j]=dp[i-1][j-k]+dp[i][j];
			}
		}
	}
	while(scanf("%d", &s)!=EOF){
		if(s==1){
			printf("10
"); continue; } int ans=0; for(int i=1; i<10; i++){ ans=ans+dp[i][s]; } printf("%d
", ans); } return 0; }