bnuoj 34985 Elegant String DP+矩阵快速幂
题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
typedef long long ll;
const int maxn=;
const ll mod=; ll n,k;
struct matrix
{
ll an[maxn][maxn];
}A,B;
matrix multiply(matrix x,matrix y)
{
matrix sum;
memset(sum.an,,sizeof(sum.an));
for (int i= ;i<=k ;i++)
{
for (int j= ;j<=k ;j++)
{
for (int k2= ;k2<=k ;k2++) {
sum.an[i][j]=(sum.an[i][j]+x.an[i][k2]*y.an[k2][j]%mod);
if (sum.an[i][j]>=mod) sum.an[i][j] -= mod;
}
}
}
return sum;
}
matrix power(ll K,matrix q)
{
matrix temp;
for (int i= ;i<=k ;i++)
{
for (int j= ;j<=k ;j++)
temp.an[i][j]= i==j ;
}
while (K)
{
if (K&) temp=multiply(temp,q);
q=multiply(q,q);
K >>= ;
}
return temp;
}
int main()
{
int t,ncase=;
scanf("%d",&t);
while (t--)
{
scanf("%lld%lld",&n,&k);
if (n==)
{
printf("Case #%d: %d\n",ncase++,k+);continue;
}
matrix q;
for (int i= ;i<=k ;i++)
{
for (int j= ;j<=k ;j++)
{
if (j>=i) q.an[i][j]=(ll);
else if (j==i-) q.an[i][j]=(ll)(k+-i);
else q.an[i][j]=(ll);
}
}
q=power(n-,q);
// for (int i=1 ;i<=k ;i++)
// {
// for (int j=1 ;j<=k ;j++)
// cout<<q.an[i][j]<<" ";
// cout<<endl;
// }
ll ans=;
for (int i= ;i<=k ;i++)
{
ans=(ans+(ll)q.an[i][]*(ll)(k+))%mod;
}
printf("Case #%d: %lld\n",ncase++,ans);
}
return ;
}
bnuoj 34985 Elegant String DP+矩阵快速幂的更多相关文章
- HDU 5434 Peace small elephant 状压dp+矩阵快速幂
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5434 Peace small elephant Accepts: 38 Submissions: ...
- 【BZOJ】2004: [Hnoi2010]Bus 公交线路 状压DP+矩阵快速幂
[题意]n个点等距排列在长度为n-1的直线上,初始点1~k都有一辆公车,每辆公车都需要一些停靠点,每个点至多只能被一辆公车停靠,且每辆公车相邻两个停靠点的距离至多为p,所有公车最后会停在n-k+1~n ...
- 【BZOJ】4861: [Beijing2017]魔法咒语 AC自动机+DP+矩阵快速幂
[题意]给定n个原串和m个禁忌串,要求用原串集合能拼出的不含禁忌串且长度为L的串的数量.(60%)n,m<=50,L<=100.(40%)原串长度为1或2,L<=10^18. [算法 ...
- BZOJ5298 CQOI2018 交错序列 【DP+矩阵快速幂优化】*
BZOJ5298 CQOI2018 交错序列 [DP+矩阵快速幂优化] Description 我们称一个仅由0.1构成的序列为"交错序列",当且仅当序列中没有相邻的1(可以有相邻 ...
- Codeforces 621E Wet Shark and Block【dp + 矩阵快速幂】
题意: 有b个blocks,每个blocks都有n个相同的0~9的数字,如果从第一个block选1,从第二个block选2,那么就构成12,问对于给定的n,b有多少种构成方案使最后模x的余数为k. 分 ...
- codeforces E. Okabe and El Psy Kongroo(dp+矩阵快速幂)
题目链接:http://codeforces.com/contest/821/problem/E 题意:我们现在位于(0,0)处,目标是走到(K,0)处.每一次我们都可以从(x,y)走到(x+1,y- ...
- [BZOJ1009] [HNOI2008] GT考试(KMP+dp+矩阵快速幂)
[BZOJ1009] [HNOI2008] GT考试(KMP+dp+矩阵快速幂) 题面 阿申准备报名参加GT考试,准考证号为N位数X1X2-.Xn,他不希望准考证号上出现不吉利的数字.他的不吉利数学A ...
- poj4474 Scout YYF I(概率dp+矩阵快速幂)
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4100 Accepted: 1051 Descr ...
- hihocoder第42周 3*N骨牌覆盖(状态dp+矩阵快速幂)
http://hihocoder.com/contest/hiho42/problem/1 给定一个n,问我们3*n的矩阵有多少种覆盖的方法 第41周做的骨牌覆盖是2*n的,状态转移方程是dp[i] ...
随机推荐
- 在Array原型链上扩展remove,contain等方法所遇到的坑
相信jser兄弟们肯定会碰到这样一个问题, 在做数组类的操作的时候,会要求删除数组中的一个元素:亦或是判断某值是否存在于这个数组: OK,拿删除数组元素举例,扩展方法为: Array.prototyp ...
- C# 修改IE 源代码参照样例
using Microsoft.Win32; using System; using System.Collections.Generic; using System.ComponentModel; ...
- DELPHI 使用dbexpress控件连接MySQL数据库方法
1. 在窗体上放置4个控件: SQLConnection1,SimpleDataSet1,DataSource1,DBGrid (注意: 如果 出现"Operation not allowe ...
- 添加常驻Notification
private static final int NOTIFICATION_ID=250; //用来标示notification,通过notificatinomanager来发布同样标示的notifi ...
- WPF优化体验<一>
最近将一个开发和维护了五年的一个Winform项目进行重构,考虑到最近很流行将用户体验挂在嘴上,于是采用了WPF技术,希望能在外观和体验上有一个全新的效果. 以前使用Winform的时候内存控制得不错 ...
- hdu 4198 Quick out of the Harbour
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4198 Quick out of the Harbour Description Captain Cle ...
- hdu 1212 Big Number
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1212 Big Number Description As we know, Big Number is ...
- Power Map 入门
Excel 的 Microsoft Power Map是三维 (3-D) 数据的可视化工具,允许您以新的方式看信息.电源映射允许您发现您可能看不到传统的二维 (2-d) 表和图中的见解. 使用Powe ...
- MEF(Managed Extensibility Framework) 微软平台插件化开发
体验Managed Extensibility Framework精妙的设计 MEF(Managed Extensibility Framework)是.NET Framework 4.0一个重要 ...
- linux调整分区大小
查看一下当前分区情况 1 2 3 4 5 6 7 8 [root@localhost ~]# df -h Filesystem Size Used Avail Use% Mou ...