Sicily 1011.Lenny's Lucky Lotto
来源:互联网 发布:神州科捷工作知乎 编辑:程序博客网 时间:2024/05/20 17:40
题目链接在此
一开始用搜索,发现超时(不要嘲笑我)。
还是把代码po一下好了:
#include<stdio.h>void dfs(int num, int path, int length, int limit, int& total) {if (path == length) {total++;return;}for (int i = num * 2; i <= limit; i++) {dfs(i, path + 1, length, limit, total);}}int main() {int caseNum;int length, limit;scanf("%d", &caseNum);for (int t = 1; t <= caseNum; t++) {scanf("%d%d", &length, &limit);int total = 0;for (int i = 1; i <= limit; i++){dfs(i, 1, length, limit, total);}printf("Case %d: n = %d, m = %d, # lists = %d\n", t, length, limit, total);}return 0;}
还得用动态规划。
参考了这位大神的思路。
(1)令dp[i][j] 表示长度为i,恰好以j为结尾的序列的可能数;
(2)设置初始状态: dp[1][j] = 1(1<=j<=M): 表示长度为1,恰好以j为结尾的序列数为1。
(3)列出状态转移方程:对2<=i<=N, dp[i][j] = ∑dp[i-1][k](1<=k<=j/2 ):表示长度为i,恰好以j结尾的序列的总数等于长度为i-1,以小于等于j/2结尾的序列的个数之和。
(4)最终答案= ∑ dp[N][m],(1<=m<=M)
另外要注意数据规模问题,用long long
代码:
#include<stdio.h>int main() {int caseNum;int length, limit;scanf("%d", &caseNum);for (int t = 1; t <= caseNum; t++) {scanf("%d%d", &length, &limit);long long dp[11][2001] = {0};long long total = 0;for (int n = 1; n <= length; n++) {for (int m = 1; m <= limit; m++){if (n == 1)dp[n][m] = 1;else {for (int k = 1; k <= m / 2; k++) {dp[n][m] += dp[n - 1][k];}}}}for (int m = 1; m <= limit; m++)total += dp[length][m];printf("Case %d: n = %d, m = %d, # lists = %lld\n", t, length, limit, total);}return 0;}
最后,这位大神还提到了如何做优化。
0 0
- [sicily online]1011. Lenny's Lucky Lotto
- sicily 1011. Lenny's Lucky Lotto
- Sicily 1011. Lenny's Lucky Lotto
- Sicily 1011.Lenny's Lucky Lotto
- Sicily 1011. Lenny's Lucky Lotto
- sicily 1011. Lenny's Lucky Lotto
- sicily 1011. Lenny's Lucky Lotto
- Sicily 1011. Lenny's Lucky Lotto
- Sicily 1011 Lenny's Lucky Lotto
- 1011. Lenny's Lucky Lotto
- Scily 1011. Lenny's Lucky Lotto
- Scily 1011. Lenny's Lucky Lotto
- pku2193 Lenny's Lucky Lotto Lists
- poj 2193 Lenny's Lucky Lotto Lists
- zoj 2402 - Lenny's Lucky Lotto Lists
- POJ 2193 Lenny's Lucky Lotto Lists
- zoj 2402 Lenny's Lucky Lotto Lists
- ZOJ 2402 Lenny's Lucky Lotto Lists(简单DP)
- UML全面总结
- win8改win7教程写下来留作记念
- 三员之三权分立BMB20-2007
- 《数据结构》实验二: 线性表实验
- hadoop(七) - hadoop集群环境搭建
- Sicily 1011.Lenny's Lucky Lotto
- C++中const用法总结
- 一个简单的模拟用户名是否存在的基于契约优先WSDL的webservice开发实例
- [统计学习方法]K近邻法
- 《数据结构》实验三: 栈和队列实验
- 安卓编译工具
- MDK和IAR环境仿真时,当全速运行的时候如何查看全局变量的值
- Centos java版本变更
- HDU 1429