[JZOJ5836] Sequence
Problem
Solution
吼题啊吼题!
首先如何求本质不同的子序列个数就是 \(f[val[i]]=1+\sum\limits_{j=1}^k f[j]\)
其中 \(f[i]\) 表示的是以 \(i\) 结尾的子序列个数
先把原数列的不同子序列个数求出来,然后观察一下这个转移,贪心的发现每次都是选一个最早出现的 \(i\) 填到序列末尾,然后更新这个值。
所以填的部分一定是 \(\frac mk\) 个 \(K\) 的排列,还有多出来了 \(m\%k\) 个元素暴力填进去。
每 \(K\) 个元素的转移是一样的,可以拿矩乘做。然后多余的部分求前缀积暴力求就行了。
Code
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<cctype>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using std::min;
using std::max;
using std::swap;
using std::vector;
const int N=105;
const int M=1e6+5;
typedef double db;
typedef long long ll;
#define int long long
const int mod=1e9+7;
#define pb(A) push_back(A)
#define pii std::pair<int,int>
#define mp(A,B) std::make_pair(A,B)
int n,m,k,per[M];
int val[M];pii las[M];
struct Mat{
int a[N][N];
void clear(){
memset(a,0,sizeof a);
}
void init(){
clear();
for(int i=1;i<=k+1;i++)
a[i][i]=1;
}
void print(){
for(int i=1;i<=k+1;i++,puts(""))
for(int j=1;j<=k+1;j++)
printf("%lld ",a[i][j]);
}
friend Mat operator*(Mat x,Mat y){
Mat z;z.clear();
for(int i=1;i<=k+1;i++){
for(int p=1;p<=k+1;p++){
for(int j=1;j<=k+1;j++)
z.a[i][j]=(z.a[i][j]+x.a[i][p]*y.a[p][j]%mod)%mod;
}
} return z;
}
}cs,f,qzh[N];
int getint(){
int X=0,w=0;char ch=0;
while(!isdigit(ch))w|=ch=='-',ch=getchar();
while( isdigit(ch))X=X*10+ch-48,ch=getchar();
if(w) return -X;return X;
}
Mat ksm(Mat x,int y){
Mat ans;ans.init();
while(y){
if(y&1) ans=ans*x;
x=x*x;y>>=1;
} return ans;
}
signed main(){
freopen("sequence.in","r",stdin);freopen("sequence.out","w",stdout);
n=getint(),m=getint(),k=getint();
int sum=0;f.clear();f.a[1][k+1]=1;
for(int i=1;i<=k;i++) las[i]=mp(0,i);
for(int i=1;i<=n;i++){
val[i]=getint();
int p=f.a[1][val[i]];
f.a[1][val[i]]=(sum+1)%mod;
sum-=p;sum+=f.a[1][val[i]];sum%=mod;
las[val[i]]=mp(i,val[i]);
} std::sort(las+1,las+1+k);
qzh[0].init();
for(int i=1;i<=k;i++){
per[i]=las[i].second;
qzh[i].clear();
for(int j=1;j<=k+1;j++) qzh[i].a[j][j]=1;
for(int j=1;j<=k+1;j++) qzh[i].a[j][per[i]]=1;
qzh[i]=qzh[i-1]*qzh[i];
} cs=ksm(qzh[k],m/k);
cs=cs*qzh[m%k];
f=f*cs;int ans=0;
for(int i=1;i<=k;i++) (ans+=f.a[1][i])%=mod;
printf("%lld\n",ans);
return 0;
}
[JZOJ5836] Sequence的更多相关文章
- oracle SEQUENCE 创建, 修改,删除
oracle创建序列化: CREATE SEQUENCE seq_itv_collection INCREMENT BY 1 -- 每次加几个 STA ...
- Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等
功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...
- DG gap sequence修复一例
环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Sequence Reconstruction 序列重建
Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
随机推荐
- Knockout.js组件系统的详解之(一) - 组件的定义和注册
(Knockout版本:3.4.1 ) KO的组件主要从以下四个部分进行详细介绍: 1.组件的定义和注册 2.组件绑定 3.使用自定义元素 4.自定义组件加载器(高级) 目录结构 1.通过" ...
- CSS3知识!
一.引入样式 1.行内样式表 <h1 style="color: red;font-size: 18px;">10-30</h1> 2.内部样式表(在hea ...
- select和其元素options
普通的select形式为: <select> <option>选中元素1</option> <option>选中元素2</option> & ...
- C++顺序容器之list初探
C++顺序容器之list初探 双向链表,支持双向顺序访问.在list中任何位置进行插入和删除速度都很快. list不支持随机访问,为了访问一个元素,必须遍历整个容器. #include<iost ...
- 跨域访问问题js
您可以通过使用JSONP形式的回调函数来加载其他网域的JSON数据 访问百度的地址解析 返回来省,地区,市 $.getJSON("http://api.map.baidu.com/clou ...
- Maven的多mirrors的配置
repo1 central Human Readable Name for this Mirror. http://repo1.maven.org/maven2/ repo2 central Huma ...
- httpd-vhosts.conf配置
<VirtualHost 127.0.0.1:80> ServerName zjm.appems.com DocumentRoot E:\qian\web <Directory &q ...
- modelsin联合仿真
1-选择eda仿真工具 tool->options->eda tool options 2-assignments->settings->eda tool settings- ...
- ASP.NET Core 请求/查询/响应参数格式转换(下划线命名)
业务场景: 在 ASP.NET Core 项目中,所有的代码都是骆驼命名,比如userName, UserName,但对于 WebApi 项目来说,因为业务需要,一些请求.查询和响应参数的格式需要转换 ...
- 设计模式,Let's “Go”! (上)
code[class*="language-"], pre[class*="language-"] { background-color: #fdfdfd; - ...