Ural 1353 Milliard Vasya'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 <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>

using namespace std;
const int INF=0x3f3f3f3f;
#define LL long long
int dp[10][90];
int main()
{
    int n, i, j, sum, s, k;
    memset(dp,0,sizeof(dp));
    for(i=1;i<=9;i++)
        dp[1][i]=1;
    for(i=2;i<=9;i++)
    {
        s=0;
        for(j=1;j<=81;j++)
        {
            for(k=0;k<j&&k<=9;k++)
            {
                dp[i][j]+=dp[i-1][j-k];
            }
        }
    }
    while(scanf("%d",&n)!=EOF)
    {
        if(n==1)
        {
            puts("10");
            continue ;
        }
        sum=0;
        for(i=1;i<=9;i++)
        {
            sum+=dp[i][n];
        }
        printf("%d\n",sum);
    }
    return 0;
}

Ural 1353 Milliard Vasya's Function(DP)

时间: 2024-07-30 13:50:59

Ural 1353 Milliard Vasya's Function(DP)的相关文章

递推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,

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

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

URAL1353——DP——Milliard Vasya&#39;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?

URAL 1167. Bicolored Horses (DP)

题目链接 题意 :农夫每天都会放马出去,然后晚上把马赶入马厩,于是让马排成一行入马厩,但是不想马走更多的路,所以让前p1匹入第一个马厩,p2匹马入第二个马厩…………但是他不想让他的任何一个马厩空着,所有的马都必须入马厩.有两种颜色的马,如果 i 匹黑马与 j 匹白马同在一个马厩,不愉快系数是 i * j,总系数就是k个系数相加.让总系数最小. 思路 : dp[i][j] 代表的是前 i 个马厩放 j 匹马的最小不愉快系数值. 1 //1167 2 #include <cstdio> 3 #in

Ural 1260 A nudnik photographer(DP)

A nudnik photographer 大意: 对1到N这些数进行排列,1必须要在最左边,相邻的两个数之间的差值不能超过2,问有多少种排列的方法. 思路: 对座位进行DP,当第一个是1,第二个是2的时候,组合为dp[i-1]:当第一个是1,第二个是3的时候,第三个也确定了是2,组合为dp[i-3]:还有最后一种情况是1357--8642. 所以DP方程为dp[i] = dp[i-1]+dp[i-3]+1. #include <stdio.h> int n; int dp[100]; int

URAL 1073 Square Country(DP)

题目链接 题意 :这个人要投资地,每块地都是正方形并且边长都是整数,他希望他要买的地尽量的少碎块.每买一块地要付的钱是边长的平方,而且会得到一个一份证书,给你一个钱数,让你求出能得到的证书个数. 思路 :其实就是求x12+x22+--+Xn2中的最小的n. 1 //1073 2 #include <stdio.h> 3 #include <iostream> 4 #include <math.h> 5 6 using namespace std ; 7 8 int a[