【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 ...
随机推荐
- [Form Builder]内置函数execute_trigger、do_key详解
转:http://yedward.net/?id=82 1.execute_trigger:用来运行一个指定的触发器,常用来运行用户自定义的触发器. 语法:procedure execute_trig ...
- java基础加强
一.泛型 Generic 1.集合泛型: 在没有泛型之前,集合中存入的数据,类型就会丢失掉,在取出数据时,需要做强制类型转换,就有转换失败的风险,而这种风险,在编译阶段是没有办法检查出来的 引入泛型后 ...
- C# 自定义排序
/// <summary> /// 实体 /// </summary> public class Product { public int ID { get; set; } p ...
- Hyper-V Windows 8.1 & Windows Server 2012 R2 Q&A
从Windows8开始,x64位系统自带Hyper-V功能,很多开发者和专业用户往往希望利用的Microsoft提供的这一免费功能,但是微软在这方面并不是最佳. 主要写几个大家经常遇到的问题. Win ...
- hadoop_集群安装_1
这篇文章中主要介绍的是,如何基于VM安装Linux,以及如何在安装好Linux之后,基于操作系统安装VMTools. 在安装之前,应该先规划好 每个node*的IP地址,以及 hostname: no ...
- mongodb write 【摘自网上,只为记录,学习】
mongodb有一个write concern的设置,作用是保障write operation的可靠性.一般是在client driver里设置的,和db.getLastError()方法关系很大 一 ...
- javascript BOM对象 第15节
<html> <head> <title>浏览器对象</title> <script type="text/javascript&quo ...
- 02_Jquery_03_类选择器
[简述] 类选择器就是通过类名(css类名)来查询元素! $(".myClass")就可以把所有包含了class="myClass"的元素查询出来 [index ...
- 配置iSCSI
先查下yum list | grep iscsi, 存在iscsi包, 进行安装:yum install iscsi-initiator-utils.x86_64, cat /etc/iscsi/in ...
- 九度OJ 1079 手机键盘
题目地址:http://ac.jobdu.com/problem.php?pid=1079 题目描述: 按照手机键盘输入字母的方式,计算所花费的时间 如:a,b,c都在“1”键上,输入a只需要按一次, ...

