Matches Puzzle Game

Problem Description
As 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).
Input
The 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).

Output
For each test case, you should output the answer modulo m.
Sample Input
4
12 1000000007
17 1000000007
20 1000000007
147 1000000007
Sample Output
Case #1: 1
Case #2: 5
Case #3: 38
Case #4: 815630825
Source
 
 
【题意】
  有n根火柴(n<=500),要刚好用完,摆出一个减式A-B=C(A>0,B>0,C>0)求有多少种方案
 
【分析】
  这题很好想,但是就看个人DP能力了,我一开始打的DP就TLE,真是太年轻!
  之前做的几题数位DP都是数字限制,即数值固定一个区间,而现在这题可不管你那个数有多大,只要火柴够用就好了。
  所以之前的题我是记录位数,标记前导0的,再加个越限标记flag。
  搞到我这题一开始就也记录位数了,然记录位数并无卵用!!还会TLE啊ORZ,
  我一开始想法,先减掉3根火柴,减法变加法(加法好打一些),f[i][j][k]表示i位,j根火柴,k表示状态(即两个加数分别是否还处于前导0中),然后记忆化搜索
  位数不超过n/4的,所以时间大概是150*500*4*10*10*2,代码也放一下,正确性还是保证的:
  

 #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)的更多相关文章

  1. HDU5456 Matches Puzzle Game(DP)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5456 Description As an exciting puzzle game for ...

  2. HDU 4507 (鬼畜级别的数位DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4507 题目大意:求指定范围内与7不沾边的所有数的平方和.结果要mod 10^9+7(鬼畜の元凶) 解题 ...

  3. HDU 5787 K-wolf Number (数位DP)

    K-wolf Number 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5787 Description Alice thinks an integ ...

  4. 【HDU 3652】 B-number (数位DP)

    B-number Problem Description A wqb-number, or B-number for short, is a non-negative integer whose de ...

  5. HDU 5787 K-wolf Number(数位DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5787 [题目大意] 求区间[L,R]内十进制数相邻k位之间不相同的数字的个数. [题解] 很显然的 ...

  6. 2017"百度之星"程序设计大赛 - 复赛1005&&HDU 6148 Valley Numer【数位dp】

    Valley Numer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  7. HDU 4352 XHXJ's LIS 数位dp lis

    目录 题目链接 题解 代码 题目链接 HDU 4352 XHXJ's LIS 题解 对于lis求的过程 对一个数列,都可以用nlogn的方法来的到它的一个可行lis 对这个logn的方法求解lis时用 ...

  8. HDU 2089 不要62(数位dp模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意:求区间内不包含4和连续62的数的个数. 思路: 简单的数位dp模板题.给大家推荐一个好的讲解博客.h ...

  9. 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 ...

  10. HDU 2089(暴力和数位dp)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2089 不要62 Time Limit: 1000/1000 MS (Java/Others)    M ...

随机推荐

  1. Clean Code(三):注释

    在工作中经常在刚开始写代码的时候,写下类与方法的注释,时间久了,CR多了,也不可能一直去修改注释了.加班都累成dog.注释才不改呢,就是这么任性,哈哈. 项目久了,注释会欺骗阅读者,本人被骗过,也骗过 ...

  2. KindEditor 修改多图片上传显示限制大小和张数

    在使用KindEditor的时候用到多图片上传时,提示有最多上传20张图片,单张图片容量不超过1MB: 修改的文件的地方在:kindeditor\plugins\multiimage\multiima ...

  3. magento 操作数据库

    查:     $read = Mage::getSingleton(“core/resource”)->getConnection(‘core_read’);     $sql = “selec ...

  4. SQL Server delete、truncate、drop

    在T-SQL中这三个命令符,相信很多朋友都不会陌生的,我自己在工作也会常常使用到它们,虽然我们清除的知道用这三个命令符可以达到怎样的预期效果. 但是却很少深入的去了解它们,知道它们有什么区别,又各有什 ...

  5. VIM学习1

    不得不说鸟哥的Linux写得太好了,VIM篇章,通读一篇,感觉收获挺大.之前几年前装逼硬着学,感觉硬是没懂,看的特晕,学得特别慢,抄一两遍也没什么多大的作用.这一回看了,感觉马上就能记住不少,当然大多 ...

  6. mvc Routing特性优化

    在mvc中,Url地址是利用routing特性来支持,但是这个Routing有个问题,多个不同的地址和指向同一个action方法, 例如: http://test.com (默认) http://te ...

  7. MVC小系列(十二)【RenderAction和RenderPartial区别】

    二者作用:RenderAction:渲染分部视图到页面上,要求提供Action和控制器名称RenderPartial:渲染分部视图到页面上,要求提供分部视图的名称,即路径,如果是在当前控制下或者sha ...

  8. SQL Cursor 游标的使用

    DECLARE @name VARCHAR(50)  --声明游标 DECLARE cursor_VAA1 CURSOR FOR SELECT VAA05 FROM VAA1 --打开游标 OPEN ...

  9. iOS iTunes文件共享

    众所周知苹果不允许用户查看文件,不同的应用之间文件也没有联系.从电脑上往手机传文件,也只能通过开放沙盒目录,传到对应的应用下. 有时候我们需要导入文件到应用沙盒下,或者从沙盒中导出文件.这就需要应用的 ...

  10. C++类继承内存布局(一)

    转自:http://blog.csdn.net/jiangyi711/article/details/4890889# 一 类布局 不同的继承方式将导致不同的内存布局 1)C结构 C++基于C,所以C ...