HDU5863 cjj's string game (dp+矩阵快速幂)

cjj's string game

                                                                  Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
                                                                                           Total Submission(s): 275    Accepted Submission(s): 187


Problem Description
cjj has k kinds of characters the number of which are infinite. He wants to build two strings with the characters. The lengths of the strings are both equal to n. 

cjj also define a cjj_val for two string.
a[i,j] means the substring a[i],a[i+1],...,a[j-1],a[j] of string a.

cjj_val = max({ j-i+1 }) where a[i,j]=b[i,j] for every 0<=i<=j<n.

Know cjj wants to know that if he wants to build two strings with k different characters whose cjj_val is equal to m, how many ways can he do that.
 

Input
The first line of the input data is an integer T(1<=T<=100), means the number of test case.

Next T lines, each line contains three integers n(1<=n<=1000000000), m(1<=m<=10), k(1<=k<=26).
 

Output
For each test case, print one line, the number of the ways to build the string. The answer will be very large, you just need to output ans mod 1000000007.
 

Sample Input
  
  
2 3 2 3 3 3 3
 

Sample Output
  
  
108 27
 

Author
BUPT
 

Source

——————————————————————————————————

题目的意思是给出用k个字母构造长度为n的两个序列要求最长公共子串为m,求方案数

思路:dp,dp[i][j]表示前i为公共后缀长为j的方案数,那么dp[i][j]=dp[i-1][j-1]*k (j>0),而j等于0时 dp[i][0]=Σ(dp[i-1][j])*k*(k-1) (0≤j≤m) 

n较大矩阵加速


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>

using namespace std;

#define ll long long
const int INF = 0x3f3f3f3f;
#define mod 1000000007


int n,len;
struct Matrix
{
    ll x[15][15];
    Matrix()
    {
        memset(x,0,sizeof x);
    }
} ;

Matrix mul(Matrix c,Matrix d)
{
    Matrix sum;
    for(int i=0; i<=len; i++)
    {
        for(int j=0; j<=len; j++)
        {
            for(int k=0; k<=len; k++)
            {
                sum.x[i][j]=sum.x[i][j]+(c.x[i][k]*d.x[k][j])%mod;
                sum.x[i][j]%=mod;
            }
        }
    }
    return sum;
}

Matrix mypow(Matrix l,int m)
{
    Matrix sum;
    for(int i=0; i<=len; i++)
        sum.x[i][i]=1;
    while(m)
    {
        if(m&1) sum=mul(sum,l);
        m>>=1;
        l=mul(l,l);
    }
    return sum;
}

ll solve(int m,int k)
{
    Matrix k1,a,t;
    a.x[0][0]=1;
    for(int i=0; i<=m; i++)
    {
        k1.x[i][0]=k*(k-1);
        if(i<m)
            k1.x[i][i+1]=k;
    }
    len=m;
    t=mypow(k1,n);
    a=mul(a,t);
    ll ans=0;
    for(int i=0; i<=m; i++)
    {
        ans=(ans+a.x[0][i])%mod;
    }
    return ans;

}
int main()
{
    int m,k,T;
    for(scanf("%d",&T); T--;)
    {

        scanf("%d%d%d",&n,&m,&k);
        printf("%lld\n",((solve(m,k)-solve(m-1,k))%mod+mod)%mod);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值