链接:http://acm.hdu.edu.cn/showproblem.php?pid=5318

The Goddess Of The Moon

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 438    Accepted Submission(s): 150

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. 
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
2
10 50
12 1213 1212 1313231 12312413 12312 4123 1231 3 131
5 50
121 123 213 132 321
 
Sample Output
86814837
797922656
Hint
11 111 is different with 111 11
 

题意:有n个小楼梯,假设两个楼梯的 前缀等于还有一个的后缀就能够首尾相连,前缀后缀长度要大于等于2。

问m个楼梯组成。有多少种组成方法。

做法:要去重,然后judge  每一个楼梯能不能连,构造出构造矩阵。初始矩阵第一行全为1,然后矩阵高速幂。

#include <cstdio>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <set>
#include <math.h>
#include <string.h>
#include <iostream>
using namespace std; #define Matr 55 //矩阵大小,注意能小就小 矩阵从1開始 所以Matr 要+1 最大能够100
#define ll __int64
struct mat//矩阵结构体。a表示内容,size大小 矩阵从1開始 但size不用加一
{
ll a[Matr][Matr];
mat()//构造函数
{
memset(a,0,sizeof(a));
}
};
int Size= 0 ;
ll mod= 1000000007; mat multi(mat m1,mat m2)//两个相等矩阵的乘法,对于稀疏矩阵。有0处不用运算的优化
{
mat ans=mat();
for(int i=1;i<=Size;i++)
for(int j=1;j<=Size;j++)
if(m1.a[i][j])//稀疏矩阵优化
for(int k=1;k<=Size;k++)
ans.a[i][k]=(ans.a[i][k]+m1.a[i][j]*m2.a[j][k])%mod; //i行k列第j项
return ans;
} mat quickmulti(mat m,ll n)//二分高速幂
{
mat ans=mat();
int i;
for(i=1;i<=Size;i++)ans.a[i][i]=1;
while(n)
{
if(n&1)ans=multi(m,ans);//奇乘偶子乘 挺好记的.
m=multi(m,m);
n>>=1;
}
return ans;
} void print(mat m)//输出矩阵信息,debug用
{
int i,j;
printf("%d\n",Size);
for(i=1;i<=Size;i++)
{
for(j=1;j<=Size;j++)
printf("%d ",m.a[i][j]);
printf("\n");
}
}
set<string> my; string str[60]; int judge(string a,string b)
{
for(int i=2;i<=a.size()&&i<=b.size();i++)
{
int flag=1;
for(int j=0;j<i;j++)
{
if(a[a.size()-i+j]!=b[j])
flag=0;
}
if(flag)
return 1;
}
return 0;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
int kk=0;
my.clear();
for(int i=0;i<n;i++)
{
string ss;
cin>>ss;
if(my.find(ss)==my.end())
{
my.insert(ss);
str[++kk]=ss;
}
}
n=kk;
if(m==0||n==0)
{
printf("0\n");
continue;
}
mat gouzao=mat(),chu=mat();//构造矩阵 初始矩阵 for(int i=1;i<=kk;i++)
{
for(int j=1;j<=kk;j++)
{
if(judge(str[i],str[j]))
gouzao.a[i][j]=1;
}
} for(int i=1;i<=kk;i++)
{
chu.a[1][i]=1;
}
Size=kk;
chu=multi(chu,quickmulti(gouzao,m-1)); __int64 ans=0;
for(int i=1;i<=kk;i++)
{
ans=(ans+chu.a[1][i])%mod;
} printf("%I64d\n",ans);
}
return 0;
}

