BNUOJ 34985 Elegant String 2014北京邀请赛E题 矩阵快速幂
题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985
题目大意:问n长度的串用0~k的数字去填,有多少个串保证任意子串中不包含0~k的某一个全排列
邀请赛上A的较多的一道题,比赛的时候死活想不出,回来之后突然就想通了,简直..... = =!
解题思路:
对于所有串我们都只考虑末尾最多有多少位能构成全排列的一部分(用l来表示),即最多有多少位不重复的数字出现,将问题转化为求末尾最多有k位能构成全排列的串的总数量
假设k为5,有一个串……0023,不难发现l=3
我们以这个串出发在之后添上数字,假如我们添的是0、2、3中的一个:
0: ……00230 (l=3)
2: ……00232 (l=2)
3: ……00233 (l=1)
假如是l长度中没有出现过的数字
则构成新串 ……00231 ……00234 ……00235 l=4
最后可以得到规律:总长度为n串中 l=m的串的数量 x1 得到 总长度为n+1的串中 l=(1,2,……,m)的串
总长度为n串中 l=m的串的数量 x(k-m+2) 得到 总长度为n+1的串中 l=m+1的串
用mar[i][j]来表示由l=j的串得到l=i的串所以
mar可以表示为(以k=5为例)
1 1 1 1 1
5 1 1 1 1
0 4 1 1 1
0 0 3 1 1
0 0 0 2 1
通过该矩阵我们可以由长度为n的串数量可以推出长度为n+1的串的数量:
于是我们可以通过长度1的串最终得到总长度为n的串, n=1时只有l最多为1 总数为 k+1
快速幂求得该矩阵的(n-1)次幂,该矩阵的第一列相加乘(k+1)即为最终结果
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define FFF 20140518
struct node{
long long mar[][];
}sor;
void init(int k)
{
memset(sor.mar,,sizeof(sor.mar));
for(int i=;i<=k;i++)
{
for(int j=i;j<=k;j++)
{
sor.mar[i][j]=;
}
if(i>)
{
sor.mar[i][i-]=k-i+;
}
}
}
node marMulti(node a,node b,int k)
{
node ret;
memset(ret.mar,,sizeof(ret.mar));
for(int i=;i<=k;i++)
{
for(int j=;j<=k;j++)
{
for(int l=;l<=k;l++)
{
ret.mar[i][j]+=(a.mar[i][l]*b.mar[l][j])%FFF;
ret.mar[i][j]%=FFF;
}
}
}
return ret;
}
node matrixPow(long long x,int k)
{
node now=sor;
node ret;
memset(ret.mar,,sizeof(ret.mar));
for(int i=;i<=k;i++)
ret.mar[i][i]=;
while(x)
{
if(x%==)
ret=marMulti(now,ret,k);
x/=;
now=marMulti(now,now,k);
}
return ret;
}
void print(node sor,int k)
{
for(int i=;i<=k;i++)
{
for(int j=;j<=k;j++)
{
cout<<sor.mar[i][j]<<' ';
}
cout<<endl;
}
}
int main()
{
int keng,k,Case=;
long long n;
scanf("%d",&keng);
while(keng--)
{
scanf("%lld%d",&n,&k);
init(k);
node ret=matrixPow(n-,k);
int ans=;
// print(sor,k);
// print(ret,k);
for(int i=;i<=k;i++)
{
ans+=(ret.mar[i][]*(k+))%FFF;
ans%=FFF;
}
printf("Case #%d: %d\n",Case++,ans);
}
return ;
}
BNUOJ 34985 Elegant String 2014北京邀请赛E题 矩阵快速幂的更多相关文章
- bnuoj 34985 Elegant String DP+矩阵快速幂
题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 We define a kind of strings as elegant s ...
- bnuoj 16493 Just Pour the Water(矩阵快速幂)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=16493 [题解]:矩阵快速幂 [code]: #include <cstdlib> #i ...
- 徐州邀请赛 江苏 icpc I. T-shirt 矩阵快速幂
题目 题目描述 JSZKC is going to spend his vacation! His vacation has N days. Each day, he can choose a T-s ...
- 【构造共轭函数+矩阵快速幂】HDU 4565 So Easy! (2013 长沙赛区邀请赛)
[解题思路] 给一张神图,推理写的灰常明白了,关键是构造共轭函数,这一点实在是要有数学知识的理论基础,推出了递推式,接下来就是矩阵的快速幂了. 神图: 给个大神的链接:构造类斐波那契数列的矩阵快速幂 ...
- hihocoder 1084 扩展KMP && 2014 北京邀请赛 Justice String
hihocoder 1084 : http://hihocoder.com/problemset/problem/1084 北京邀请赛 Just String http://www.bnuoj.co ...
- 2014 北京邀请赛ABDHJ题解
A. A Matrix 点击打开链接 构造,结论是从第一行開始往下产生一条曲线,使得这条区间最长且从上到下递减, #include <cstdio> #include <cstrin ...
- HDU5863 cjj's string game(DP + 矩阵快速幂)
题目 Source http://acm.split.hdu.edu.cn/showproblem.php?pid=5863 Description cjj has k kinds of charac ...
- ACM学习历程——HDU5015 233 Matrix(矩阵快速幂)(2014陕西网赛)
Description In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 2 ...
- 2019南昌邀请赛 C. Angry FFF Party 大数矩阵快速幂+分类讨论
题目链接 https://nanti.jisuanke.com/t/38222 题意: 定义函数: $$F(n)=\left\{\begin{aligned}1, \quad n=1,2 \\F(n- ...
随机推荐
- (八)Struts2 文件上传和下载
所有的学习我们必须先搭建好Struts2的环境(1.导入对应的jar包,2.web.xml,3.struts.xml) 第一节:Struts2 文件上传 Struts2 文件上传基于Struts2 拦 ...
- IOS LocationManager定位国内偏移,火星坐标(GCJ-02)解决方法
转载自:http://blog.csdn.net/swingpyzf/article/details/16972351 纠偏也可参考:http://www.2cto.com/kf/201310/253 ...
- map函数(转)
STL中map用法详解 说明:如果你具备一定的C++ template知识,即使你没有接触过STL,这个文章你也应该可能较轻易的看懂.本人水平有限,不当之处,望大家辅正. 一.Map概述 Map是ST ...
- 一个由IsPrime算法引发的细节问题
//******************************* // // 2014年9月18日星期四,于宿舍撰写 // 作者:夏华林 // //******************* ...
- 从内部剖析C#集合之HashTable
计划写几篇文章专门介绍HashTable,Dictionary,HashSet,SortedList,List 等集合对象,从内部剖析原理,以便在实际应用中有针对性的选择使用. 这篇文章先介绍Hash ...
- 怎样让老浏览器兼容html5新标签
CSS样式设置默认样式: <style> article, aside, canvas, details, figcaption, figure, footer, header, hgro ...
- Express使用html模板
express默认使用jade模板,可以配置让其支持使用ejs或html模板. 1. 安装ejs 在项目根目录安装ejs. npm install ejs 2.引入ejs var ejs = requ ...
- php接口开发--复制缩减Codeigniter的车轮
接口需求: 输出json 单一入口 安全 http://segmentfault.com/q/1010000000143852基于token验证?session? 缓存 session cookie ...
- Forward reference vs. forward declaration
Q:Im a bit confused. What is the difference between forward declaration and forward reference? Forwa ...
- WPF布局容器综合展示
Border控件,以及几个重要要的属性:Background:背景的 Brush 对象BorderBrush:用来绘制边框BorderThickness: Border 边框的宽度,设置边框每一边的线 ...