码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-05-20 09:57:25      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:dp

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;
}

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

标签:dp

原文地址:http://blog.csdn.net/guard_mine/article/details/45850247

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!