标签:
描述
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位的,但是十位的刚好就只有这个数,所以单做特例处理一下,所以输入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\n");
continue;
}
int ans=0;
for(int i=1; i<10; i++){
ans=ans+dp[i][s];
}
printf("%d\n", ans);
}
return 0;
}
NYOJ VF(数位dp)
标签:
原文地址:http://blog.csdn.net/shiwaigaoren12345/article/details/51367314