【HDU - 5456】Matches Puzzle Game(数位dp,思维)

题干:

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=CA−B=C with exactly n (5≤n≤500)n (5≤n≤500) matches (or sticks). 


In these equations, A,BA,B and CC 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)m (3≤m≤2×109). 

Input

The input contains several test cases. The first line of the input is a single integer tt which is the number of test cases. Then t (1≤t≤30)t (1≤t≤30) test cases follow. 

Each test case contains one line with two integers n (5≤n≤500)n (5≤n≤500) and m (3≤m≤2×109)m (3≤m≤2×109).

Output

For each test case, you should output the answer modulo mm. 

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

解题报告:

   一眼上来就是搜索,谁能想到是数位dp、、太难了太难了、、

首先去掉3根火柴棒来凑成减号和等号,然后问题就变成了将剩下的火柴棒拼成B+C=A的形式。然后同时对ABC这三个数的同一个数位进行dp,注意这题dp的时候不是跟一般的数位dp一样是从高位到低位进行dp的,而是从低位像高位进行dp,因为这样方便记录前导零的问题。

参考题解,用dp[n][a][b][c]来表示剩余n根火柴棒时,从最低位开始的放法总数,a表示当前位是否有来自低一位的进位,b表示是否已停止放B这个数,c表示是否已停止放C这个数,则最终我们所求的结果就是dp[n-3][0][0][0]。

代码逻辑是这样的:

num[i]代表要凑出i这个数字需要的火柴个数。

每次检查当前剩余多少根火柴,如果<0直接return 0,然后如果b和c都已经停止了,那么直接进入出口,当然,出口中返回0或者1要检查是否合法:如果当前没有来自低一位的进位(又因为B和C已经停止了),所以你当前位也肯定是啥都没有,也就是要检查cnt是否=0;如果有来自低一位的进位,那么当前位一定是1,也就是看cnt是否==num[1]就行了。

然后就是枚举中间过程,注意这里只是用了数位dp的思想,所以不需要设置limit啥之类的东西,并且之类的res需要设置成longlong不然就爆int了。

然后就是分b和c这几种情况进行讨论,如果B和C都还没确定,那么就枚举他们分别取值是什么,然后通过B和C的取值就得到A对应位的取值(不难证明 一定是(B+C)%10,并且如果有进位,那么进位一定是1,可以通过小学学习的加法竖式简单证明)。

