【HDU 5456】 Matches Puzzle Game (数位DP)
Matches Puzzle Game
Problem DescriptionAs an exciting puzzle game for kids and girlfriends, the Matches Puzzle Game asks the player to find the number of possible equations A−B=C with exactly n (5≤n≤500) matches (or sticks).
In these equations, A,B and C are positive integers. The equality sign needs two matches and the sign of subtraction needs just one. Leading zeros are not allowed.
Please answer the number, modulo a given integer m (3≤m≤2×109).InputThe input contains several test cases. The first line of the input is a single integer t which is the number of test cases. Then t (1≤t≤30) test cases follow.Each test case contains one line with two integers n (5≤n≤500) and m (3≤m≤2×109).
OutputFor each test case, you should output the answer modulo m.Sample Input4
12 1000000007
17 1000000007
20 1000000007
147 1000000007Sample OutputCase #1: 1
Case #2: 5
Case #3: 38
Case #4: 815630825Source
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define LL long long int f[][][][];//weishu huocai zero shifoujinwei
// 0 00 1 0x 2 x0 3 xx
int us[]={,,,,,,,,,};
int m,sum; int ffind(int n,int k,int zero,int step)
{
sum++;
if(k<) return ;
if(n==) return (k==&&step==);
if(f[n][k][zero][step]!=-) return f[n][k][zero][step];
LL ans=;
for(int a=;a<;a++)
for(int b=;b<;b++)
{
for(int l=;l<;l++) //xia yi bu shi fou jin wei
{
if(n==&&a==&&zero<=) continue;
if(n==&&b==&&zero!=&&zero!=) continue;
if(step&&(a+b+l<)) continue;
if(!step&&(a+b+l>=)) continue;
int now=;
if(a!=||zero==||zero==) now+=us[a];
if(b!=||zero==||zero==) now+=us[b];
if(a!=||b!=||l!=||zero!=) now+=us[(a+b+l)%];
if(now>k) continue; int nz;
if(a==&&b==&&zero==) nz=;
else if(a==&&zero!=&&zero!=) nz=;
else if(b==&&zero!=&&zero!=) nz=;
else nz=;
ans=(ans+ffind(n-,k-now,nz,l) )%m;
}
}
f[n][k][zero][step]=(int)ans;
return (int)ans;
} int main()
{
int T,kase=;
scanf("%d",&T);
while(T--)
{
sum=;
int n;
scanf("%d%d",&n,&m);
memset(f,-,sizeof(f));
printf("Case #%d: %d\n",++kase,ffind(n/,n,,));
}
return ;
}
TLE的代码
其实与位数无关,但是不及记录位数就要从低位开始填数了,不然无法表示,会重复。
f[j][k]表示j根火柴,k状态,状态表示当前两个加数分别是否结束了,结束了只能填0,并且不花费火柴。
AC代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define LL long long int f[][][];//weishu huocai zero shifoujinwei
// 0 00 1 0x 2 x0 3 xx
int us[]={,,,,,,,,,};
int m; int ffind(int k,int zero,int step)
{
if(k<) return ;
if(zero==)
{
if(step==) k-=;
return k==;
}
if(f[k][zero][step]!=-) return f[k][zero][step];
LL ans=;
for(int a=;a<;a++)
{
for(int b=;b<;b++)
{
int now=;
if(zero==||zero==) now+=us[a];
if(zero==||zero==) now+=us[b];
now+=us[(a+b+step)%];
if(now>k) continue; ans=(ans+ffind(k-now,zero,a+b+step>=) )%m;
if(a!=&&zero!=&&zero!=) ans=(ans+ffind(k-now,zero==?:,a+b+step>=) )%m;
if(b!=&&zero!=&&zero!=) ans=(ans+ffind(k-now,zero==?:,a+b+step>=) )%m;
if(a!=&&b!=&&zero==) ans=(ans+ffind(k-now,,a+b+step>=) )%m; if(zero==||zero==) break;
}
if(zero==||zero==) break;
} f[k][zero][step]=(int)ans;
return (int)ans;
} int main()
{
int T,kase=;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d%d",&n,&m);
memset(f,-,sizeof(f));
printf("Case #%d: %d\n",++kase,ffind(n,,));
}
return ;
}
[HDU 5456]
所以如果跟位数无关,最好思想回到原始的位置啊,从低位开始填可能会--柳暗花明又一村??
2016-10-09 14:15:15
【HDU 5456】 Matches Puzzle Game (数位DP)的更多相关文章
- HDU5456 Matches Puzzle Game(DP)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5456 Description As an exciting puzzle game for ...
- HDU 4507 (鬼畜级别的数位DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4507 题目大意:求指定范围内与7不沾边的所有数的平方和.结果要mod 10^9+7(鬼畜の元凶) 解题 ...
- HDU 5787 K-wolf Number (数位DP)
K-wolf Number 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5787 Description Alice thinks an integ ...
- 【HDU 3652】 B-number (数位DP)
B-number Problem Description A wqb-number, or B-number for short, is a non-negative integer whose de ...
- HDU 5787 K-wolf Number(数位DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5787 [题目大意] 求区间[L,R]内十进制数相邻k位之间不相同的数字的个数. [题解] 很显然的 ...
- 2017"百度之星"程序设计大赛 - 复赛1005&&HDU 6148 Valley Numer【数位dp】
Valley Numer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 4352 XHXJ's LIS 数位dp lis
目录 题目链接 题解 代码 题目链接 HDU 4352 XHXJ's LIS 题解 对于lis求的过程 对一个数列,都可以用nlogn的方法来的到它的一个可行lis 对这个logn的方法求解lis时用 ...
- HDU 2089 不要62(数位dp模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意:求区间内不包含4和连续62的数的个数. 思路: 简单的数位dp模板题.给大家推荐一个好的讲解博客.h ...
- hdu 4352 XHXJ's LIS 数位dp+状态压缩
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4352 XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others ...
- HDU 2089(暴力和数位dp)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2089 不要62 Time Limit: 1000/1000 MS (Java/Others) M ...
随机推荐
- Javascript,闭包
js变量的调用 一.全局变量函数调用 var n = 99; function f1() { alert(n); } f1();//99 在函数外部定义的变量,再函数中可以被调用,因为变量n,与f1在 ...
- Unity3D 之武器系统冷却功能的实现方式
先上方法 //如果Fire1按钮被按下(默认为ctrl),每0.5秒实例化一发子弹 public GameObject projectile; public float fireRate = 0.5F ...
- Js中的appenChild,insertBefore--createDocumentFragment
平时项目中会有一些流程,或者是评论相关的东西,这些一般只会是在页面初次加载一部分,剩余部分搞一个更多的标签,当点击更多的时候,ajax请求把所有数据加载完(当然这里也有分页的实现方法,本篇不作讨论), ...
- DDX_Text (MFC)
DDX_Text (MFC) 描述:该DDX_Text功能管理int的转移,UINT,long,DWORD,CString,float, 或 double编辑控件之间的数据在对话框中,表单视图或控制视 ...
- JS中==和===的区别
1.对于string,number等基础类型,==和===是有区别的 1)不同类型间比较,==之比较“转化成同一类型后的值”看“值”是否相等,===如果类型不同,其结果就是不等 2)同类型比较,直接进 ...
- eclipse下使用maven配置库托管jar包
1.项目是通过maven配置库托管jar包 首先要保证maven配置库中有相应的jar包才能通过这个方法来添加jar包.maven的有点就是把要用到的jar包统一放在一个配置库中,在某个项目需要用到这 ...
- cyark - 数字方舟(看侣行第三季时发现的)
数字方舟 Ben Kacyra
- Codevs 1697 ⑨要写信
1697 ⑨要写信 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 传送门 题目描述 Description 琪露诺(冰之妖精)有操控冷气的能力.能瞬间冻结小东西,比 ...
- C/C++中memset函数
本文学习参考http://baike.baidu.com/link?url=ZmSyY8ciB_nJt9KM-W2fiEFJrC2mugFsLqRdY2b4pLe8rD_jRXyN7_pj0GBBD2 ...
- Oracle 10g 下载地址
Oracle Database 10g Release 2 (10.2.0.1.0) Enterprise/Standard Edition for Microsoft Windows (32-bit ...