hdu 5318 The Goddess Of The Moon 矩阵高速幂的更多相关文章

  1. hdu5318 The Goddess Of The Moon (矩阵高速幂优化dp)

    题目:pid=5318">http://acm.hdu.edu.cn/showproblem.php?pid=5318 题意:给定n个数字串和整数m,规定若数字串s1的后缀和数字串s2 ...

  2. hdu 3306 Another kind of Fibonacci(矩阵高速幂)

    Another kind of Fibonacci                                                        Time Limit: 3000/10 ...

  3. HDU 5318——The Goddess Of The Moon——————【矩阵快速幂】

    The Goddess Of The Moon Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  4. 2015 Multi-University Training Contest 3 hdu 5318 The Goddess Of The Moon

    The Goddess Of The Moon Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  5. hdu 5318 The Goddess Of The Moon

    The Goddess Of The Moon Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  6. HDU 1757 A Simple Math Problem(矩阵高速幂)

    题目地址:HDU 1757 最终会构造矩阵了.事实上也不难,仅仅怪自己笨..= =! f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 ...

  7. hdu 1757 A Simple Math Problem (矩阵高速幂)

    和这一题构造的矩阵的方法同样. 须要注意的是.题目中a0~a9 与矩阵相乘的顺序. #include <iostream> #include <cstdio> #include ...

  8. hdu 3221 Brute-force Algorithm(高速幂取模,矩阵高速幂求fib)

    http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序.问funny函数调用了多少次. 我们定义数组为所求 ...

  9. HDU 1575 Tr A(矩阵高速幂)

    题目地址:HDU 1575 矩阵高速幂裸题. 初学矩阵高速幂.曾经学过高速幂.今天一看矩阵高速幂,原来其原理是一样的,这就好办多了.都是利用二分的思想不断的乘.仅仅只是把数字变成了矩阵而已. 代码例如 ...

随机推荐

  1. Vue基础操作

    一.Vue入门基础知识 1.Vue使用的基本操作 i. 先下载,引入vue.jsii. Vue,实例化一个vue实例化对象(new Vue({})) 1. 新建一个vue实例化对象(Vue是一个构造函 ...

  2. PatentTips - Object-oriented processor architecture and operating method

    BACKGROUND OF THE INVENTION The present invention relates to processors and computer systems. More s ...

  3. POJ——T2421 Constructing Roads

    http://poj.org/problem?id=2421 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24132   ...

  4. $.ajax() 获取不到return 返回值

    /*常见错误示例 直接在 ajax 里面return 结果 */ function demo(){ $.ajax({ url : 'test.do', type : "post", ...

  5. Linux下基于多线程的echo

    准备开始写一些Linux 下网络编程以及多线程的blog,就从这个简单的echo程序开始吧. 在echo的服务端使用多线程与客户进行通信,可以实现一个服务端程序同时连接多个客户的功能.那么,到底在服务 ...

  6. HDU 2686 Matrix(最大费用最大流+拆点)

    题目链接:pid=2686">http://acm.hdu.edu.cn/showproblem.php?pid=2686 和POJ3422一样 删掉K把汇点与源点的容量改为2(由于有 ...

  7. android AudioManager AUDIOFOCUS

    如今開始做音乐播放器的模块.遇到了几个问题 当播放音乐的过程中,去调节音量或者情景模式中的铃声设置,结果会有两种声音同一时候响起. 引起此问题的解决办法是音乐焦点问题没弄清 现分析一下音乐焦点的几个属 ...

  8. 快学Scala习题解答—第三章 数组相关操作

    3 数组相关操作  3.1 编写一段代码.将a设置为一个n个随机整数的数组,要求随机数介于0(包括)和n(不包括)之间  random和yield的使用 import scala.math.rando ...

  9. 基于对话框的应用程序,点击button打开一个网页

    核心:使用Webbrowser控件 加入一个新的对话框,右键 Insert ActiveX control,选中 双击对话框生成响应的类(Web).并为webbrowser绑定成员变量(m_Web) ...

  10. 英语影视台词---八、the shawshank redemption

    英语影视台词---八.the shawshank redemption 一.总结 一句话总结:肖申克的救赎 1.It's funny. On the outside, I was an honest ...