VF

时间限制:1000 ms  |  内存限制:65535 KB
难度:2
 
描述
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?
 
输入
There are multiple test cases.
Integer S (1 ≤ S ≤ 81).
输出
The milliard VF value in the point S.
样例输入
1
样例输出
10

题目大意:题中给出了N。每次询问给你一个s,问你从1--N中各个位之和为s的这样的数字有多少。

解题思路:定义状态:dp[i][j]表示前i位的和为j的这样的数有多少个。i的范围只需要1-9即可,因为N的值为1e9,即使你要找i为10的数,也只有1e9这个数,所以枚举i到10是无意义的。j的范围为1-i*9,因为i位最大的和就是i位都为9。需要枚举k,k表示在第i位需要加的数字,则状态转移方程为dp[i][j]=dp[i][j]+dp[i-1][j-k]。同时需要特殊处理边界,也是最容易出差错的地方。

#include<bits/stdc++.h>
using namespace std;
const int N=1e9;
int dp[10][100];
int main(){
int s,i,j,k,sum;
for(i=1;i<10;i++) //边界处理
dp[1][i]=1;
for(i=1;i<10;i++){
for(j=1;j<=i*9;j++){
for(k=0;k<10&&j>=k;k++){ //枚举第i位值k
dp[i][j]=dp[i][j]+dp[i-1][j-k];
}
}
}
while(scanf("%d",&s)!=EOF){
if(s==1)
printf("10\n");
else{
sum=0;
for(i=1;i<10;i++)
sum+=dp[i][s];
printf("%d\n",sum);
}
}
return 0;
}

  

nyoj 269——VF——————【dp】的更多相关文章

  1. Kattis - honey【DP】

    Kattis - honey[DP] 题意 有一只蜜蜂,在它的蜂房当中,蜂房是正六边形的,然后它要出去,但是它只能走N步,第N步的时候要回到起点,给出N, 求方案总数 思路 用DP 因为N == 14 ...

  2. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

  3. HDOJ 1501 Zipper 【DP】【DFS+剪枝】

    HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...

  4. HDOJ 1257 最少拦截系统 【DP】

    HDOJ 1257 最少拦截系统 [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...

  5. HDOJ 1159 Common Subsequence【DP】

    HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  6. HDOJ_1087_Super Jumping! Jumping! Jumping! 【DP】

    HDOJ_1087_Super Jumping! Jumping! Jumping! [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  7. POJ_2533 Longest Ordered Subsequence【DP】【最长上升子序列】

    POJ_2533 Longest Ordered Subsequence[DP][最长递增子序列] Longest Ordered Subsequence Time Limit: 2000MS Mem ...

  8. HackerRank - common-child【DP】

    HackerRank - common-child[DP] 题意 给出两串长度相等的字符串,找出他们的最长公共子序列e 思路 字符串版的LCS AC代码 #include <iostream&g ...

  9. LeetCode:零钱兑换【322】【DP】

    LeetCode:零钱兑换[322][DP] 题目描述 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成 ...

随机推荐

  1. 「POJ 2182」 Lost Cows

    题目链接 戳这 题目大意 \(N(2 <= N <= 8,000)\)头奶牛有\(1..N\)范围内的独特品牌.对于每头排队的牛,知道排在那头牛之前的并比那头牛的品牌小的奶牛数目.根据这些 ...

  2. Glib之GObject宏介绍

    G_DEFINE_TYPE定义一个静态类型 /** * G_DEFINE_TYPE(`G_DEFINE_TYPE_WITH_CODE`比`G_DEFINE_TYPE`就是多了一个自定义代码参数_C_) ...

  3. CHNetRequest网络请求

    Paste JSON as Code • quicktype 软件的使用 iOS开发:官方自带的JSON使用 JSON 数据解析 XML 数据解析 Plist 数据解析 NetRequest 网络数据 ...

  4. linux获取域名地址

    dig live-195887137.cn-north-1.elb.amazonaws.com.cn +short

  5. django修改数据库连接

    settings.py DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'domain', 'USER ...

  6. 3月份GitHub上最热门的Java开源项目

    今天,我们来盘点3月份GitHub上最热门的Java项目的时候了,如果你每月都有关注猿妹发布的排行榜,那么本月的Java项目对你来说一定不陌生,这些都是曾经多次出现在榜单中的项目: 1 advance ...

  7. luoguP1401 城市

    https://www.luogu.org/problemnew/show/P1401 二分答案网络流判断是否可行即可 #include <bits/stdc++.h> using nam ...

  8. Win7下C/C++跨平台开发工具IDE的安装之CodeBlocks

    1. Win7下安装CodeBlocks: 下载带有mingw的CodeBlocks:http://www.codeblocks.org/downloads/26#windows 运行所下载程序: 点 ...

  9. UML之用例图详解

    原文链接:https://blog.csdn.net/mj_ww/article/details/53020080 UML,即Unified Model Language,统一建模语言.百度百科对他的 ...

  10. 洛谷 P2234 [HNOI2002]营业额统计

    题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额.分析营业情况是 ...