URAL1353——DP——Milliard Vasya's Function

Description

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 = 10 9) 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

input output
1
10

大意:问你从1到10^9之内所有位数相加为S的数有多少

我们定义dp[i][j]表示一共i位的数的和为j,那么可以得到状态转移方程dp[i][j] = dp[i-1][j] + dp[i-1][j-1] + ...dp[i-1][j-9]当然满足j-9 >= 0 因为考虑的是当前状态与原来状态的改变,把状态看做是一个整体,要插入的只是一个元素,dp[i-1][j]表示往入0,其他的意思就是插入k,考虑当s = 1时,要加1

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[10][85];
int main()
{
    int S;
    memset(dp,0,sizeof(dp));
    for(int i = 1; i <= 9; i++)
        dp[1][i] = 1;
    for(int i = 2; i <= 9 ;i++){
        for(int j = 1; j <= 81; j++) {
            dp[i][j] = dp[i-1][j];
                for(int k = 1; k <= 9 &&  k <= j ;k++){
                    dp[i][j] += dp[i-1][j-k];
                }
        }
    }
    while(~scanf("%d",&S)){
        int ans = 0;
        if(S == 1) {printf("10\n"); continue;}
        for(int i = 1; i <= 9;i++)
            ans += dp[i][S];
        printf("%d\n",ans);
    }
    return 0;
}

  

URAL1353——DP——Milliard Vasya's Function

时间: 2024-10-13 23:12:59

URAL1353——DP——Milliard Vasya's Function的相关文章

Ural 1353 Milliard Vasya&#39;s Function(DP)

题目地址:Ural 1353 定义dp[i][j],表示当前位数为i位时,各位数和为j的个数. 对于第i位数来说,总可以看成在前i-1位后面加上一个0~9,所以状态转移方程就很容易出来了: dp[i][j]=dp[i][j]+dp[i][j-1]+dp[i][j-2]+.......+dp[i][j-9]: 最后统计即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <

递推DP URAL 1353 Milliard Vasya&#39;s Function

题目传送门 1 /* 2 题意:1~1e9的数字里,各个位数数字相加和为s的个数 3 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 4 状态转移方程:dp[i][j] += dp[i-1][j-k],为了不出现负数 5 改为:dp[i][j+k] += dp[i-1][j] 6 */ 7 #include <cstdio> 8 #include <cstring> 9 #include <cmath> 10 #include <algorithm

URAL 1353 Milliard Vasya&#39;s Function DP

题目:click here 分析:dp[i][j] 表示i位数字,当前数字和为j的个数.dp[i][j] = dp[i-1][j] + dp[i-1][j-k] 前面表示在i-1位数后面加零,后面表示在前一位前面加k这个数.(注意k的取值范围). 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int s; 5 int dp[11][83]; 6 int cnt[83]; 7 void pre() { 8 memset( dp, 0,

timus.1353. Milliard Vasya&#39;s Function 动态规划

题目传送门 题目大意: 输入一个1到81(9*9)的数字s,求在1到109有多少个数字的数位和等于s. 解题思路: 我们很容易知道一位数字等于s的个数.要使一个i位的数字数位和等于s,可以通过一个i-1位的数字后面加上0~9(如果s<9就是0~s).于是很容易得出方程 dp[i][j]=dp[i-1][j-0]+dp[i-2][j-1]+……+dp[i-9][j-9];其中i表示i位的数字,j表示s.dp[i][j]表示一个i位数字数位和为j的方案数. #include <iostream&g

ural 1353. Milliard Vasya&#39;s Function

点击打开链接 1353. Milliard Vasya's Function Time limit: 1.0 second Memory limit: 64 MB 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

Ural 1353 Milliard Vasya&amp;#39;s Function(DP)

题目地址:Ural 1353 定义dp[i][j].表示当前位数为i位时,各位数和为j的个数. 对于第i位数来说.总能够看成在前i-1位后面加上一个0~9.所以状态转移方程就非常easy出来了: dp[i][j]=dp[i][j]+dp[i][j-1]+dp[i][j-2]+.......+dp[i][j-9]: 最后统计就可以. 代码例如以下: #include <iostream> #include <cstdio> #include <string> #inclu

URAL1353---Milliard Vasya&#39;s 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

CodeForces - 837E - Vasya&#39;s Function | Educational Codeforces Round 26

/* CodeForces - 837E - Vasya's Function [ 数论 ] | Educational Codeforces Round 26 题意: f(a, 0) = 0; f(a, b) = 1 + f(a, b-gcd(a, b)); 求 f(a, b) , a,b <= 1e12 分析: b 每次减 gcd(a, b) 等价于 b/gcd(a,b) 每次减 1 减到什么时候呢,就是 b/gcd(a,b)-k 后 不与 a 互质 可先将 a 质因数分解,b能除就除,不能

Educational Codeforces Round 26 E - Vasya&#39;s Function

数论题还是好恶心啊. 题目大意:给你两个不超过1e12的数 x,y,定义一个f ( x, y ) 如果y==0 返回 0 否则返回1+ f ( x , y - gcd( x , y ) ); 思路:我们设gcd ( x , y) 为G,那么 设 x  = A*G,y = B*G,我们考虑减去多少个G时x y 的gcd会改变,我们设减去 k个G的时候 x和y 的gcd为改变,即 A*G 和 ( B - k ) * G 的 gcd 改变了,什么情况下会改变呢,就是A 和( B -  k )的gcd