The Goddess Of The Moon

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 589    Accepted Submission(s): 251

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
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

 

题目大意:有t组数据。每组数据有n,m。分别表示有n种类型的字串(可能重复),每种字串无限多个。问你如果从这些字串中挑出m个连成一串。问能形成多少种字串。能连接的要求是前边那个串的后缀跟后边那个串的前缀重复最少2个字符。(连接成新的字串时直接连接,不用重叠)。

解题思路:定义dp[i][j]表示选出前i个字串以j字串为结尾的种数。首先定义a[i][j]表示j字串可以连接在i字串后边。dp[i][j]+=dp[i][k]*a[k][j] (1<=k<=n)。然后用矩阵快速幂去优化矩阵相乘。

#include<stdio.h>
#include<string.h>
#include<string>
#include<set>
#include<algorithm>
using namespace std;
const int MOD=1000000007;
typedef long long INT;
int n,m;
char str[55][12];
struct Matrix{
int a[55][55];
Matrix(){
memset(a,0,sizeof(a));
}
void init(){
for(int i=1;i<=n;i++)
a[1][i]=1;
}
Matrix operator *(Matrix &X)const {
Matrix ret;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
for(int k=1;k<=n;k++){
ret.a[i][j]=(ret.a[i][j]%MOD+((INT)a[i][k]*X.a[k][j])%MOD)%MOD;
}
}
}
return ret;
}
};
Matrix &Pow(Matrix &ret,Matrix a,int x){
while(x){
if(x&1){
ret=ret*a;
}
x>>=1;
a = a * a;
}
return ret;
}
bool check(int x,int y){//检查是否能连接
int lenx=strlen(str[x]),leny=strlen(str[y]);
if(lenx==1||leny==1)
return 0;
for(int i=lenx-2;i>=0;i--){
int ii=i,jj=0;
while(ii<lenx&&jj<leny&&str[x][ii]==str[y][jj]){
ii++,jj++;
}
if(ii==lenx){
return 1;
}
}
return 0;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
Matrix A,B;
scanf("%d%d",&n,&m);
set<string>ST;
ST.clear();
for(int i=1;i<=n;i++){
scanf("%s",str[i]);
ST.insert(str[i]);
}
n=0;
for(set<string>::iterator it=ST.begin();it!=ST.end();it++){
strcpy(str[++n],(*it).c_str());
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(check(i,j))
A.a[i][j]=1;
}
}
B.init();
Pow(B,A,m-1);
int res=0;
for(int i=1;i<=n;i++){
res=(res%MOD+B.a[1][i]%MOD)%MOD;
}
printf("%d\n",res); }
return 0;
}

  

下面这个代码时间快得很。

#include<bits/stdc++.h>
using namespace std;
const int MOD=1000000007;
typedef long long INT;
int n,m;
char str[55][12];
struct Matrix{
int a[55][55];
Matrix(){
memset(a,0,sizeof(a));
}
void init(){
for(int i=1;i<=n;i++)
a[1][i]=1;
}
Matrix operator *(Matrix &X)const {
Matrix ret;
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
if(a[i][k])
for(int j=1;j<=n;j++){
if(X.a[k][j])
ret.a[i][j]=(ret.a[i][j]%MOD+((INT)a[i][k]*X.a[k][j])%MOD)%MOD;
}
}
}
return ret;
}
};
Matrix &Pow(Matrix &ret,Matrix a,int x){
// while(x){
// if(x&1){
// ret=ret*a;
// }
// x>>=1;
// a = a * a;
// }
for(;x;x>>=1,a=a*a)
if(x&1)
ret=ret*a;
return ret;
}
bool check(int x,int y){
int lenx=strlen(str[x]),leny=strlen(str[y]);
if(lenx==1||leny==1)
return 0;
for(int i=lenx-2;i>=0;i--){
int ii=i,jj=0;
while(ii<lenx&&jj<leny&&str[x][ii]==str[y][jj]){
ii++,jj++;
}
if(ii==lenx){
return 1;
}
}
return 0;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
Matrix A,B;
scanf("%d%d",&n,&m);
set<string>ST;
ST.clear();
for(int i=1;i<=n;i++){
scanf("%s",str[i]);
ST.insert(str[i]);
}
n=0;
for(set<string>::iterator it=ST.begin();it!=ST.end();it++){
strcpy(str[++n],(*it).c_str());
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(check(i,j))
A.a[i][j]=1;
}
}
B.init();
Pow(B,A,m-1);
int res=0;
for(int i=1;i<=n;i++){
res=(res%MOD+B.a[1][i]%MOD)%MOD;
}
printf("%d\n",res); }
return 0;
}

  

