zoukankan      html  css  js  c++  java
  • 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): 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
    ",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
    ",res);
    
        }
        return 0;
    }
    

      

  • 相关阅读:
    SVG Stroke属性
    C# 线程同步之排它锁/Monitor监视器类
    在Mac OS X Yosemite 10.10.3 中搭建第一个 ASP.NET 5 Web 项目
    jquery 之 Deferred 使用与实现
    jQuery 之 Callback 实现
    在解决方案中所使用 NuGet 管理软件包依赖
    下载和使用 Open XML PowerTools
    下载和编译 Open XML SDK
    Open XML SDK 在线编程黑客松
    VS2013 解决方案文件结构分析
  • 原文地址:https://www.cnblogs.com/chengsheng/p/4686188.html
Copyright © 2011-2022 走看看