题意:= =中文题

思路一:比赛时队友想的。。。然后我赛后想了一下想了个2维dp,但是在转移的时候,貌似出了点小问题...吧?然后就按照队友的思路又写了一遍。

定义dp[i][j][k],表示第i列,放j个,剩下k个的种类数。其中j<=2, k<=2,j<=2的来源是只往上、下放。然后状态转移就是

dp[i][j][a[i] - j - k] = (dp[i][j][a[i] - j - k] + p[j] * dp[i - 1][k][j]) % mod;

//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
/*
定义dp[i][j][k]表示第i个人放了j个剩下k个的种类数 */
const LL mod = ;
const int maxn = 1e4 + ;
LL dp[maxn][][];
char ch[maxn];
int a[maxn];
int n;
LL p[] = {, , }; LL solve(){
memset(dp, , sizeof(dp));
for (int i = ; i <= ; i++) {
if (a[] - i > ) continue;
if (a[] - i < ) break;
dp[][i][a[] - i] = p[i];
}
for (int i = ; i <= n; i++){
for (int j = ; j <= ; j++){
for (int k = ; k <= ; k++){
if (a[i] - j - k > ) continue;
if (a[i] - j - k < ) break;///目前放入j个,dp[i-1]剩下j个
dp[i][j][a[i] - j - k] = (dp[i][j][a[i] - j - k] + p[j] * dp[i - ][k][j]) % mod;
}
}
}
LL ans = ;
for (int i = ; i <= ; i++){
if (i > a[n]) break;
ans = (ans + dp[n][i][]) % mod;
}
return ans;
} int main(){
int t; scanf("%d", &t);
while (t--){
scanf("%s", ch);
n = strlen(ch);
bool flag = true;
for (int i = ; ch[i] != '\0'; i++){
a[i + ] = ch[i] - '';
if ((i == || i == n - ) && ch[i] > ''){
flag = false; break;
}
if (ch[i] > '') {
flag = false; break;
}
}
if (!flag) {
printf("0\n"); continue;
}
printf("%I64d\n", solve());
}
return ;
}

思路二:递推

和之前的dp类似,我们定义每次每次都只能往列上放,因此最多只能放两个。然后我们发现,如果第一个位置的地雷数确定了,后面所有的都确定了,那么我们只需要枚举一下,就有答案了

//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = 1e4 + ;
const LL mod = ;
LL dp[maxn];
char ch[maxn];
int a[maxn];
int n;
int p[] = {, , }; LL solve(){
LL ans = ;
for (int i = ; i <= && i <= a[]; i++){
if (a[] - i > ) continue;
dp[] = a[] - i;
for (int j = ; j <= n; j++){
dp[j] = a[j - ] - dp[j - ] - dp[j - ];
}
if (dp[n] + dp[n - ] != a[n]) continue;
LL res = ;
for (int j = ; j <= n; j++){
res = res * p[dp[j]];
if (res > mod) res %= mod;
}
ans = (ans + res) % mod;
}
return ans;
} int main(){
int t; cin >> t;
while (t--){
scanf("%s", ch);
n = strlen(ch);
bool flag = true;
for (int i = ; i < n; i++){
if ((i == || i == n-) && ch[i] > ''){
flag = false; break;
}
if (ch[i] > ''){
flag = false; break;
}
a[i + ] = ch[i] - '';
}
if (!flag) {
printf("0\n"); continue;
}
printf("%I64d\n", solve());
}
return ;
}

