hdu 5318 The Goddess Of The Moon(矩阵二分幂优化dp)

题目链接

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
 

Author
ZSTU
 

Source
题意:
第一行给你两个数n,m;(n<=50,m<=1e9)
第二行是n个数(不大于1e9的数),这种数每个都有若干个。
要求你从上面的数中选出m个数来接在一起形成串,问你可以弄成多少种不同的串,两个数字A和B可以连在一起的条件是:A的后缀等于B的前缀,其中前缀和后缀长度必须大于1。


分析:一看这种题就是矩阵二分幂优化的dp,果断无脑构造矩阵,开一个a[55][55]的矩阵,其中a[i][j]等于0或1,等于0表示第i个数不能和第j个数连在一起,等于1就是可以连在一起,然后将这个矩阵跑m-1次方就出来答案了,想想还是比较简单无脑的,但是有个要注意此题告诉的数字有可能有重复的,所以map去重就行了,还有直接省略掉长度为1的数字(因为不满足前缀后缀长度大于1)。

#include<stdio.h>
#include<string.h>
#include<map>
#include<algorithm>
#define mod 1000000007
#define ll long long
using namespace std;
ll a[55][55],b[55];
ll n,m;
struct node
{
    ll ar[55][55];
};
node multi(node x,node y)
{
    node tem;
    for(ll i=1;i<=n;i++)
    {
        for(ll j=1;j<=n;j++)
        {
            tem.ar[i][j]=0;
            for(ll k=1;k<=n;k++)
            tem.ar[i][j]=(tem.ar[i][j]+x.ar[i][k]*y.ar[k][j])%mod;
        }
    }
    return tem;
}
node get(ll num)
{
    node ans,tem;
    memcpy(tem.ar,a,sizeof(tem.ar));
    for(ll i=1;i<=n;i++)
        for(ll j=1;j<=n;j++)
        ans.ar[i][j]=i==j?1:0;
    while(num)
    {
        if(num&1) ans=multi(ans,tem);
        num>>=1;
        tem=multi(tem,tem);
    }
    return ans;
}
bool ok(ll x,ll y)///判断b[x]的一个后缀是否和b[y]的前缀相同
{
    ll cx[15],cy[15];
    ll numx=b[x],numy=b[y];
    ll lx=0,ly=0;
    while(numx)
    {
        cx[++lx]=numx%10;
        numx/=10;
    }
    while(numy)
    {
        cy[++ly]=numy%10;
        numy/=10;
    }
    for(ll i=2;i<=min(lx,ly);i++)
    {
        bool flog=true;
        for(ll j=i,k=ly;j>=1;j--,k--)
        {
            if(cx[j]!=cy[k])
            {
                flog=false;
                break;
            }
        }
        if(flog) return true;
    }
    return false;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld%lld",&n,&m);
        ll cnt=0;
        map<ll,ll> mm;
        for(ll i=1;i<=n;i++)
        {
            ll num;
            scanf("%lld",&num);
            if(num<10) continue;
            if(!mm[num])
            {
                cnt++;
                b[cnt]=num;
                mm[num]++;
            }
        }
        n=cnt;
        memset(a,0,sizeof(a));
        for(ll i=1;i<=n;i++)
        {
            for(ll j=1;j<=n;j++)
            {
                if(i==j)
                {
                    a[i][j]=1;
                    continue;
                }
                if(ok(i,j)) a[i][j]=1;
            }
        }
        node A=get(m-1);
        ll ans=0;
        for(ll i=1;i<=n;i++)
            for(ll j=1;j<=n;j++)
            ans=(ans+A.ar[i][j])%mod;
        printf("%lld\n",ans);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值