题目

Source

http://acm.hdu.edu.cn/showproblem.php?pid=5456

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

分析

题目求用N根火柴棒拼成A-B=C这种等式的方案数。

化成加法形式B+C=A,这样比较好写。
类似加法竖式的样子,从低位到高位,考虑DP:

  • dp[n][0/1][0/1][0/1]表示,还剩下的火柴棒数为n,是否向下一位进位,B最高位是否已确定,C最高位是否已确定

按字面意思转移。。

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
long long d[555][2][2][2];
int cost[10]={6,2,5,5,4,5,6,3,7,6};
int main(){
int t,n;
long long m;
scanf("%d",&t);
for(int cse=1; cse<=t; ++cse){
scanf("%d%I64d",&n,&m);
n-=3;
memset(d,0,sizeof(d));
d[n][0][0][0]=1;
for(int len=n; len>0; --len){
for(int i=0; i<=9; ++i){
for(int j=0; j<=9; ++j){
if(cost[i]+cost[j]>len) continue; int tmp0=len-cost[i]-cost[j]-cost[(i+j)%10];
int tmp1=len-cost[i]-cost[j]-cost[(i+j+1)%10]; if(tmp0>=0) d[tmp0][i+j>9][0][0]+=d[len][0][0][0],d[tmp0][i+j>9][0][0]%=m;
if(tmp1>=0) d[tmp1][i+j+1>9][0][0]+=d[len][1][0][0],d[tmp1][i+j+1>9][0][0]%=m; if(i){
if(tmp0>=0) d[tmp0][i+j>9][1][0]+=d[len][0][0][0],d[tmp0][i+j>9][1][0]%=m;
if(tmp1>=0) d[tmp1][i+j+1>9][1][0]+=d[len][1][0][0],d[tmp1][i+j+1>9][1][0]%=m;
}
if(j){
if(tmp0>=0) d[tmp0][i+j>9][0][1]+=d[len][0][0][0],d[tmp0][i+j>9][0][1]%=m;
if(tmp1>=0) d[tmp1][i+j+1>9][0][1]+=d[len][1][0][0],d[tmp1][i+j+1>9][0][1]%=m;
}
if(i&&j){
if(tmp0>=0) d[tmp0][i+j>9][1][1]+=d[len][0][0][0],d[tmp0][i+j>9][1][1]%=m;
if(tmp1>=0) d[tmp1][i+j+1>9][1][1]+=d[len][1][0][0],d[tmp1][i+j+1>9][1][1]%=m;
}
}
}
for(int i=0; i<=9; ++i){
int tmp0=len-cost[i]-cost[i];
int tmp1=len-cost[i]-cost[(i+1)%10]; if(tmp0>=0) d[tmp0][0][0][1]+=d[len][0][0][1],d[tmp0][0][0][1]%=m;
if(tmp1>=0) d[tmp1][i+1>9][0][1]+=d[len][1][0][1],d[tmp1][i+1>9][0][1]%=m; if(tmp0>=0) d[tmp0][0][1][0]+=d[len][0][1][0],d[tmp0][0][1][0]%=m;
if(tmp1>=0) d[tmp1][i+1>9][1][0]+=d[len][1][1][0],d[tmp1][i+1>9][1][0]%=m; if(i){
if(tmp0>=0) d[tmp0][0][1][1]+=d[len][0][0][1],d[tmp0][0][1][1]%=m;
if(tmp1>=0) d[tmp1][i+1>9][1][1]+=d[len][1][0][1],d[tmp1][i+1>9][1][1]%=m; if(tmp0>=0) d[tmp0][0][1][1]+=d[len][0][1][0],d[tmp0][0][1][1]%=m;
if(tmp1>=0) d[tmp1][i+1>9][1][1]+=d[len][1][1][0],d[tmp1][i+1>9][1][1]%=m;
}
}
}
long long res=(d[0][0][1][1]+d[cost[1]][1][1][1])%m;
printf("Case #%d: %I64d\n",cse,res);
}
return 0;
}

HDU5456 Matches Puzzle Game(DP)的更多相关文章

  1. 【Hihocoder1634】Puzzle Game(DP)

    题意:有一个n*m的矩阵,每个矩阵里有一个数字a[i][j].现在要求将其中一个格子的值改为p,使得修改后矩阵的最大子矩阵和最小,求这个最小值 n,m<=150,abs(a[i][j])< ...

  2. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  3. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  4. lightOJ 1047 Neighbor House (DP)

    lightOJ 1047   Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...

  5. UVA11125 - Arrange Some Marbles(dp)

    UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...

  6. 初探动态规划(DP)

    学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...

  7. Tour(dp)

    Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...

  8. 2017百度之星资格赛 1003:度度熊与邪恶大魔王(DP)

    .navbar-nav > li.active > a { background-image: none; background-color: #058; } .navbar-invers ...

  9. Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)

    Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...

随机推荐

  1. C++ 操作XML文件 使用MSXML.DLL

    使用MSXML.DLL读写XML; 文件顶部加入 #import "msxml3.dll"; using namespace MSXML2; //这两句作用是,在程序的文件夹下生成 ...

  2. java从基础知识(九)I/O

    java中的流可以从不同的角度进行分类 按流的方向:输入流.输出流(注意这里的输入(read).输出是相对于程序而言的(writer),个人认为从读.写角度理解更为直观) 按处理数据单位:字节流.字符 ...

  3. (转)android 蓝牙通信编程

    转自:http://blog.csdn.net/pwei007/article/details/6015907 Android平台支持蓝牙网络协议栈,实现蓝牙设备之间数据的无线传输. 本文档描述了怎样 ...

  4. 项目 CTR预估

    项目和数据介绍 给定查询和用户信息后预测广告点击率 搜索广告是近年来互联网的主流营收来源之一.在搜索广告背后,一个关键技术就是点击率预测-----pCTR(predict the click-thro ...

  5. Hadoop学习记录

    http://blog.csdn.net/m_star_jy_sy/article/details/26476907配置windows里eclipse连接hadoop集群 hadoop常见命令 启动H ...

  6. too many open files 报错

    看到这种某个程序或sock 打开文件数超出了限制,可以在/etc/security/limits.conf 这个文件中设置某个用户的可打开文件数. 例如: root hard nofile 16384 ...

  7. lombok 简化java代码注解

    lombok 简化java代码注解 安装lombok插件 以intellij ide为例 File-->Setting-->Plugins-->搜索"lombok plug ...

  8. GLUT的简洁OO封装

    毕业设计用到了OpenGL,由于不会用MFC和Win32API做窗口程序:自然选用了GLUT.GLUT很好用,就是每次写一堆Init,注册callback,觉得有点恶心,于是对他做了简单的OO封装.记 ...

  9. centos6u3 安装 celery 总结

    耗时大概6小时. 执行 pip install celery 之后, 在 mac 上 celery 可以正常运行, 在 centos 6u3 上报错如下: Traceback (most recent ...

  10. tp5 中 model 的修改器

    修改器可以在数据赋值的时候自动进行转换处理 class User extends Model { public function setNameAttr($value){ return strtolo ...