HDU 5456 Matches Puzzle Game(数位DP)
来源:互联网 发布:游戏主机 知乎 编辑:程序博客网 时间:2024/05/18 00:52
题目链接:点击打开链接
题意:给你n个火柴棍, 要求你恰好用完, 来组成一个一个等式, 等式的形式是a - b = c 。求可以组成的等式个数。
思路:很明显的数位DP, 不过巧妙的是, 该题利用了手动模拟大数相加的过程,首先, 我们不妨将等式改成b + c = a, 用d[res][a][b][c] 表示还剩res根火柴, 当前对应位相加之后有没有进位, b和c是否已经停止放火柴棒 的方法数。
细节参见代码:
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<string>#include<vector>#include<stack>#include<bitset>#include<cstdlib>#include<cmath>#include<set>#include<list>#include<deque>#include<map>#include<queue>#define Max(a,b) ((a)>(b)?(a):(b))#define Min(a,b) ((a)<(b)?(a):(b))using namespace std;typedef long long ll;typedef long double ld;const ld eps = 1e-6, PI = 3.1415926535897932384626433832795;const int mod = 1000000000 + 7;const int INF = 0x3f3f3f3f;// & 0x7FFFFFFFconst int seed = 131;const ll INF64 = ll(1e18);const int maxn = 500 + 10;int T,kase = 0,vis[maxn][2][2][2];int table[] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};ll m,n,d[maxn][2][2][2];inline void add(ll& a, ll b) { a += b; if(a >= m) a -= m;}ll dp(int res, int A, int B, int C) { ll& ans = d[res][A][B][C]; if(!B && !C) { if(res == 0 && !A) return 1; else if(A && table[1] == res) return 1; else return 0; } if(vis[res][A][B][C] == kase) return ans; vis[res][A][B][C] = kase; ans = 0; if(B && C) { for(int i=0;i<10;i++) { for(int j=0;j<10;j++) { ll sum = table[i] + table[j] + table[(i + j + A) % 10]; if(sum > res) continue; bool nxt = i + j + A >= 10; add(ans, dp(res - sum, nxt, 1, 1)); if(j) add(ans, dp(res - sum, nxt, 1, 0)); if(i) add(ans, dp(res - sum, nxt, 0, 1)); if(i && j) add(ans, dp(res - sum, nxt, 0, 0)); } } } else if(B) { for(int i=0;i<10;i++) { ll sum = table[i] + table[(i + A) % 10]; if(sum > res) continue; bool nxt = i + A >= 10; add(ans, dp(res - sum, nxt, 1, 0)); if(i) add(ans, dp(res - sum, nxt, 0, 0)); } } else if(C) { for(int i=0;i<10;i++) { ll sum = table[i] + table[(i + A) % 10]; if(sum > res) continue; bool nxt = i + A >= 10; add(ans, dp(res - sum, nxt, 0, 1)); if(i) add(ans, dp(res - sum, nxt, 0, 0)); } } return ans;}int main() { scanf("%d",&T); while(T--) { scanf("%I64d%I64d",&n,&m); printf("Case #%d: ",++kase); ll ans = dp(n - 3, 0, 1, 1); printf("%I64d\n",ans); } return 0;}
0 0
- HDU 5456 Matches Puzzle Game [数位DP]
- HDU 5456 Matches Puzzle Game(数位DP)
- hdu 5456 Matches Puzzle Game(dp)
- HDU 5456 Matches Puzzle Game 【DP】
- HDU5456 Matches Puzzle Game[数位DP]
- Hdu 5456 Matches Puzzle Game
- [DP] HDOJ 5456 Matches Puzzle Game
- hdu5456 Matches Puzzle Game(记忆化dfs+dp)
- hdu 5456 Matches Puzzle Game(记忆化搜索)
- HDU 5456 Matches Puzzle Game 2015沈阳网络赛(记忆化搜索)
- HDU 5456 Matches Puzzle Game(2015 ACM/ICPC Asia Regional Shenyang Online)
- hdu5456 Matches Puzzle Game
- 2015 ACM/ICPC Asia Regional Shenyang Online 1007 hdu 5456 Matches Puzzle Game
- Hdu 5456 Matches Puzzle Game (记忆化搜索) 2015 ACM-ICPC沈阳网赛
- HDU5456 Matches Puzzle Game (记忆化搜索)
- HDU 5456(数位dp)
- HDU 5456 数位dp
- hdu 4734(数位dp)
- 第3周项目1-个人所得税计算器
- ZOJ 1010 Area
- Ubuntu 12.04中文输入法的安装
- 第3周项目3—输出星号图(3)
- repo 管理多git 项目
- HDU 5456 Matches Puzzle Game(数位DP)
- Ubuntu14.04下安装Nessus
- ListView触摸事件和Item点击事件冲突解决方法
- 13、使用DBUtils操作数据库
- Scala之主构造函数
- Beautiful String
- 安卓开发之AutoCompleteTextView(关键词自动匹配)
- 我和我的小伙伴们
- 如何在google被封的情况下登陆stackoverflow账号?