URAL 1353 Milliard Vasya's Function(数位DP)

来源:互联网 发布:《梦里花落知多少》txt 编辑:程序博客网 时间:2024/06/06 05:09
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 digitsS. 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

inputoutput
1
10
设右区间为1e9直接数位DP即可。
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<bitset>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair<int,int>pil;const int INF = 0x3f3f3f3f;LL dp[10][100];int s;int num[]={0,0,0,0,0,0,0,0,0,0,1};LL dfs(int pos,int sum,int flag){    if(pos==0)        return sum==0;    if(!flag&&dp[pos][sum]!=-1)        return dp[pos][sum];    int ed=flag?num[pos]:9;    LL res=0;    for(int i=0;i<=ed;i++)        res+=dfs(pos-1,sum-i,flag&&i==ed);    if(!flag) dp[pos][sum]=res;    return res;}int main(){    while(~scanf("%d",&s))    {        CLEAR(dp,-1);        LL ans=dfs(10,s,1);        printf("%lld\n",ans);    }    return 0;}/**/


0 0
原创粉丝点击