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- ...
随机推荐
- [转载]SQL字符串处理函数大全
[转载]http://www.cnblogs.com/andy2005/archive/2007/12/04/981864.html select语句中只能使用sql函数对字段进行操作(链接sql s ...
- JAVA zip解压 MALFORMED 错误
最近在在使用zip 解压时,使用JDK1.7及以上版本在解压时,某些文件会报异常 Exception in thread "main" java.lang.IllegalArgum ...
- UIButton 使用imageEdgeInsets和titleEdgeInsets属性
现在App的底部栏.侧边栏.顶部栏经常出现一些包含图像和文字的Item,以前用按钮上面添加label和imageView, 想想实在是对资源的浪费.. 图1 — 底部 ...
- C#动态二维数组输出
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- php文件缓存
1.最新代码 <?php class cache { private static $_instance = null; protected $_options = array( 'cache_ ...
- hibernate错误提示
2016-05-03 09:45:03,275 -- WARN -- org.hibernate.internal.util.xml.DTDEntityResolver.resolveEntity( ...
- Faces.JavaServer Pages(JSP)
zhengly.cn atitit.Servlet2.5 Servlet 3.0 新特性 jsp2.0 jsp2.1 jsp2.2新特性 1.1. Servlet和JSP规范版本对应关系:1 1.2. ...
- Java 不定长度参数
在调用某个方法时,若是方法的参数个数事先无法确定该如何处理?例如System.out.printf()方法中并没有办法事先决定要给的参数个数,像是: ? 1 2 3 System.out.printf ...
- Android 开机过程PMS分析
12-13 12:25:05.595 3253 3253 SystemServer !@Boot: Start PackageManagerService pm/PackageManagerServi ...
- paramiko模块实现堡垒机
通过SSHClient 执行命令 """通过用户名密码验证""" import paramiko # 创建 SSH 对象 ssh = par ...