枚举好B和C之后,就有几种选择,可以让B停止,可以让C停止,也可以让B和C都停止,分别进行讨论就行了。注意B能停止的话,一定要保证i>0,同理C能停止的话,一定要保证j>0,因为不然就会出现前导零的情况使得这样就会出现前导零的情况,所以我们要避免这种情况发生,所以我没要特判一下i是否等于0,来进行下一步判断就可以了,其他的情况同理,就不再说了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 555 + 5;
int t,iCase,n;
const int num[]= {6,2,5,5,4,5,6,3,7,6};
ll mod;
ll dp[555][2][2][2];
ll dfs(int cnt,int a,int b,int c) {if(cnt<0)return 0;if(~dp[cnt][a][b][c])return dp[cnt][a][b][c];if(b&&c) {if((a&&cnt==num[1])||(!a&&!cnt))return 1;else return 0;}ll res = 0;if(!b&&!c) {for(int i=0; i<=9; ++i)for(int j=0; j<=9; ++j) {res+=dfs(cnt-(num[i]+num[j]+num[(i+j+a)%10]),(i+j+a)/10,0,0);if(i)res+=dfs(cnt-(num[i]+num[j]+num[(i+j+a)%10]),(i+j+a)/10,1,0);if(j)res+=dfs(cnt-(num[i]+num[j]+num[(i+j+a)%10]),(i+j+a)/10,0,1);if(i&&j)res+=dfs(cnt-(num[i]+num[j]+num[(i+j+a)%10]),(i+j+a)/10,1,1);}} else if(!b) {for(int i=0; i<=9; ++i) {res+=dfs(cnt-(num[i]+num[(i+a)%10]),(i+a)/10,0,1);if(i)res+=dfs(cnt-(num[i]+num[(i+a)%10]),(i+a)/10,1,1);}} else if(!c) {for(int i=0; i<=9; ++i) {res+=dfs(cnt-(num[i]+num[(i+a)%10]),(i+a)/10,1,0);if(i)res+=dfs(cnt-(num[i]+num[(i+a)%10]),(i+a)/10,1,1);}}dp[cnt][a][b][c] = res % mod;return dp[cnt][a][b][c];
}
int main()
{cin>>t;while(t--) {memset(dp,-1,sizeof dp);scanf("%d%lld",&n,&mod);ll ans = dfs(n-3,0,0,0)%mod;ans = (ans+mod)%mod;printf("Case #%d: %lld\n",++iCase,ans);}return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440182.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

图解算法学习笔记(三):递归

本章内容&#xff1a; 学习递归&#xff1b;如何将问题分解成基线条件和递归条件。 1) 每个递归函数都有两部分&#xff1a;基线条件(base case)和递归条件(recursive base)。例如&#xff1a;打印3...2...1 def countdown(i):print(i)if i < 0:returnelse:countdown(i…

深入理解Angular模块化概念

深入理解Angular模块NgModule装饰器 Angular应用程序全部是由 模块化组成的 &#xff0c;即模块化开发&#xff08;组件/指令/服务/管道/路由&#xff09;&#xff0c;与js模块化是不同的概念&#xff0c;但具有异曲同工之妙&#xff1b; Angular 模块之间是隔离 的&#xff0…

在IIS中启用父路径,不被黑客利用

在IIS中&#xff0c;有时要启用父路径&#xff0c;但黑客常常利用父路径访问硬盘文件。因此&#xff0c;我使用了一种方法&#xff1a; 先将IIS暂停&#xff0c;启用父路径。之所以暂停是为了防止操作时遭攻击然后&#xff0c;在根目录下创建虚拟路径“..”&#xff0c;任意选择…

【HRBUST - 1996】数学等式 (HASH 或 二分)

题干&#xff1a; 又到了数学题的时刻了&#xff0c;给出三个数组A,B,C,然后再给出一个数X&#xff0c;现在我想知道是否能找到三个数满足等式A[i]B[j]C[k]X&#xff0c;你能帮助我么&#xff1f;&#xff1f; Input 本题有多组数据&#xff0c;每组数据第一行输入三个数n, …

Apollo自动驾驶入门课程第⑨讲 — 控制(上)

目录 1. 简介 2. 控制流程 3. PID控制 4. PID优劣对比 本文转自微信公众号&#xff1a;Apollo开发者社区 原创&#xff1a; 阿波君 Apollo开发者社区 9月26日 上周我们发布了无人驾驶技术的 规划篇&#xff0c;车辆基于高精地图&#xff0c;感知和预测模块的数据来进行这一…

Angular高版本中为自定义的独立class类添加显式的Angular装饰器

Angular9或10及以后的版本&#xff0c;如果自定义的类上面没有写装饰器的话&#xff0c;编译后在Browser平台不会报错&#xff0c;但是在执行打包命令npm run build --prod时就会报错如下所示&#xff1a; error NG2007: Class is using Angular features but is not decorate…

Python操作Kafka爬坑

组内做大数据&#xff0c;需要kafka写入数据&#xff0c;最近在看python正好&#xff0c;练练手&#xff0c;网上找了一圈&#xff0c;都是用的pykafka&#xff0c;经过一整圈的安装&#xff0c;最终搞定&#xff0c;代码如下#coding:u8import sysimport timeimport randomimpo…

Apollo自动驾驶入门课程第⑩讲 — 控制(下)

目录 1. 线性二次调节器 2. 模型控制预测 3. 总结 本文转自微信公众号&#xff1a;Apollo开发者社区 原创&#xff1a; 阿波君 Apollo开发者社区 昨天 Apollo自动驾驶课程马上进入尾声&#xff0c;在无人驾驶技术控制篇&#xff08;上&#xff09;中&#xff0c;具体讲解了最…

*【ZOJ - 3703】Happy Programming Contest(带优先级的01背包)

题干&#xff1a; In Zhejiang University Programming Contest, a team is called "couple team" if it consists of only two students loving each other. In the contest, the team will get a lovely balloon with unique color for each problem they solved.…

图解算法学习笔记(四):快速排序

目录 1&#xff09; 示例1&#xff1a; 2&#xff09;快速排序 3) 再谈大O表示法 4&#xff09;小结 本章内容&#xff1a;学习分而治之&#xff0c;快速排序 1&#xff09; 示例1&#xff1a; 假设你是农场主&#xff0c;有一小块土地&#xff0c;你要将这块地均匀分成方…

ThriftParserError: ThriftPy does not support generating module with path in protocol 'd'

使用python连接hive&#xff0c;在 from impala.dbapi import connect 语句报如下错误&#xff1a; ThriftParserError: ThriftPy does not support generating module with path in protocol d 定位到 D:\Anaconda3\Lib\site-packages\thriftpy\parser\parser.py的 if u…

【HDU - 5468】Puzzled Elena(容斥原理,dfs序,数学,素因子分解,有坑)

题干&#xff1a; Problem Description Since both Stefan and Damon fell in love with Elena, and it was really difficult for her to choose. Bonnie, her best friend, suggested her to throw a question to them, and she would choose the one who can solve it.Suppo…

图解算法学习笔记(五):散列表

目录 1&#xff09;示例1&#xff1a; 2&#xff09;散列函数 3&#xff09;应用案例 4&#xff09;冲突 5&#xff09;性能 6&#xff09;小结 本章内容&#xff1a; 学习散列表&#xff0c;最有用的数据结构之一。 学习散列表的内部机制&#xff1a;实现、冲突和散列函…

Ros 消息结构1

1、ROS的消息头信息 #Standard metadata for higher-level flow data types #sequence ID: consecutively increasing ID uint32 seq#Two-integer timestamp that is expressed as: # * stamp.secs: seconds (stamp_secs) since epoch # * stamp.nsecs: nanoseconds since sta…

【HDU - 5475】An easy problem(线段树,思维)

题干&#xff1a; One day, a useless calculator was being built by Kuros. Lets assume that number X is showed on the screen of calculator. At first, X 1. This calculator only supports two types of operation. 1. multiply X with a number. 2. divide X with…

图解算法学习笔记(六):广度优先搜索

目录 1&#xff09;图简介 2&#xff09;图是什么 3&#xff09;广度优先搜索 4&#xff09;实现图 5&#xff09;实现算法 6&#xff09;小结 本章内容; 学习使用新的数据结构图来建立网络模型&#xff1b; 学习广度优先搜索&#xff1b; 学习有向图和无向图…

图解算法学习笔记(七):狄克斯特拉算法

目录 1&#xff09;使用狄克斯特拉算法 2&#xff09;术语 3&#xff09;实现 4&#xff09;小结 本章内容; 介绍加权图&#xff0c;提高或降低某些边的权重&#xff1b; 介绍狄克斯特拉算法&#xff0c;找出加权图中前往X的最短路径&#xff1b; 介绍图中的环…

【HDU - 5477】A Sweet Journey(思维,水题)

题干&#xff1a; Master Di plans to take his girlfriend for a travel by bike. Their journey, which can be seen as a line segment of length L, is a road of swamps and flats. In the swamp, it takes A point strengths per meter for Master Di to ride; In the f…

[转载]Bluetooth协议栈学习之SDP

原文地址&#xff1a;Bluetooth协议栈学习之SDP作者&#xff1a;BigSam78作者&#xff1a; Sam &#xff08;甄峰&#xff09; sam_codehotmail.com SDP(service discovery protocol&#xff1a;服务发现协议)提供了一个方法&#xff0c;让应用程序检测哪些服务是可用的并探测这…

图解算法学习笔记(八):贪婪算法

目录 &#xff08;1&#xff09;背包问题 &#xff08;2&#xff09;集合覆盖问题 &#xff08;3&#xff09;NP完全问题 &#xff08;4&#xff09;小结 本章内容&#xff1a; 学习如何处理没有快速算法的问题&#xff08;NP完全问题&#xff09;。学习近似算法&#xff…