当前位置:网站首页>2039: [蓝桥杯2022初赛] 李白打酒加强版 (动态规划)
2039: [蓝桥杯2022初赛] 李白打酒加强版 (动态规划)
2022-07-06 23:08:00 【疯疯癫癫才自由】
题目描述
话说大诗人李白,一生好饮。幸好他从不开车。
一天,他提着酒壶,从家里出来,酒壶中有酒 2 斗。他边走边唱:
无事街上走,提壶去打酒。
逢店加一倍,遇花喝一斗。
这一路上,他一共遇到店 N 次,遇到花 M 次。
已知最后一次遇到的是花,他正好把酒喝光了。
请你计算李白这一路遇到店和花的顺序,有多少种不同的可能?
注意:壶里没酒( 0 斗) 时遇店是合法的,加倍后还是没酒;但是没酒时遇花是不合法的。输入格式
输入包含多组测试数据。
第一行为T,表示存在T组测试数据,T不超过30。
对于每组测试数据,输入两个整数N 和M.
1 ≤ N, M ≤ 100。输出格式
输出一个整数表示答案。由于答案可能很大,输出模1000000007 的结果。
输入样例 复制
1 5 10
输出样例 复制
14
最开始是没写出来的,想的是深度优先搜索,也就是第二个代码,不能AC,后面看别人的推荐,才知道闫学灿(y总)(acwing网站的创始人,开了很多课,我相信有必要去看一看,慢慢的把数据结构学了,也得准备CCF的CSP认证了),看了他讲的蓝桥杯初赛,才搞懂了,从侧面也能看出,我很弱,emm,确实,革命尚未成功,我得继续努力。
分析:
状态设计:dp[i][j][k]的值表示遇到i家店,j朵花,酒壶中还剩k斗酒的可能情况数;
状态转移方程:dp[i][j][k]=dp[i-1][j][k/2](i>1&&k%2==0) + dp[i][j-1][k+1](j>1);
边界设计:除了dp[0][0][2]=1,其他元素全为0;
他一共遇到店 N 次,遇到花 M 次。已知最后一次遇到的是花, 他正好把酒喝光了;所以
最后一次肯定遇到的是花,那么最后的结果便是dp[N][M-1][1];
并且酒壶中酒的容量不能超过M;
/**
再编码:
状态设计:dp[i][j][k]的值表示遇到i家店,j朵花,酒壶中还剩k斗酒的可能情况数;
状态转移方程:dp[i][j][k]=dp[i-1][j][k/2](i>1&&k%2==0) + dp[i][j-1][k+1](j>1);
边界设计:除了dp[0][0][2]=1,其他元素全为0;
他一共遇到店 N 次,遇到花 M 次。已知最后一次遇到的是花, 他正好把酒喝光了;所以
最后一次肯定遇到的是花,那么最后的结果便是dp[N][M-1][1];
并且酒壶中酒的容量不能超过M;
*/
#include <cstdio>
#include <cstring>
using namespace std;
const int mod=1000000007;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
int dp[n+1][m+1][m+1];
memset(**dp,0,sizeof(dp));
dp[0][0][2]=1;
for(int i=0;i<=n;++i)
for(int j=0;j<=m;++j)
for(int k=0;k<=m;++k)
{
int &val=dp[i][j][k];
if(i>=1&&k%2==0)
val=(val+dp[i-1][j][k/2])%mod;
if(j>=1)
val=(val+dp[i][j-1][k+1])%mod;
}
printf("%d\n",dp[n][m-1][1]);
}
return 0;
}
DFS解决
只能AC 13%,不得不说,对于一个问题,不能找到正确的算法策略与正确的算法策略
比较,可谓是天壤之别;
/**
DFS解决
只能AC 13%,不得不说,对于一个问题,不能找到正确的算法策略与正确的算法策略
比较,可谓是天壤之别;
*/
#include <iostream>
#include <cstdio>
using namespace std;
const int mod=1000000007;
void drink_DFS(int store,int flow,int drink);
int sum=0,N,M;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
sum=0;
scanf("%d%d",&N,&M);
drink_DFS(0,0,2);
printf("%d\n",sum);
}
return 0;
}
void drink_DFS(int store,int flow,int drink)
{
if(drink < 0) return;
if(store > N || flow >= M) return;
if(drink>M) return;
if(store==N&&flow==M-1&&drink==1)
{
sum+=1;
sum%=mod;
return;
}
drink_DFS(store+1,flow,2*drink);
drink_DFS(store,flow+1,drink-1);
// store+=1;
// drink*=2;
// drink_DFS(store,flow,drink);
// store-=1;
// drink/=2;
// flow+=1;
// drink-=1;
// drink_DFS(store,flow,drink);
// flow-=1;
// drink+=1;
}
边栏推荐
- R descriptive statistics and hypothesis testing
- JS 的 try catch finally 中 return 的执行顺序
- STM32 encapsulates the one key configuration function of esp8266: realize the switching between AP mode and sta mode, and the creation of server and client
- 【愚公系列】2022年7月 Go教学课程 005-变量
- 《五》表格
- Markdown编辑器
- 谈谈讲清楚这件事的重要性
- 【Android Kotlin协程】利用CoroutineContext实现网络请求失败后重试逻辑
- 动态生成表格
- Pointer and array are input in function to realize reverse order output
猜你喜欢
AttributeError: module ‘torch._ C‘ has no attribute ‘_ cuda_ setDevice‘
sublime使用技巧
【愚公系列】2022年7月 Go教学课程 005-变量
Section 1: (3) logic chip process substrate selection
When knative meets webassembly
基于Bevy游戏引擎和FPGA的双人游戏
Read of shell internal value command
Mysql database (basic)
Ansible概述和模块解释(你刚走过了今天,而扑面而来的却是昨天)
Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
随机推荐
R descriptive statistics and hypothesis testing
Factor analysis r practice (with R installation tutorial and code)
当 Knative 遇见 WebAssembly
Leetcode minimum difference in student scores
想要选择一些部门优先使用 OKR, 应该如何选择试点部门?
史上最全学习率调整策略lr_scheduler
01 machine learning related regulations
Meow, come, come: do you really know if, if else
Vscode automatically adds a semicolon and jumps to the next line
Time complexity & space complexity
5G VoNR+之IMS Data Channel概念
高数中值定理总结
STM32F103ZE+SHT30检测环境温度与湿度(IIC模拟时序)
STM32F103实现IAP在线升级应用程序
Ansible报错:“msg“: “Invalid/incorrect password: Permission denied, please try again.“
带你遨游银河系的 10 种分布式数据库
Leetcode longest public prefix
U++ game learning notes
The execution order of return in JS' try catch finally
01机器学习相关规定