cjj's string game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 334 Accepted Submission(s): 215
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.
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).
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
Recommend
wange2014
题意:用k个不同的字母,有多少种方法构造出两个长度n最长公共子串长度为m的字符串。
解题思路:用DP解决可以这么表示状态:dp[i][j]表示两个字符串前i个字符都构造好了并且它们后面的j个字符相同的方案数,状态的转移就是,末尾j个相同的可以转移到0个相同的也能转移到j+1个相同的(前提是j<m)。因为m只有10,并且n很大,所以可以用矩阵快速幂来解决,最后求出来的矩阵的第一行就是长度n的字符串末尾各个情况的方案数。不过样表示状态最后求出来不是要求的,因为LCS小于m的也会包含于其中。那么减去小于m的方案数就好了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
const LL mod=1000000007;
int n,m,k;
struct Matrix
{
LL v[15][15];
Matrix()
{
memset(v,0,sizeof v);
}
} dan;
Matrix mul(Matrix a,Matrix b,int d)
{
Matrix ans;
for(int i=0; i<d; i++)
{
for(int j=0; j<d; j++)
{
for(int k=0; k<d; k++)
{
ans.v[i][j]+=(a.v[i][k]*b.v[k][j])%mod;
ans.v[i][j]%=mod;
}
}
}
return ans;
}
int pow(Matrix a,int k,int d)
{
Matrix ans=dan;
while(k)
{
if(k&1) ans=mul(ans,a,d);
k>>=1;
a=mul(a,a,d);
}
int sum=0;
for(int i=0;i<d;i++) sum=(sum+ans.v[0][i])%mod;
return sum;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d %d %d",&n,&m,&k);
dan.v[0][0]=1;
Matrix a;
for(int i=1;i<=m+1;i++) a.v[i-1][i]=k,a.v[i-1][0]=k*(k-1);
int ans1=pow(a,n,m+1);
int ans2=pow(a,n,m);
printf("%lld\n",((ans1-ans2%mod)+mod)%mod);
}
return 0;
}