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/Others)
Total Submission(s): 547    Accepted Submission(s): 221


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
 

Source
 


题目大意:将地球和月球都看做一个点,给你n条短链。对于两条短链A和B(按照数字串给出,可以看做数字组成的字符串),如果短链A的后缀(超过一个字符)等于短链B的前缀【简单的说就是字符串A的后xx>1)个字符与字符串B的前x(x>1)个字符是一样的】,那么短链B就可以连接在短链A后面,形成一个新链,该新链的前后同样可以按条件连接短链。问选取m个链相连,可以由多少种不同的连接方法???


解题思路:给出的n条链不一定都是不一样的。换句话说,n条链是有可能重复的。因此首先要去重,得到具体有多少种链可以用。尽管已经去重了,但是这些短链还是不能直接使用,因为并不是每条链都可以和其他链相连。因此要继续处理出每一条链可以和那些链相连。处理过这个问题之后,该如何解决呢?先举个例子来看看。


例如:

假设n=5条短链去重后剩余k=3种短链(编号1,2,3),判断那些可以相连后为12,13,32,11,22,33可相连,问选取m=4条短链连接的方案数?

当m=1时,有1,2,3即3种,其中以1结尾的有1,以2结尾的有1,以3结尾的有1

当m=2时,有11,12,13,22,32,33即6种,其中以1结尾的有1,以2结尾的有3,以3结尾的有2

当m=3时,有111,112,113,122,132,133,222,322,332,333即10种,其中以1结尾的有1,以2结尾的有6,以3结尾的有3

当m=4时,有1111,1112,1113,1122,1132,1133,1222,1322,1332,1333,2222,3222,3322,3332,3333即15种,其中以1结尾的有1,以2结尾的有10,以3结尾的有5;


如果n条短链中选取m条短链,问有多少种连接方案?

设a[i][m]表示m条链相连并且以短链编号为i结尾的连接方案数,那么a[1][m-1]+a[2][m-1]+……+a[k][m-1]=a[i][m]。

我们构造一个K*K的矩阵A,A[i][j]的值表示j链能否连接在i链后面(1表示可以,0表示不可以),那么我们会得到下面的递推公式:




则最终的求解公式为:





因此,我们只要运用矩阵快速幂求出矩阵后,将第一行的值加和就是要求的结果了。




代码如下:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#include <limits.h>
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-4)
#define inf (1<<28)
#define sqr(x) (x) * (x)
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
#define MAX 55
//矩阵类型的结构体
struct matrix
{
    long long a[MAX][MAX];
    //初始化矩阵的值
    matrix()
    {
        memset(a,0,sizeof(a));
    }
};
//矩阵的乘法:N行N列的方阵m_a与m_b相乘
matrix multiplication(matrix m_a,matrix m_b,int N)
{
    matrix s;
    int i,j,k;
    //
    for(i=1;i<=N;i++)
    {
        for(j=1;j<=N;j++)
        {
            //稀疏矩阵的优化,0元素不必乘
            if(m_a.a[i][j])
            {
                for(k=1;k<=N;k++)
                {
                    s.a[i][k]=(s.a[i][k]+m_a.a[i][j]*m_b.a[j][k])%mod;
                }
            }
        }
    }
    return s;
}
//矩阵快速幂(二分快速幂)
matrix quickmulti(matrix m_a,int N,long long n)
{
    matrix s;
    int i;
    //构造单位矩阵
    for(i=1;i<=N;i++)
        s.a[i][i]=1;
    //二分快速幂
    while(n)
    {
        //奇乘偶子乘
        if(n&1)
            s=multiplication(m_a,s,N);
        m_a=multiplication(m_a,m_a,N);
        n>>=1;
    }
    return s;
}
//判断短链b能否连在短链a后面
int judge(string a,string b)
{
    int i,j,flag;
    //
    for(i=2;i<=a.size()&&i<=b.size();i++)
    {
        flag=1;
        for(j=0;j<i;j++)
        {
            if(a[a.size()-i+j]!=b[j])
                flag=0;
        }
        if(flag)
            return 1;
    }
    return 0;
}

int main()
{
    int i,j,k,n,m,t;
    //储存n条链
    set<string> chain;
    //储存不重复的k种链
    string str[60];

    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        //去重处理
        chain.clear();
        k=0;
        for(i=0;i<n;i++)
        {
            string s;
            cin>>s;
            if(chain.find(s)==chain.end())
            {
                chain.insert(s);
                str[++k]=s;
            }
        }
        //特判
        if(m==0||n==0)
        {
            printf("0\n");
            continue;
        }
        //定义构造矩阵structure_matrix,初始矩阵initial_matrix
        matrix structure_matrix,initial_matrix;
        //初始化构造矩阵
        for(i=1;i<=k;i++)
        {
            for(j=1;j<=k;j++)
            {
                //
                if(judge(str[i],str[j]))
                    structure_matrix.a[i][j]=1;
            }
        }
        //初始化初始矩阵
        for(i=1;i<=k;i++)
        {
            initial_matrix.a[1][i]=1;
        }
        //矩阵快速幂
        initial_matrix=multiplication(initial_matrix,quickmulti(structure_matrix,k,m-1),k);
        //计算结果
        long long sum=0;
        for(i=1;i<=k;i++)
        {
            sum=(sum+initial_matrix.a[1][i])%mod;
        }
        printf("%I64d\n",sum);
    }
    return 0;
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值