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. 【SQL】- 基础知识梳理(七) - 索引

    索引的概念 在关系型数据库中,索引是对数据库表中一列或多列的值进行排序的一种结构. SQL SERVER中有索引的类型:按存储结构区分:“聚集索引(又称聚类索引,簇集索引)”,“分聚集索引(非聚类索引 ...

  2. IdentityServer4实现单点登录统一认证

    什么是单点登录统一认证:假如某公司旗下有10个网站(比如各种管理网站:人事系统啊,财务系统啊,业绩系统啊等),我是该公司一管理员或者用户,按照传统网站模式是这样:我打开A网站 输入账号密码 然后进入到 ...

  3. SpringMVC+Hibernate 项目开发之三 (创建SpringMVC项目)

    引用(很全面了):http://blog.csdn.net/dhx20022889/article/details/38041039 我只想说默认创建的项目使用的Spring版本可能不是你想要的,可以 ...

  4. C# LINQ(4)

    where作为LINQ的条件关键字. where的右面是表达式 表达式可以方法返回值,但是where的右面最终是一个可检测真假的表达式 代码: static void Main(string[] ar ...

  5. 什么时候调用dealloc

    什么时候回调用dealloc? 1.这个类被release的时候会被调用: 2.这个对象的retain count为0的时候会被调用: 3.或者说一个对象或者类被置为nil的时候:

  6. EL表达式的语法与应用

    EL(是Expression Language的缩写),使用EL对JSP输出进行优化,可以使得页面结构更加清晰,代码可读性高,也更加便于维护. EL表达式的语法: 语法:$(EL 表达式) $  和 ...

  7. ant实例

    <?xml version="1.0" encoding="UTF-8" ?> <project name="javaTest&qu ...

  8. linux的发行版

    Linux的不同版本以及应用领域 1.Linux内核及发行版介绍 <1>Linux内核版本 内核(kernel)是系统的心脏,是运行程序和管理像磁盘和打印机等硬件设备的核心程序,它提供了一 ...

  9. 微信小程序之页面之间传递值

    页面之间传值有三种方式 1.url传值 2.本地存储传值 3.全局变量传值 1.url传值: 通过url传值的需要通过option来获取参数值. 更多详情可以访问小程序-navigateTo章节. A ...

  10. A printf format reference page (cheat sheet)

    Summary: This page is a printf formatting cheat sheet. I originally created this cheat sheet for my ...