首页 > ACM题库 > HDU-杭电 > hdu 2429 Word Game-快速幂-[解题报告]C++
2014
01-26

hdu 2429 Word Game-快速幂-[解题报告]C++

Word Game

问题描述 :

      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?

输入:

      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.

输出:

      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.

样例输入:

1
3
abc
cac
cde
abc
cde
3

样例输出:

2

题目:hdu 2429 Word Game

思路:矩阵快速幂。。

其实我理解的题目是后手赢了就不需要再继续走了。。。可是AC的程序是,后手赢了继续走到先手赢也算先手赢得一种,呵呵。

然后就是,防爆栈。。。。。

#include <cstring>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <cstdio>
#include <string>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
const LL mod=10001;
struct Matrix
{
    LL m[33][33];
}E,D;
int M,N;
string s[33],S,T;
int Si,Ti;
void init()
{
    for(int i=1;i<33;i++)
        for(int j=1;j<=33;j++)
            E.m[i][j]=(i==j);
}
Matrix Multi(Matrix A,Matrix B)
{
    Matrix ans;
    for(int i=1;i<=M;i++)
        for(int j=1;j<=M;j++)
        {
            ans.m[i][j]=0;
            for(int k=1;k<=M;k++)
                ans.m[i][j]=(ans.m[i][j]+A.m[i][k]*B.m[k][j])%mod;
        }
    return ans;
}
Matrix Pow(Matrix A,LL k)
{
    Matrix ans=E;
    while(k)
    {
        if(k&1)
        {
            k--;
            ans=Multi(ans,A);
        }
        else
        {
            k/=2;
            A=Multi(A,A);
        }
    }
    return ans;
}
Matrix last(Matrix A,Matrix B,int m,int n,int l)
{
    Matrix ans;
    for(int i=1;i<=m;i++)
        for(int j=1;j<=l;j++)
        {
            ans.m[i][j]=0;
            for(int k=1;k<=n;k++)
                ans.m[i][j]=(ans.m[i][j]+A.m[i][k]*B.m[k][j])%mod;
        }
    return ans;
}
void Print(Matrix A)
{
    for(int i=1;i<=M;i++)
    {
        for(int j=1;j<=M;j++)
            cout<<A.m[i][j]<<" ";
        cout<<endl;
    }
}
Matrix Add(Matrix A,Matrix B)
{
    Matrix ans;
    for(int i=1;i<=M;i++)
        for(int j=1;j<=M;j++)
            ans.m[i][j]=(A.m[i][j]+B.m[i][j])%mod;
    return ans;
}
Matrix Sum(Matrix A,LL k)
{
    if(k==0)
        return E;
    if(k==1)
        return A;
    if(k==3)
        return Add(A,Pow(A,3));
    if(((k+1)/2)&1)
        return Add(Sum(A,k-2),Pow(A,k));
    else
        return Multi(Add(E,Pow(A,(k+1)/2)),Sum(A,(k-1)/2));

}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>M;
        init();
        for(int i=1;i<=M;i++)
            cin>>s[i];
        cin>>S;
        cin>>T;
        cin>>N;
        Matrix tmp;
        memset(tmp.m,0,sizeof(tmp.m));
        for(int i=1;i<=M;i++)
            for(int j=1;j<=M;j++)
            {
                if(s[i][s[i].size()-1]==s[j][0])
                    tmp.m[i][j]=1;
            }
        //Print(tmp);
        for(int i=1;i<=M;i++)
        {
            if(s[i]==S)
                Si=i;
            if(s[i]==T)
                Ti=i;
        }
        if(!(N&1))
            N--;
        tmp=Sum(tmp,N);
        Matrix ans;
        memset(ans.m,0,sizeof(ans.m));
        ans.m[1][Si]=1;
        ans=last(ans,tmp,1,M,M);
        cout<<ans.m[1][Ti]%mod<<endl;
    }
    return 0;
}

解题转自:http://blog.csdn.net/shiyuankongbu/article/details/12429579