NYOJ VF(数位dp)
来源:互联网 发布:什么是网站域名 编辑:程序博客网 时间:2024/06/05 23:03
描述
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
样例输出
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
这一题数位dp,dp[i][j]表示这个数字有i位,以及各个位上数字的和为j。
则有dp[i][j]=dp[i-1][j-k]+dp[i][j](k>=0&&k<=9)
但是有一些两个细节要稍微处理一下:
(1)i最大就只枚举到9,因为虽然有10^9,这个数其实是有10位的,但是十位的刚好就只有这个数,所以单做特例处理一下,所以输入s==1时单独输出
(2)对i==1是单独判断一下,因为这个数是首位,是不允许为0的。所以就是1~9
AC代码:
# include <cstdio># include <cstring>using namespace std;int dp[20][90];int main(){int s, i, j, k, ans;memset(dp, 0, sizeof(dp));for(j=1; j<=9; j++){dp[1][j]=1;}for(i=2; i<=9; i++){for(j=1; j<=9*i; j++){for(k=0; k<=9; k++){if(j>=k)dp[i][j]=dp[i-1][j-k]+dp[i][j];}}}while(scanf("%d", &s)!=EOF){if(s==1){printf("10\n");continue;}int ans=0;for(int i=1; i<10; i++){ans=ans+dp[i][s];}printf("%d\n", ans);}return 0;}
0 0
- NYOJ VF(数位dp)
- NYOJ-269-VF(DP不好理解)
- NYOJ VF
- NYOJ VF
- nyoj VF 269 (DP)求数字和的个数
- NYOJ 269 VF
- NYOJ 269 VF
- NYOJ 269 VF
- NYOJ-269 VF
- NYOJ 269--VF
- nyoj-269 VF
- NYOJ 269 VF
- nyoj 269 VF
- NYOJ 269 VF
- (NYoj 269) VF --经典DP问题--下一个状态为前多个状态之和
- NYOJ 236 VF (动态规划)
- nyoj 269 VF 动规
- 01串&&VF(nyoj 252 && nyoj 269)
- 九度OJ - 1184 - 二叉树遍历
- 6.3外存分配方式
- Android Studio报错@integer/google_play_services_version').
- awk getline命令解析
- PL/SQL Developer中SQL语句如何格式化
- NYOJ VF(数位dp)
- Red Hat Enterprise Linux 7 系统安装
- 《15个提高Google搜索的技巧》
- c++作业5
- Ext.Panel 面板控件、 Ext.window.Window 窗口控件、 Ext.container.Viewport 布局控件
- 6.4目录管理
- 配置TCP/IP网络
- 04_Eclipse JSP/Servlet 环境搭建
- 03:矩形分割 来源OJ