Milliard Vasyas Function(简单数位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秒 ************************************************************************/;const double pi = acos(-1.0);const int inf = 0x3f3f3f3f;const double eps = 1e-15;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;}

,你曾经说,最大的愿望,

Milliard Vasyas Function(简单数位dp)

相关文章:

你感兴趣的文章:

标签云: