Hdu-5863 cjj's string game(矩阵快速幂)

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 题意:让你用k个字母构造出两个长度为n的a,b串,其中a,b连续对应子串的长度不能超过m,问方案数。 分析:dp[i][j]表示现在构造了i长度,长度j后缀连续对应相等,那么dp[i][j] = dp[i-1][j-1]*k,特别地,dp[i][0] = sigma(dp[i-1][j]*(k-1)*k) (0=<j<=m).
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
#define INF 0x3f3f3f3f
#define eps 1e-9
#define MOD 1000000007
#define size 20
using namespace std;
typedef long long ll;
ll n;
int T,m,k,num;
struct Matrix 
{
	ll val[size][size];
	int x,y;
	friend Matrix operator *(const Matrix a,const Matrix b)
	{
		Matrix c = {0};
		c.x = a.x,c.y = b.y;
		for(int i = 1;i <= a.x;i++)
		 for(int j = 1;j <= b.y;j++)
		  for(int k = 1;k <= b.x;k++)
		   c.val[i][j] = (c.val[i][j] + a.val[i][k]*b.val[k][j]) % MOD;
		return c; 
	}
};
Matrix ksm(Matrix a,ll b)
{
	Matrix c = a;
	b--;
	while(b)
	{
		if(b & 1) c = c*a;
		a = a*a;
		b>>=1; 
	}
	return c;
}
ll got(int m)
{
	Matrix now = {0};
	now.x = now.y = m+1;
	for(int i = 1;i <= m;i++) now.val[i+1][i] = num;
	for(int i = 1;i <= m+1;i++) now.val[i][m+1] = num*(num-1);	
	now = ksm(now,n);
	ll ans = 0;
	for(int i = 1;i <= m+1;i++) ans = (ans + now.val[m+1][i]) % MOD;
	return ans;
}
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		scanf("%I64d%d%d",&n,&m,&num);
		printf("%I64d\n",(got(m)-got(m-1)+MOD) % MOD);
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值