URAL1353---Milliard Vasya's Function(简单数位dp)

1 篇文章 0 订阅

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

知道数位dp的应该都会做
dp[i][j] i位数,和为j的数个数

/*************************************************************************
    > File Name: URAL1353.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年05月19日 星期二 21时27分23秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

int dp[15][100];
int bit[15];

int dfs(int now, int sum, bool flag) {
    if (now == -1) {
        return sum == 0;
    }
    if (!flag && ~dp[now][sum]) {
        return dp[now][sum];
    }
    int ans = 0;
    int end = flag ? bit[now] : 9;
    for (int i = 0; i <= end; ++i) {
        if (sum >= i) {
            ans += dfs(now - 1, sum - i, flag && i == end);
        }
    }
    if (!flag) {
        dp[now][sum] = ans;
    }
    return ans;
}

int main() {
    memset(dp, -1, sizeof(dp));
    int s;
    int n = 1e9;
    int cnt = 0;
    while (n) {
        bit[cnt++] = n % 10;
        n /= 10;
    }
    while (cin >> s) {
        printf("%d\n", dfs(cnt - 1, s, 1));
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值