HDU 5318——The Goddess Of The Moon——————【矩阵快速幂】的更多相关文章

  1. hdu 5318 The Goddess Of The Moon 矩阵高速幂

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5318 The Goddess Of The Moon Time Limit: 6000/3000 MS ( ...

  2. 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/ ...

  3. HDU 1757 A Simple Math Problem(矩阵快速幂)

    题目链接 题意 :给你m和k, 让你求f(k)%m.如果k<10,f(k) = k,否则 f(k) = a0 * f(k-1) + a1 * f(k-2) + a2 * f(k-3) + …… ...

  4. HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  5. (hdu 6030) Happy Necklace 找规律+矩阵快速幂

    题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=6030 Problem Description Little Q wants to buy a nec ...

  6. hdu 2604 Queuing dp找规律 然后矩阵快速幂。坑!!

    http://acm.hdu.edu.cn/showproblem.php?pid=2604 这题居然O(9 * L)的dp过不了,TLE,  更重要的是找出规律后,O(n)递推也过不了,TLE,一定 ...

  7. hdu 4291 2012成都赛区网络赛 矩阵快速幂 ***

    分析:假设g(g(g(n)))=g(x),x可能非常大,但是由于mod 10^9+7,所以可以求出x的循环节 求出x的循环节后,假设g(g(g(n)))=g(x)=g(g(y)),即x=g(y),y也 ...

  8. hdu 1757 A Simple Math Problem (矩阵快速幂,简单)

    题目 也是和LightOJ 1096 和LightOJ 1065 差不多的简单题目. #include<stdio.h> #include<string.h> #include ...

  9. 2017ACM暑期多校联合训练 - Team 2 1006 HDU 6050 Funny Function (找规律 矩阵快速幂)

    题目链接 Problem Description Function Fx,ysatisfies: For given integers N and M,calculate Fm,1 modulo 1e ...

随机推荐

  1. GeoServer中GeoWebCache(GWC)的使用

    本文介绍GeoWebCache的使用方法,包括如何切缓存,访问缓存wms/wmts服务,如何复用栅格缓存等. 文章大部分内容转载自https://www.cnblogs.com/naaoveGIS/p ...

  2. asp.net core 外部认证多站点模式实现

    PS:之前因为需要扩展了微信和QQ的认证,使得网站是可以使用QQ和微信直接登录.github 传送门 .然后有小伙伴问,能否让这个配置信息(appid, appsecret)按需改变,而不是在 Con ...

  3. Docker - 使用 Nexus3 搭设私有 NuGet 仓库

    目录 前言 NuGet Nexus 私有仓库 说明 安装 Nexus 拉取 Nexus 镜像 运行 Nexus Nexus NuGet 仓库简单使用 Nexus 默认帐号 Repositories 上 ...

  4. 190310HTML&CSS&JS

    一.HTML 1.web server import socket def handle_request(client): buf = client.recv(1024) client.send(by ...

  5. 2、Numpy常用函数

    创建单位矩阵和读写文件使用eye()创建单位矩阵 # -*- coding: utf-8 -*- import numpy as np i = np.eye(3) print(i) 结果: [[ 1. ...

  6. 洛谷 P3381【模板】最小费用最大流

    题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行包含四个正整数N.M.S.T,分别表 ...

  7. sharepoint_study_1

    描述:机器上进行SharePoint开发,需要SQL Server提供最基本的服务 解决: SQL Server 的数据库引擎: SQL Server 代理: SQL Server 浏览器组件:

  8. CH2401 送礼物 双向搜索

    双向搜索:把前一半的可行状态搜出来,然后sort+unique,之后搜后一半时,结束时二分一下前一半的答案,拼出一个与W尽量接近的ans来更新 ps:距LYD说前一半取n/2+2时跑的最快...不知, ...

  9. Trailing Loves (or L'oeufs?)

    The number "zero" is called "love" (or "l'oeuf" to be precise, literal ...

  10. 江西理工大学南昌校区排名赛 E: 单身狗的种树游戏

    题目描述 萌樱花是一只单身狗. 萌樱花今天决定种树,于是他来到了自己家的后院. 萌樱花的后院有n个树坑,所有树坑排列在一行上,每一个树坑都可以种一棵树,相邻树坑间的距离为1,现在所有的树坑都是空着的. ...