题目描述:http://poj.org/problem?id=3971
第一次用数位dp,参见http://www.cnblogs.com/lvpengms/p/3926678.html
#include <stdio.h>
#include <string.h>
#define MAX_N 1000000
int N, L, D, dp[MAX_N][2];
char w[MAX_N];
int main(){
int t, i;
scanf("%d", &t);
while(t--){
scanf("%d%d%d", &N, &L, &D);
getchar();
memset(w, 0, sizeof(w));
for(i = L-1; i >= 0; i--){
scanf("%c", &w[i]);
w[i] -= 48;
}
memset(dp, 0, sizeof(dp));
if(w[0]){
dp[0][0] = 1;
dp[0][1] = 1;
}else{
dp[0][0] = 1;
dp[0][1] = 0;
}
for(i = 1; i < N; i++){
if(w[i]){
dp[i][0] += dp[i-1][0];
dp[i][0] %= D;
dp[i][1] += dp[i-1][0];
dp[i][1] %= D;
dp[i][1] += dp[i-1][1];
dp[i][1] %= D;
}else{
dp[i][0] += dp[i-1][0];
dp[i][0] %= D;
dp[i][0] += dp[i-1][1];
dp[i][0] %= D;
dp[i][1] += dp[i-1][1];
dp[i][1] %= D;
}
}
printf("%d\n", dp[N-1][0]);
}
return 0;
}