标签:


2 10 50 12 1213 1212 1313231 12312413 12312 4123 1231 3 131 5 50 121 123 213 132 321
86814837 797922656Hint11 111 is different with 111 11
/* ***********************************************
Author :CKboss
Created Time :2015年07月29日 星期三 09时55分36秒
File Name :HDOJ5318.cpp
************************************************ */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long int LL;
const LL mod=1000000007;
int n,m;
string str[55];
bool check(int a,int b)
{
if(a==b) return true;
int n1=str[a].length();
int n2=str[b].length();
for(int i=2,ml=min(n1,n2);i<=ml;i++)
{
int p1=n1-i,p2=0;
bool flag=true;
for(int j=0;j<i&&flag;j++,p1++,p2++)
{
if(str[a][p1]==str[b][p2]) continue;
else flag=false;
}
if(flag==true) return flag;
}
return false;
}
struct Matrix
{
LL m[52][52];
Matrix()
{
memset(m,0,sizeof(m));
}
void getE()
{
for(int i=0;i<n;i++) m[i][i]=1;
}
void toString()
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
cout<<m[i][j]<<" ";
cout<<endl;
}
cout<<endl;
}
};
Matrix Muilt(Matrix a,Matrix b)
{
Matrix mat;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
LL temp=0;
for(int k=0;k<n;k++)
{
temp=(temp+(a.m[i][k]*b.m[k][j])%mod)%mod;
}
mat.m[i][j]=temp;
}
}
return mat;
}
Matrix Pow(Matrix& m,int k)
{
Matrix e;
e.getE();
while(k)
{
if(k&1) e=Muilt(e,m);
m=Muilt(m,m);
k/=2;
}
return e;
}
LL Solve(Matrix& e)
{
LL ans=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
ans=(ans+e.m[i][j])%mod;
}
}
return ans;
}
set<string> ss;
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T_T;
scanf("%d",&T_T);
while(T_T--)
{
scanf("%d%d",&n,&m);
ss.clear();
int kut=0;
string sss;
for(int i=0;i<n;i++)
{
cin>>sss;
if(sss.length()<2) continue;
if(ss.count(sss)==0)
{
ss.insert(sss);
str[kut++]=sss;
}
}
n=kut;
Matrix mt;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(check(i,j)==true) mt.m[i][j]=1;
}
}
Matrix e = Pow(mt,m-1);
cout<<Solve(e)<<endl;;
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDOJ 5318 The Goddess Of The Moon 矩阵快速幂
标签:
原文地址:http://blog.csdn.net/ck_boss/article/details/47127839