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)
m (3≤m≤2×109).
Output
For each test case, you should output the answer modulo
m.
题意:
给出每个数字的火柴拼接数,问用N个火柴棍构恰好造出一个无前导0的A-B=C的方程式的方案数(减号和等号算3个棍子)
解法:
转换为A+B=C,然后数位DP求解,DP[I][J][2][3][3] 分别表示 棒数 位数 进位状态 A的状态,B的状态 :状态0表示前导为1-9,1表示前导为0,2表示前导为空格
(状态比较丑,A,B的状态其实可以只表示成2种:0:没到最高位,1:已经到过最高位)
可以发现每多一位(即J+1),I都是增加的(除非A,B的前导都是空格),那么J这一维可以省略(这个优化完全想不到,一开始跑了900MS,后来才知道这一维可以优化掉,然后就变成了60MS)
注意取模数最大为20亿,所以加法会暴int,需要定义成unsigned int
代码:
#include
#include
#include
#include
#include
#include
#include
#include