HDU 5965 三维dp 或 递推的更多相关文章

  1. 题解报告:hdu 2084 数塔(递推dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2084 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这 ...

  2. [AHOI2009]中国象棋 DP,递推,组合数

    DP,递推,组合数 其实相当于就是一个递推推式子,然后要用到一点组合数的知识 一道很妙的题,因为不能互相攻击,所以任意行列不能有超过两个炮 首先令f[i][j][k]代表前i行,有j列为一个炮,有k列 ...

  3. UVa 926【简单dp,递推】

    UVa 926 题意:给定N*N的街道图和起始点,有些街道不能走,问从起点到终点有多少种走法. 很基础的dp.递推,但是有两个地方需要注意,在标记当前点某个方向不能走时,也要同时标记对应方向上的对应点 ...

  4. hdu 1284 钱币兑换问题 (递推 || DP || 母函数)

    钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  5. HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  6. 【BZOJ4944】【NOI2017】泳池 概率DP 常系数线性递推 特征多项式 多项式取模

    题目大意 有一个\(1001\times n\)的的网格,每个格子有\(q\)的概率是安全的,\(1-q\)的概率是危险的. 定义一个矩形是合法的当且仅当: 这个矩形中每个格子都是安全的 必须紧贴网格 ...

  7. [NOI2017]泳池——概率DP+线性递推

    [NOI2017]泳池 实在没有思路啊~~~ luogu题解 1.差分,转化成至多k的概率减去至多k-1的概率.这样就不用记录“有没有出现k”这个信息了 2.n是1e9,感觉要递推然后利用数列的加速技 ...

  8. HDU 5322 Hope ——NTT 分治 递推

    发现可以推出递推式.(并不会) 然后化简一下,稍有常识的人都能看出这是一个NTT+分治的情况. 然而还有更巧妙的方法,直接化简一下递推就可以了. 太过巧妙,此处不表,建议大家找到那篇博客. 自行抄写 ...

  9. lightoj 1126 - Building Twin Towers(dp,递推)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1126 题解:一道基础的dp就是简单的递推可以设dp[height_left][ ...

随机推荐

  1. 【第三篇】Volley图片加载之NetworkImageView代码分析

    在Volley的使用之加载图片讲过使用NetWorkImageView进行图片加载的例子,本文着重讲解NetWorkImageView内部是如何实现的,以及Volley这个控件有什么特性.   1,通 ...

  2. node读写json文件(进阶)

    该方法可用于修改配置文件,直接上代码 fs.readFile('test1.json','utf8',function (err, data) { if(err) console.log(err); ...

  3. c# 使用 HttpWebRequest模拟登陆

    c# 使用 HttpWebRequest模拟登陆(附带验证码) 分类: C# .net2010-06-04 00:50 35647人阅读 评论(43) 收藏 举报 c#exceptionstreams ...

  4. Sun jdk, Openjdk, Icedtea jdk关系

    转自: http://blog.chinaunix.net/uid-20648944-id-3204527.html Sun jdk与Openjdk版本发展历史如下图所示: 1.    Openjdk ...

  5. <hr/>标签改变颜色注意事项

    1.css改变颜色 <hr style="border:0;background-color:#093;height:1px;">   注意: 如果不加border:0 ...

  6. 自己写deque

    //deque /* what is a deque? In Chinese, it's called "双端队列". It's different from a queue. I ...

  7. shell脚本学习(五)

    1.统计文件的行数.单词数.字符数 1)行数: wc -l file cat file | wc -l 2)单词数 wc -w file cat file | wc -w 3)统计字符数 wc -c ...

  8. [ An Ac a Day ^_^ ] hdu 1662 Trees on the level 数据结构 二叉树

    紫书上的原题 正好学数据结构拿出来做一下 不知道为什么bfs的队列一定要数组模拟…… 还可以练习一下sscanf…… #include<stdio.h> #include<iostr ...

  9. UNIX基础--控制台和终端

    虚拟控制台和终端 Virtual Consoles and Terminals: FreeBSD 虚拟控制台的默认配置为8个,但并不是硬性设置, 您可以很容易设置虚拟控制台的个数增多或减少. 虚拟控制 ...

  10. VS2010编译错误 LNK 2019 unresolved external symbol错误解决办法

    Link错误有很多种,主要是没有在连接中加入lib文件路径,或者lib配置正确,传参错误 一个solution里面多个project之间引用其他project函数会出现这个错误,由于包含了头文件而没处 ...