CF135E Weak Subsequence (计数问题)
题目大意:对于给定字符集大小k,求有多少个字符串满足它的最长【既是子串又是弱子序列】的长度为w;
神仙计数题
打表发现,对于字符串S而言,它的最长【既是子串又是弱子序列】,一定存在一个对应的子串,是S的前缀或者后缀
如果不是前缀或者后缀,那么它一定还可以向两边扩展
这启示我们分类讨论
容易发现,最优的情况一定是除了最后一位以外,其它每个字符都在相同的位置取,最后一位在其他位置取来构成弱子序列。
我们重点考虑最后一位带来的影响,
假设现在我们找到了一个字符串$w$作为子串,在后面接上一段字符串$L$
我们要保证$w$是最长的【既是子串又是弱子序列】才能正确地统计答案
1.$L$中一定存在一个字符和第$w$个字符相等
2.$L$中每个字符都不相等,否则继续向前取还能更长
3.前$L$个字符互不相等,否则从后面开始取能更长
有了这三个条件,我们就可以开始讨论了:
1.$w-L\geq 2$时,把序列分成5块,$[1,L], L+1, [L+2,w-1], w, [w+1,w+L]$
$[1,L]$存在一个字符和第L+1位相等,$[w+1,w+L]$位存在一个字符和第$w$位相等,两种情况取交集,可得答案为:
$k^{w-L-2}((A_{k}^{L})^{2}k^{2}-(A_{k}^{L+1})^{2})$
中间$w-L-2$个随便取,两边$L$个分别从$k$个里取且互不相等,第$L+1$个和第$w$个先假设随便取,再去掉两边都没有相同的情况
2.$w-L=1$时,把序列分成3块,$[1,L], w, [w+1,w+L]$
$[1,L]$位存在一个字符和第$w$位相等,$[w+1,w+L]$位存在一个字符和第$w$位相等,取交集,答案为:
$k(A_{k}^{L})^{2}-A_{k}^{L+1}A_{k-1}^{L}$
比上面的情况还要简单,不解释了
3.$w\leq L$时,把序列分成5块,$[1,w-1], w, [w+1,L], L+1, [L+2,L+w]$
这种情况就比较复杂了,但大体思路不变
首先,中间$[w+1,L]$一共$L-w$个,是从$k$个里随便取的且互不相同,贡献是$A_{k}^{L-w}$
左右两边$w$个都是从$k-(L-w)$里随便取,贡献是$A_{k-(L-w)}^{w}$
两边都没有的情况的贡献呢?
$w, [w+1,L], L+1$都互不相同,贡献是$A_{k}^{L-w+2}$。前后$w-1$个都和中间$[w,L+1]$个互不相同,贡献是$A_{k-(L-w+2)}^{w-1}$
总贡献就是$A_{k}^{L-w}(A_{k-(L-w)}^{w})^{2}-A_{k}^{L-w+2}(A_{k-(L-w+2)}^{w-1})^{2}$
$L$里每个元素互不相同,所以长度最大就是$k$,暴力枚举然后计算即可
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
#define N1 1000010
using namespace std;
const ll p=; ll mul[N1],_mul[N1],inv[N1];
int m,K,n;
inline ll C(int x,int y)
{
if(y>x) return ;
return mul[x]*_mul[y]%p*_mul[x-y]%p;
}
inline ll A(int x,int y)
{
if(y>x) return ;
return mul[x]*_mul[x-y]%p;
}
ll qpow(ll x,ll y)
{
if(y<) return ; ll ans=;
for(;y;x=x*x%p,y>>=) if(y&) ans=ans*x%p;
return ans;
} int main()
{
scanf("%d%d%d",&m,&K,&n);
int i,j,L;
mul[]=mul[]=_mul[]=_mul[]=inv[]=inv[]=;
for(i=;i<=K;i++) mul[i]=mul[i-]*i%p, inv[i]=1ll*(p-p/i)*inv[p%i]%p, _mul[i]=_mul[i-]*inv[i]%p;
ll ans=;
for(L=;L<=K&&n+L<=m;L++)
{
if(n-L>=) (ans+=qpow(K,n-L-)*(1ll*K*K%p*A(K,L)%p*A(K,L)%p-A(K,L+)*A(K,L+)%p+p)%p)%=p;
else if(n-L==) (ans+=(1ll*K*A(K,L)%p*A(K,L)%p-A(K,L+)*A(K-,L)%p+p)%p)%=p;
else (ans+=(1ll*A(K,L-n)*A(K-(L-n),n)%p*A(K-(L-n),n)%p-A(K,L-n+)*A(K-(L-n+),n-)%p*A(K-(L-n+),n-)%p)%p)%=p;
}
printf("%lld\n",(ans%p+p)%p);
return ;
}
CF135E Weak Subsequence (计数问题)的更多相关文章
- @property中的copy.strong.weak总结
1.NSString类型的属性为什么用copy NSString类型的属性可以用strong修饰,但会造成一些问题,请看下面代码 #import "ViewController.h" ...
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [LeetCode] Is Subsequence 是子序列
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...
- [LeetCode] Wiggle Subsequence 摆动子序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- weak和nonull
weak和nonull是相互排斥的,所以weak和null不能同时使用,如下图:
- iOS中assign,copy,retain之间的区别以及weak和strong的区别
@property (nonatomic, assign) NSString *title; 什么是assign,copy,retain之间的区别? assign: 简单赋值,不更改索引计数(Refe ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
随机推荐
- [Angular] Using ngTemplateOutlet to create dynamic template
I can use <tamplete> syntax and a entry component as a container to create a dynamic component ...
- Facebook图搜索unicorn
unicorn(独角兽),里面类似于倒排链的reference list,相应的term如friend:2,表示entity 2的朋友列表,整个结构是shard的,上面是top aggregator, ...
- Python游戏server开发日记(一)目标
到了新的环境.老大让我有空研究下一代server技术,作为一个长期任务. 新的server想达到的目标: 1.分布式系统,对象(Entity)之间的关系类似于Actor模型. 2.逻辑服务,是单进程. ...
- storm 并行度
1个worker进程运行的是1个topology的子集(注:不会出现1个worker为多个topology服务).1个worker进程会启动1个或多个executor线程来运行1个topology的c ...
- ubuntu使用ssh连接远程电脑的方法
目前,大多数linux distributions都预先安装了ssh的客户端,即可以连接别人的电脑.但也有例外的情况,所以,下面先把ssh的客户端与服务端的安装一并讲了吧. ssh客户端及服务端的安装 ...
- Linux framebuffer显示bmp图片【转】
本文转载自:http://blog.csdn.net/luxiaoxun/article/details/7622988 framebuffer简介 帧缓冲(framebuffer)是Linux为显示 ...
- Codeforces--106C--Buns(背包)
Buns Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Status ...
- Java 8 实战 P4 Beyond Java 8
目录 Chapter 13. Thinking functionally Chapter 14. Functional programming techniques Chapter 15. compa ...
- [转] 理解 Dubbo SPI 扩展机制
写在前面 最近接触了 gRPC 体会到虽然众多 RPC 框架各有各的特点但是他们提供的特性和功能有很多的相似之处 , 这就说明他们面对同样的分布式系统带来的问题.从 2016 年左右开始接触到 dub ...
- php函数 array_values()
array_values() 函数返回一个包含给定数组中所有键值的数组,但不保留键名. 提示:被返回的数组将使用数值键,从 0 开始并以 1 递增. $a=array("Name" ...