posts - 2,  comments - 3,  trackbacks - 0

1353. Milliard Vasya's Function

Time Limit: 1.0 second
Memory Limit: 16 MB
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?

Input

Integer S (1 ≤ S ≤ 81).

Output

The milliard VF value in the point S.

Sample

input output
1
10

题意:输入s,输出自然数1...N中各位数字之和等于s的数的个数。
解题思路:
      DP:   F[i][j]表示i位数中,数字之和为j的数的个数,F[i][j] = F[i - 1][k];  (k = j....j-9 && k > 0);
              递推方程由:F[i][j] = F[i - 1][j]个数尾部补0 + F[i - 1][j - k]个数头部补k,(k = 1...9, && j - k > 0),而来。
Code:

#include<cstdio>

int F[10][82];
int count[82];

void init(){
   int i = 0;
   int j = 0;
   int k = 0;
   count[0] = 1;
   for(i = 0; i < 10; i++){
      for(j = 0; j < 82; j++){
         F[i][j] = 0;
      }
   }
   for(i = 0; i <= 9; i++){
      F[1][i] = 1;
   }
   for(i = 2; i < 10; i++){
      for(j = 1; j < 82; j++){
         for(k = j; k > 0 && k >= j - 9; k--){
            F[i][j] += F[i - 1][k];
         }
      }
   }
   count[1] = 10;
   for(j = 2; j < 82; j++){
      count[j] = 0;
      for(i = 1; i < 10; i++){
         count[j] += F[i][j];
      }
   }
}

int main(int argc, char* argv[], char* env[])
{
   int n = 0;
   init();
   while(scanf("%d", &n) != EOF){
      printf("%d\n", count[n]);
   }
   return 0;
}

posted on 2011-07-21 14:11 Lshain 阅读(414) 评论(0)  编辑 收藏 引用 所属分类: Algorithm题解-timus
<2025年6月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

常用链接

留言簿

文章分类(46)

文章档案(33)

ACM

Algorithm Link

BLOG

Format analysis

Forum

Math

mirror

OpenGL

Protocol Analyzer

Recent Contests

Search

WIN32 Programming

最新随笔

搜索

  •  

最新评论

阅读排行榜

评论排行榜