WOJ1075-Ball

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;
} 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值