NYOJ 269 VF

来源:互联网 发布:qq网名生成器软件 编辑:程序博客网 时间:2024/05/24 07:13

VF

时间限制:1000 ms  |  内存限制:65535 KB
难度:2
描述
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
思路:题目意思就是求1~1000000000之间数字之和等于s
状态方程 :d[i]][j]=d[i][j]+d[i-1][j-k] (0<=k<=j&&k<=9)
d[i][j]表示i位的数字之和等于j
推导:(数字之和等于j 的个数) =(i位数字之和等于j的个数) + (i-1位数字之和j-k的个数)
 #include<iostream>using namespace std;int d[10][82];int main(){    int i,j,s,k;    for(i=1;i<10;i++)        d[1][i]=1;    for(i=1;i<=9;i++)        for(j=1;j<=9*i;j++)           for(k=0;k<=9&&k<=j;k++)             d[i][j]=d[i][j]+d[i-1][j-k];    while(cin>>s)    {        int ans=0;        if(s==1)            cout<<10<<endl;        else          {           for(i=1;i<10;i++)             ans+=d[i][s];                cout<<ans<<endl;          }    }    return 0;}
同学的代码:
递归超时,超时不可怕,我们可以打表!
#include<stdio.h>#include<string.h>#define M 1000000000__int64 num[85];void fun(__int64 a,__int64 sum,__int64 m){if(sum>M)return;__int64 i;num[m]++;for(i=0;i<=9;i++){fun(i,sum*10+i,m+i);}}int main(){__int64 n,i;memset(num,0,sizeof(num));for(i=1;i<=9;i++){fun(i,i,i);}while(~scanf("%I64dd",&n)){printf("%I64d\n",num[n]);}return 0;}

打表代码:
#include<stdio.h>int f[82]={1,10,45,165,495,1287,3003,6435,12870,24310,43749,75501,125565,202005,315315,478731,708444,1023660,1446445,2001285,2714319,3612231,4720815,6063255,7658190,9517662,11645073,14033305,16663185,19502505,22505751,25614639,28759500,31861500,34835625,37594305,40051495,42126975,43750575,44865975,45433800,45433800,44865975,43750575,42126975,40051495,37594305,34835625,31861500,28759500,25614639,22505751,19502505,16663185,14033305,11645073,9517662,7658190,6063255,4720815,3612231,2714319,2001285,1446445,1023660,708444,478731,315315,202005,125565,75501,43749,24310,12870,6435,3003,1287,495,165,45,9,1};int main(){int n;while(~scanf("%d",&n)){printf("%d\n",f[n]);}return 0;}


0 0