Word Game hdu 2429 综合题

Word Game

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 592    Accepted Submission(s): 155

Problem Description
      It's a game with two players. Given a dictionary of words, a word S chosen from the dictionary to start with, and a word T also chosen from the dictionary as the winning word, which will be described below, the two players take turns to choose a word from the dictionary, satisfying that the first letter of the chosen word is the same as the last letter of previous word. Each word could be chosen more than once.
      Suppose they play exactly n rounds. At the last round, if the player (the first one if n is odd, the second one otherwise) chooses the winning word T, he wins. To your surprise that, the two players are not so clever that they choose words randomly.
      Here comes the question. How many different ways will the first player win if they play no more than N rounds, among all the possible ways satisfying all the conditions above?
 

 

Input
      An integer C, indicates the number of test cases.
      Then comes C blocks, formatted like this:
      An integer M, indicates the number of words in the dictionary, M <= 30.
      M string consisting of only lowercase letters, represent the words in the dictionary. The length of each word is no more than 10. There are no duplicated words.
      String S, the word to start with.
      String T, the winning word.
      A positive integer N, indicates the maximum number of rounds to play. N fits in a signed 32-bit integer.
 

 

Output
      For each test case, print a line with an integer W, indicating how many possible ways the first player wins.
      Output the answer modulo 10001.
 

 

Sample Input
  
  
1 3 abc cac cde abc cde 3
 

 

Sample Output
  
  
2
 

 

Source
 

 

Recommend
lcy
玩儿接龙游戏,给出一系列单词和起始单词S,结束单词T,两个人轮流走,第一个人先从S走,求在有限步数k内第一个人到达T(到T便为胜)的可能方法数。
我是这么想的,先给可达单词间建边,这样第一个人在k步内到达的方法数就是A1+A3+A5+...+An(Ai 表示矩阵A的i次方)的那个矩阵中[S][T]的位置的数字,之所以是奇数是因为第一个人先走,奇数步到达终点才胜。
问题是n的范围很大,如果就这么矩阵乘法暴力加过去必然tle,那么采用快速幂,二分求解:
解决过程如下:
A1+A3+A5+A7+...+An(n是<=k的最大奇数)
=A1+A1*(A2+A4+A6+...+An-1)
令T=A2;
=A+A(T1+T2+...+T(n-1)/2)
令Sm=T1+T2+...+Tm
则Sm=(T1+T2+...+Tm/2)+(T(m/2+1)+...+Tm)(如果m是奇数还要加上最后一项)
        =Sm/2*(E+T(m/2))(E是单位矩阵)
剩下的就这么二分着递归下去吧……
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值