Six guys (a,b,c,d,e,f) play passing ball game.They stand from a to f as a clockwise cycle. The rule is: one only can pass ball to the
person next to him. If the ball pass to d,the game is over. At first, a keep the ball.
Problem: when they pass the ball k(0<=k<=1000) times and the ball is at i (i represents one of the six guys),how many ways can they do it?
As the ways is too large,you only need tell me ways%M(1<=M<=10000).
输入格式
Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <=T <= 50) which is the number of test cases. For each test case, there is three parts in one line. The first part is a positive integer k that represents they pass the ball k times, The second part is a character c which means at end the ball is at c. The last part is a positive integer M,
输出格式
For each test case, output one line containing an integer, which represents ways%M.
样例输入
2 0 a 10 1 b 3
样例输出
1 1除了第四个人,其余人的转移方程为dp[i][j]=(dp[i-1][(j+1)%6]+dp[i-1][(j+5)%6])%m
关于4的处理可以先全部不算,如果询问是4的话就输出dp[k-1][2]+dp[k-1][4])%m
#include<stdio.h>
#include<string.h>
int dp[10001][6];
int main(){
int t,k,m,w,i,j;
char wc;
scanf("%d",&t);
while(t--){
scanf("%d %c %d",&k,&wc,&m);
memset(dp,0,sizeof(dp));
dp[0][0]=1;
switch(wc){
case 'a':w=0;break;
case 'b':w=1;break;
case 'c':w=2;break;
case 'd':w=3;break;
case 'e':w=4;break;
case 'f':w=5;break;
}
for(i=1;i<=k;i++)
for(j=0;j<6;j++){
if(j==3)
continue;
else
dp[i][j]=(dp[i-1][(j+1)%6]+dp[i-1][(j+5)%6])%m;
}
if(w==3)
printf("%d\n",(dp[k-1][2]+dp[k-1][4])%m);
else
printf("%d\n",dp[k][w]);
}
return 0;
}