专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Go

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

发布时间:2011-06-29 18:10:19 文章来源:www.iduyao.cn 采编人员:星星草
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 (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;
} 




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

友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

  • ModernUI课程:定义一个Logo

    ModernUI教程:定义一个Logo ModernWindow的标题栏包含了一块区域用来显示自定义的窗体Logo: 这个窗体logo通过ModernWindow.LogoD...

  • Django忘记管理员账号和密码的解决方法

    Django忘记管理员账号和密码的解决办法 看着Django的教程学习搭建网站,结果忘记第一次创建的账号和密码了。结果搭建成功以后,一直...

  • GO语言小结(1)——基本知识

    GO语言总结(1)——基本知识 1、注释(与C++一样)   行注释://  块注释:/*   ...  */ 2、标识符   可以这么说,除了数字开头...

  • golang 惯用的文件读取方式

    golang 常用的文件读取方式 Golang 的文件读取方法很多,刚上手时不知道怎么选择,所以贴在此处便后速查。 一次性读取 小文件推荐一...

  • 查询深圳市通相关信息

    查询深圳通相关信息 用 HTTP.GET 从开放 API 中查询深圳通信息,然后将 JSON 数据存入结构体中,再格式化输出。 注意:获取的并不是实...

  • Go语言设计模式实践:结合(Composite)

    Go语言设计模式实践:组合(Composite) 关于本系列 这个系列首先是关于Go语言实践的。在项目中实际使用Go语言也有段时间了,一个体会就...

  • 列出索引和遍历目录

    列出目录和遍历目录 获取目录列表用 ioutil.ReadDir(),遍历目录用 filepath.Walk(),使用方法请参考文章示例。 示例代码: package ma...

  • io 包的惯用接口速记

    io 包的常用接口速记 我没有 C/C++ 基础,没有接口的概念,且从 Python 投奔而来,Python 的极简主义(一个结果往往只提供一个方法),让我在...

  • 代理服务扩充

    代理服务扩展 之前自己实现了一个代理服务,当时考虑的是只要支持SOCKS5就好了,因为我经常用CHROME,配合着SwitchySharp,体验还是很棒...

  • 文件的创造与打开

    文件的创建与打开 文件操作是个很重要的话题,使用也非常频繁,熟悉如何操作文件是必不可少的。Golang 对文件的支持是在 os package ...

热门推荐: