HDU 5318 The Goddess Of The Moon(矩阵快速幂详解)

题目链接:?pid=5318

题面:

The Goddess Of The MoonTime Limit: 6000/3000 MS (Java/Others)Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 800Accepted Submission(s): 349

Problem Description

Chang’e (嫦娥) is a well-known character in Chinese ancient mythology. She’s the goddess of the Moon. There are many tales about Chang’e, but there’s a well-known story regarding the origin of the Mid-Autumn Moon Festival. In a very distant past, ten suns had risen together to the heavens, thus causing hardship for the people. The archer Yi shot down nine of them and was given the elixir of immortality as a reward, but he did not consume it as he did not want to gain immortality without his beloved wife Chang’e.

However, while Yi went out hunting, Fengmeng broke into his house and forced Chang’e to give up the elixir of immortality to him, but she refused to do so. Instead, Chang’e drank it and flew upwards towards the heavens, choosing the moon as residence to be nearby her beloved husband.

Yi discovered what had transpired and felt sad, so he displayed the fruits and cakes that his wife Chang’e had liked, and gave sacrifices to her. Now, let’s help Yi to the moon so that he can see his beloved wife. Imagine the earth is a point and the moon is also a point, there are n kinds of short chains in the earth, each chain is described as a number, we can also take it as a string, the quantity of each kind of chain is infinite. The only condition that a string A connect another string B is there is a suffix of A , equals a prefix of B, and the length of the suffix(prefix) must bigger than one(just make the joint more stable for security concern), Yi can connect some of the chains to make a long chain so that he can reach the moon, but before he connect the chains, he wonders that how many different long chains he can make if he choose m chains from the original chains.

Input

The first line is an integer T represent the number of test cases.Each of the test case begins with two integers n, m. (n <= 50, m <= 1e9)The following line contains n integer numbers describe the n kinds of chains.All the Integers are less or equal than 1e9.

Output

Output the answer mod 1000000007.

Sample Input

210 5012 1213 1212 1313231 12312413 12312 4123 1231 3 1315 50121 123 213 132 321

Sample Output

86814837797922656

Hint

11 111 is different with 111 11

Author

ZSTU

Source

Recommend

wange2014|We have carefully selected several similar problems for you:53385337533453335332

解题: 比赛的时候,都没看到这道题,以为超级难,过的人很少,其实就是道矩阵快速幂,不过也有可能过不了,因为有个坑点,串可能重复,所以要去重。 离散中的可达矩阵B(i,j)的意义是,通过路径长为x,,能够从i点到j点,通过长度为x的路径到达的方式有B(i,j)种。m为1时,需特判,方案数为不重复的串的数量。其余情况m–,通过矩阵快速幂求去重后矩阵的(m-1)次,并累加结果即可。代码:#include <iostream>#include <cstdio>#include <cstring>#define maxn 55#define mod 1000000007using namespace std;int n,len[55],cnt;struct Matrix{int map[maxn][maxn];};//单位矩阵和计算矩阵Matrix unit,refl;int min(int a,int b){return a<b?a:b;}//单位矩阵void init(){memset(unit.map,0,sizeof(unit.map));for(int i=0;i<maxn;i++)unit.map[i][i]=1;}//矩阵乘Matrix mult(Matrix a,Matrix b){int i,j,k;Matrix c;for(i=0;i<cnt;i++)for(j=0;j<cnt;j++){c.map[i][j]=0;for(k=0;k<cnt;k++)c.map[i][j]=(c.map[i][j]+1LL*a.map[i][k]*b.map[k][j])%mod;}return c;}//快速幂Matrix quick_pow(int x){ Matrix tmp=refl,res=unit; while(x) { //二进制,取或不取 if(x&1)res=mult(res,tmp); x=x>>1; tmp=mult(tmp,tmp); } return res;}int main(){char store[55][55],temp[55][55];int t,m,l1,l2,ans;init();scanf("%d",&t);Matrix ansM;while(t–){ //读入 memset(refl.map,0,sizeof(refl.map)); memset(ansM.map,0,sizeof(ansM.map));scanf("%d%d",&n,&m); cnt=0; //去重 for(int i=0;i<n;i++) {scanf("%s",temp[i]);bool sign=true;for(int j=0;j<i;j++){if(strcmp(temp[j],temp[i])==0){sign=false;break;}}if(sign){int j;for(j=0;j<strlen(temp[i]);j++){store[cnt][j]=temp[i][j];}//这里忘记加串结束符,郁闷了我好久….store[cnt][j]=0;cnt++;} } for(int i=0;i<cnt;i++)len[i]=strlen(store[i]); //特判 if(m==1) {printf("%d\n",cnt);continue; } //处理能否连接关系 for(int i=0;i<cnt;i++) {if(len[i]>1){for(int j=0;j<cnt;j++){if(len[j]>1){if(i==j){refl.map[i][i]=1;continue;}l1=len[i];l2=len[j];bool flag;for(int k=2;k<=min(l1,l2);k++){flag=true;for(int z=1;z<=k;z++){if(store[i][l1-z]!=store[j][k-z]){flag=false;break;}}if(flag)break;}if(flag)refl.map[i][j]=1;}}} }m–; //快速幂ansM=quick_pow(m); ans=0; //累加 for(int i=0;i<cnt;i++) {for(int j=0;j<cnt;j++){ans=(ans+ansM.map[i][j])%mod;} } printf("%d\n",ans);}return 0;}

版权声明:本文为博主原创文章,未经博主允许不得转载。

人生并不在于获取,更在于放得下。放下一粒种子,收获一棵大树;

HDU 5318 The Goddess Of The Moon(矩阵快速幂详解)

相关文章:

  • 【算法】直接插入排序C语言实现
  • 嵌入式 FAAC1.28 在海思HI3518C/HI3518A平台linux中的编译优化
  • Android 动画animation 深入分析
  • Mybatis极其(最)简(好)单(用)的一个分页插件
  • Ext JS Kitchen Sink [Learning by doing](2)ArrayGrid
  • API开发第三篇:PHP的设计模式之完美的单例模式
  • 你感兴趣的文章:

    标签云:

    亚洲高清电影在线, 免费高清电影, 八戒影院夜间, 八戒电影最新大片, 出轨在线电影, 午夜电影院, 在线影院a1166, 在线电影院, 在线观看美剧下载, 日本爱情电影, 日韩高清电影在线, 电影天堂网, 直播盒子app, 聚合直播, 高清美剧, 高清美剧在线观看 EhViewer-E站, E站, E站绿色版, qqmulu.com, qq目录网, qq网站目录,