题意:给出n个火柴棒,问能摆成多少个a-b=c的等式,其中,a,b,c均为正整数。
做法:dp[i][j][cr][b][c],当前用了i个火柴棒,从低位到高位,已经考虑到了第j位,cr代表j-1位是否给j位贡献了一个1,b代表数字b是否已经到了最高位,c代表数字c是否已经到了最高位。有多少种方法到达目标状态。不断枚举在j位的数字进行数位dp即可。由于j肯定是不断在增大的,所以写的时候可以直接舍去这维。
#include
Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 65535/102400 K (Java/Others)
Total Submission(s): 107 Accepted Submission(s): 70
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
2015 ACM/ICPC Asia Regional Shenyang Online