Vasya is the beginning mathematician. He decided to make an important contribution to the science an
来源:互联网 发布:人工智能的负面影响 编辑:程序博客网 时间:2024/04/27 21:58
题意:1~1000000000之间,各位数字之和等于给定s的数的个数
每行给出一个数s(1 ≤ s ≤ 81),求出1~10^9内各位数之和与s相等的数的个数。
1、只有s=1时,10^9的系数才能为1,否则就大于10^9;
所以和为1的要单一列出来。
2、如果s!=1:
定义状态dp[i][j]为前i位各位数之和为j的情况数量:对于前i为的数字之和最大为:9*i,即每一位数字都是9.
i=1、只有一位的数字,因为s>=1,所以最低位只能是1-9 其中的一个数字,
每行给出一个数s(1 ≤ s ≤ 81),求出1~10^9内各位数之和与s相等的数的个数。
1、只有s=1时,10^9的系数才能为1,否则就大于10^9;
所以和为1的要单一列出来。
2、如果s!=1:
定义状态dp[i][j]为前i位各位数之和为j的情况数量:对于前i为的数字之和最大为:9*i,即每一位数字都是9.
i=1、只有一位的数字,因为s>=1,所以最低位只能是1-9 其中的一个数字,
i>1、假设第i位放数字k(则k只能是0~9并且k<=s),若要使第前i位数字之和为j,那么前i-1位只能放j-k,由此得出动态转移方程:d[i]][j]=d[i][j]+d[i-1][j-k]
#include<cstdio>
int dp[9][81];
int main(){
int n;
for(int i=1;i<=9;i++)
dp[1][i]=1;
for(int i=1;i<=9;i++)
for(int j=1;j<=9*i;j++)
for(int k=0;k<=9 && k<=j;k++)
dp[i][j]+=dp[i-1][j-k];
while(~scanf("%d",&n)){
if(n==1){
printf("10\n");
continue;
}
int sum=0;
for(int i=1;i<=9;i++)
sum+=dp[i][n];
printf("%d\n",sum);
}
}
总结:关于数字每位数字和的问题。每一位只能是0~9的其中一个数字。最高位不能为零,所以最高位涉及到的数字就要单一列出来,作为特殊的处理。定义状态dp[i][j]为前i位各位数之和为j的情况数量,假设第i位放数字k(则k只能是0~9并且k<=s),若要使第前i位数字之和为j,那么前i-1位只能放j-k,由此得出动态转移方程:d[i]][j]=d[i][j]+d[i-1][j-k] (0<=k<=j&&k<=9)
阅读全文
0 0
- Vasya is the beginning mathematician. He decided to make an important contribution to the science an
- What is the correct way to completely remove an application?
- Alias "bootstrap.widgets.TbHeroUnit" is invalid. Make sure it points to an existing PHP file and the
- What is the best way to create a good interface to an abstraction?
- An Introduction to the JAIN SIP API
- An Introduction to the Extended Kalman Filter
- An Interactive Guide To The Fourier Transform
- An Introduction to the Theory of Computation
- an-introduction-to-the-raphael-js-librar
- An Introduction to the Raphael JS Library
- unable to start the monitor on 4454 ,an other instance is problaly using the same port
- convert the contents of an NSData object to an NSString
- An association from the table * refers to an unmapped class: *
- An association from the table refers to an unmapped class
- An association from the table * refers to an unmapped class: *
- An association from the table refers to an unmapped class
- HOWTO:How to make the WebGrid work like an Excel sheet
- How to make an SDL window the child of another window in Win32
- 前端chrome浏览器调试总结
- Ubuntu16.04 Tensorflow安装
- SpringMVC注解开发(基础)---SpringMVC学习笔记(七)
- HBase 默认配置
- android服务端与客户端
- Vasya is the beginning mathematician. He decided to make an important contribution to the science an
- js实现Div拖拽
- ActiveX控件属性:自定义属性的使用
- java 不规则二维数组的定义与输出 及 Arrays.fill(type[] A , type B)的用法 及 交换两行的方法
- Tomcat8.5源码分析-HostConfig
- HDU 2767 Proving Equivalences(强连通 Tarjan+缩点)
- Windows----搜狗输入法在word中中英文正常转换,在浏览器上只有英文
- AI看啥片
- Linux常用命令集