A - B = C 可以转化成 B + C = A
题解:http://m.blog.csdn.net/blog/stl112514/48678307
http://acm.hdu.edu.cn/showproblem.php?pid=5456
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
int n,m;
int a[10]={6,2,5,5,4,5,6,3,7,6};
ll dp[510][2][2][2];
int dfs(int num,int cr,int b,int c){
if(num>n)
return 0;
if(dp[num][cr][b][c]!=-1)
return dp[num][cr][b][c];
if(b==1&&c==1){ //两个加数都已经到最高位
if(num==n&&cr==0) //没有进位
return 1;
if(num+a[1]==n&&cr==1) //有进位,剩余的火柴数刚好等于进位"1"
return 1;
return 0;
}
if(num==n) //火柴已经用完,又不符合上面的判别条件
return 0;
dp[num][cr][b][c]=0;
if(b==0){
if(c==0){
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
int t=a[i]+a[j]+a[(i+j+cr)%10];
dp[num][cr][b][c]+=dfs(num+t,(i+j+cr)/10,0,0);
if(i!=0)
dp[num][cr][b][c]+=dfs(num+t,(i+j+cr)/10,1,0);
if(j!=0)
dp[num][cr][b][c]+=dfs(num+t,(i+j+cr)/10,0,1);
if(i!=0&&j!=0)
dp[num][cr][b][c]+=dfs(num+t,(i+j+cr)/10,1,1);
dp[num][cr][b][c]%=m;
}
}
}
else{
for(int i=0;i<10;i++){
int t=a[i]+a[(i+cr)%10];
dp[num][cr][b][c]+=dfs(num+t,(i+cr)/10,0,1);
if(i!=0)
dp[num][cr][b][c]+=dfs(num+t,(i+cr)/10,1,1);
}
}
}
else{
if(c==0){
for(int i=0;i<10;i++){
int t=a[i]+a[(i+cr)%10];
dp[num][cr][b][c]+=dfs(num+t,(i+cr)/10,1,0);
if(i!=0)
dp[num][cr][b][c]+=dfs(num+t,(i+cr)/10,1,1);
}
}
}
return dp[num][cr][b][c]%=m;
}
int main(){
int T;
scanf("%d",&T);
for(int cs=1;cs<=T;cs++){
cin>>n>>m;
n-=3;
memset(dp,-1,sizeof(dp));
printf("Case #%d: %d\n",cs,dfs(0,0,0,0));
}
}
#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<queue>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1e9+7
#define ll long long
using namespace std;
int n,m;
ll dp[505][2][2][2]; //不知道为什么int不行。。
int x[10]={6,2,5,5,4,5,6,3,7,6};
int dfs(int num,int f,int a,int b){
if(num>n)
return 0;
if(dp[num][f][a][b]!=-1)
return dp[num][f][a][b];
if(num==n){
if(f==0&&a==1&&b==1)
return 1;
return 0;
}
if(a==1&&b==1){
if(f==1&&num+2==n)
return 1;
return 0;
}
dp[num][f][a][b]=0;
if(a==0&&b==0){
for(int i=0;i<10;++i){
for(int j=0;j<10;++j){
dp[num][f][a][b]=(dp[num][f][a][b]+dfs(num+x[i]+x[j]+x[(i+j+f)%10],(i+j+f)/10,0,0))%m;
if(i!=0)
dp[num][f][a][b]=(dp[num][f][a][b]+dfs(num+x[i]+x[j]+x[(i+j+f)%10],(i+j+f)/10,1,0))%m;
if(j!=0)
dp[num][f][a][b]=(dp[num][f][a][b]+dfs(num+x[i]+x[j]+x[(i+j+f)%10],(i+j+f)/10,0,1))%m;
if(i!=0&&j!=0)
dp[num][f][a][b]=(dp[num][f][a][b]+dfs(num+x[i]+x[j]+x[(i+j+f)%10],(i+j+f)/10,1,1))%m;
}
}
}
else if(a==1&&b==0){
for(int j=0;j<10;++j){
dp[num][f][a][b]=(dp[num][f][a][b]+dfs(num+x[j]+x[(j+f)%10],(j+f)/10,1,0))%m;
if(j!=0)
dp[num][f][a][b]=(dp[num][f][a][b]+dfs(num+x[j]+x[(j+f)%10],(j+f)/10,1,1))%m;
}
}
else if(a==0&&b==1){
for(int i=0;i<10;++i){
dp[num][f][a][b]=(dp[num][f][a][b]+dfs(num+x[i]+x[(i+f)%10],(i+f)/10,0,1))%m;
if(i!=0)
dp[num][f][a][b]=(dp[num][f][a][b]+dfs(num+x[i]+x[(i+f)%10],(i+f)/10,1,1))%m;
}
}
return dp[num][f][a][b];
}
int main(){
int t,cnt=0;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
n-=3;
memset(dp,-1,sizeof(dp));
printf("Case #%d: %d\n",++cnt,dfs(0,0,0,0));
}
return 0;
}