editorial-render A
PROBLEM LINK:
Author: admin
Tester: Kevin Atienza
Editorialist: Ajay K. Verma
Russian Translator: Sergey Kulik
Mandarian Translator: Gedi Zheng
DIFFICULTY:
Medium
PREREQUISITES:
Combinatorics, Modular Arithmetic
PROBLEM:
Given two integers $N$ and $K$, find how many triplets of strings over $K$ alphabets exist, such that length of all strings is bounded by $N$, and no string in a triplet is a prefix of another string of the triplet.
EXPLANATION:
We compute the number of triplets of strings of bounded length, and then subtract the ones in which one string is a prefix of another. For the sake of brevity, we define a new notation $\prec$ to represent the "prefix" relationship, i.e., "$X$ is a prefix of $Y$" can be represented by $X \prec Y$.
Total Number of Triplets:
There are exactly $K^i$ strings of length $i$. Hence, the number of strings with length not exceeding $N$ will be:
$A = K + K^2 + K^3 + \cdots + K^N$
$A = K (K^N - 1 / (K - 1))$
If the modular inverse of $(K - 1)$ with respect to the given prime $p = 10^9 + 7$ is $x$, then the value of $A \bmod p$ would be $(K (K^N - 1) x) \bmod p$.
Both the modular inverse, and power of a number can be compute in $O (\log N)$ time. Hence, we can compute the value of $A$ in $O (\log N)$ time.
There are $A$ strings of length not exceeding $N$. Therefore, the number of triplets would be $A^3$. Next, we count how many of these triplets are bad triplets, i.e., the ones in which one string is a prefix of another.
Triplets of the Form $X \prec Y \prec Z$:
In this section we count the number of triplets which have strings forming a chain under the relationship $\prec$, i.e., if the triplet is $(P, Q, R)$, then one of the following is true:
$P \prec Q \prec R$
$P \prec R \prec Q$
$Q \prec P \prec R$
$Q \prec R \prec P$
$R \prec P \prec Q$
$R \prec Q \prec P$
Note that for some triplets more than one of these conditions might be true, e.g., $(X, X, X)$ will satisfy all $6$ conditions. Hence, we consider four categories, so that each triplet is counted exactly once.
1) $X = Y = Z$:
There will be $A$ triplets of this form, as we can choose the string $X$ in $A$ ways, and the other two will be equal to it. These triplets will satisfy all $6$ conditions, i.e., each triplet will be repeated $6$ times.
2) $X \prec Y = Z$ and $X \neq Y$:
For a given string $Y$ of length $r$, there are exactly $(r - 1)$ possible values of string $X$, the ones which are proper substring of $Y$. Hence, the number of such triplets would be:
$K^2 + 2 K^3 + 3 K^4 + \cdots + (N - 1) K^N$.
This is the sum of an arithmetic-geometric series, and can be represented explicitly as shown here. The computation of this sum also requires the computation of power and modular inverse, and hence can be done in $O (\log N)$ time.
These triplets will satisfy two of the above conditions ($X \prec Y \prec Z$, and $X \prec Z \prec Y$).
3) $X = Y \prec Z$, and $Y \neq Z$:
Number of these triplets will be the same as the one computed in (2). This is because once we pick a string $Z$ of length $r$, then $Y$ has exactly $(r - 1)$ choices.
These triplets also satisfy two of the above conditions ($X \prec Y \prec Z$, and $Y \prec X \prec Z$).
4) $X \prec Y \prec Z$, $X \neq Y$, and $Y \neq Z$:
If we pick a string $Z$ of length $r$, then we only need to choose the lengths of $X$ and $Y$. The lengths of $X$ and $Y$ must be distinct and lie between $1$ and $(r - 1)$. Hence, the number of $(X, Y)$ pairs would be the number of ways of choosing two distinct numbers between $1$ and $(r - 1)$, which is given by $(r - 1) \choose 2$. Therefore the number of such triplets would be:
$K^3 + 3 K^4 + 6 K^5 + \cdots + {{N - 1} \choose 2} K^N$
Again this is the sum of arithmetic-geometric sequence of second order, and can be computed using the similar methods. The computation will take $O (\log N)$ time.
These triplets will satisfy exactly one of the above conditions.
Hence, the number of chain triplets would be $6$ ((number of category 1 triplets)/$6$ + (number of category 2 triplets)/$2$ + (number of category 3 triplets)/$2$ + (number of category 4 triplets)/$1$).
Triplets of the Form "$X \prec Y$, $Z$ does not share prefix relationship with $Y$":
In this section we count the number of triplets in which exactly two strings satisfy the "prefix" relationship, i.e., if the triplet is $(P, Q, R)$, then one of the following is true:
$P \prec Q$, $R$ has no prefix relation with $Q$,
$Q \prec P$, $R$ has no prefix relation with $P$,
$P \prec R$, $Q$ has no prefix relation with $R$,
$R \prec P$, $Q$ has no prefix relation with $P$,
$Q \prec R$, $P$ has no prefix relation with $R$,
$R \prec Q$, $P$ has no prefix relation with $Q$,
Once again it is possible that one triplet satisfy more than one condition, however, we should count it only once.
1) $X = Y$, $Z$ has no prefix relation with $Y$:
If we have a string $Y$ of length $r$, then there are exactly $r$ prefix strings of $Y$. If a string $W$ has $Y$ as a proper prefix, then $W$ can be written as $W = Y + U$, where $U$ is a non-empty string of length at most $(N - r)$. Hence, the number of such string $W$ will be $K + K^2 + \cdots + K^{N - r}$. Since $Z$ is neither a prefix of $Y$, nor has $Y$ as its prefix, the number of possible values of $Z$ would be:
$(K + K^2 + \cdots + K^N) - (K + K^2 + \cdots + K^{N - r}) - r$
$= (K^{N - r + 1} + K^{N - r + 2} + \cdots + K^N - r)$.
Also number of ways of choosing a $r$ length string $Y$ is $K^r$. Hence, the number of such triplets $(X, Y, Z)$, with $Y$ being a $r$-length string will be:
$K^r (K^{N - r + 1} + K^{N - r + 2} + \cdots + K^N - r)$
$= K^{N + 1} * (1 + K + K^2 + \cdots + K^{r - 1}) - r * K^r$
If we take the sum of this expression over all possible values of $r$, we get the number of triplets $(X, Y, Z)$ satisfying the above criteria, and it will be:
$K^{N + 1} (N + (N - 1) K + (N - 2) K^2 + \cdots + K^{N - 1}) - (K + 2 K^2 + 3 K^3 + \cdots + N K^N)$
This is also a sum of arithmetic-geometric sequence, and can be computed in logarithmic time. These triplets satisfy two of the above conditions ($X \prec Y$, with $Z$ having no relationship, and $Y \prec X$, with $Z$ having no relationship).
2) $X \prec Y$, $X \neq Y$, and $Z$ has no prefix relation with $Y$:
Once again if we pick a string $Y$ of length $r$, then $X$ will have $(r - 1)$ choices, and Z will have $(K^{N - r + 1} + K^{N - r + 2} + \cdots + K^N - r)$ choices. Hence, the number of such triplets with $Y$ being of length $r$ would be:
$r K^r (K^{N - r + 1} + K^{N - r + 2} + \cdots + K^N - r)$
$= r K^{N + 1} (1 + K + K^2 + \cdots + K^{r - 1}) - r^2 K^r $
If we sum it over all possible values of $r$, we would get the number of triplets $(X, Y, Z)$ satisfying the above condition, which would be (after some simplifications):
${(N + 1) \choose 2} K^{N + 1} (1 + K + K^2 + \cdots + K^N) - K^{N + 1} ({2 \choose 2} K + {3 \choose 2} K^2 + \cdots + {N \choose 2} K^{N - 1})$
$ - (1^2 K + 2^2 K^2 + 3^2 K^3 + \cdots + N^2 K^N)$.
The number of triplets where exactly two strings satisfy the "prefix" relationship will be $6$ ( (number of triplets of first category)/$2$ + (number of triplets of second category)/$1$).
Now that we have computed the number of bad triplets, we can subtract them from the total number of triplets to get the number of good ones.
Time Complexity:
$O (\log N)$
AUTHOR'S AND TESTER'S SOLUTIONS:
Author's solution will be put up soon.
Tester's solution will be put up soon.
editorial-render A的更多相关文章
- React.render和reactDom.render的区别
刚开始学习react.js.发现网上的资料,有些是写着react.render,有些写着reactDom.render.觉得很奇怪就查阅了一下资料.解释如下: 这个是react最新版api,也就是0. ...
- XF custom render 各平台实现类
目前的XF还是非常简陋的,所以存在大量的自定义工作.一般情况下我们只是要需要派生原生的XF控件,然后在各平台下修改其呈现方法. 所以了解每个XF控件在不同平台上呈现使用的控件类是有所必须要的.以下别人 ...
- 塞翁失马,焉知非福:由 Styles.Render 所引发 runAllManagedModulesForAllRequests="true" 的思考
最近在使用 MVC 开发的时候,遇到一个对我来说"奇怪的问题",就是使用 BundleTable 进行 CSS.JS 文件绑定,然后使用 Styles.Render.Scripts ...
- ReactJS分析之入口函数render
前言 在使用React进行构建应用时,我们总会有一个步骤将组建或者虚拟DOM元素渲染到真实的DOM上,将任务交给浏览器,进而进行layout和paint等步骤,这个函数就是React.render() ...
- Cesium原理篇:6 Render模块(3: Shader)
在介绍Renderer的第一篇,我就提到WebGL1.0对应的是OpenGL ES2.0,也就是可编程渲染管线.之所以单独强调这一点,算是为本篇埋下一个伏笔.通过前两篇,我们介绍了VBO和Textur ...
- Cesium原理篇:6 Render模块(4: FBO)
Cesium不仅仅提供了FBO,也就是Framebuffer类,而且整个渲染过程都是在FBO中进行的.FBO,中文就是帧缓冲区,通常都属于高级用法,但其实,如果你了解了它的基本原理后,用起来还是很简单 ...
- Cesium原理篇:6 Render模块(5: VAO&RenderState&Command)
VAO VAO(Vertext Array Object),中文是顶点数组对象.之前在<Buffer>一文中,我们介绍了Cesium如何创建VBO的过程,而VAO可以简单的认为是基于VBO ...
- render :template 和 render :parital
1 .这两个都可以在controller和view中使用,而且好像可以替换,只是用:template,rails不会自动加下划线,用:partial,rails会自动添加下划线.而且规范的做法,:te ...
- AngularJs中,如何在render完成之后,执行Js脚本
AngularJs是Google开源的前端JS框架.使用AngularJs, 我们能够容易地.健壮的开发出类似于Gmail一样的单页Web应用.AngularJs这个新兴的MVC前端框架,具有以下特点 ...
- 利用Render Texture实现游戏的小雷达效果(摄影机分屏)
最近游戏蛮牛在举办一个活动,就是要做出这样的效果: 题目:实现游戏分屏效果 要求:1. 分屏,且分割线不规则(即非水平或垂直):2. 各屏可单独操作(移动.缩放),操作指该 ...
随机推荐
- 百度地图整合功能分享修正版[ZMap.js] 实例源码!
ZMap 功能说明 ZMap 是学习百度地图 api 接口,开发基本功能后整的一个脚本类,本类方法功能大多使用 prototype 原型 实现: 包含的功能有:轨迹回放,圈画区域可编辑,判断几个坐标是 ...
- 使用Owin中间件搭建OAuth2.0认证授权服务器
前言 这里主要总结下本人最近半个月关于搭建OAuth2.0服务器工作的经验.至于为何需要OAuth2.0.为何是Owin.什么是Owin等问题,不再赘述.我假定读者是使用Asp.Net,并需要搭建OA ...
- 在Eclipse 中安装插件 Activiti
(1)在eclipse中菜单help->Install New software中,点击add (2)输入要安装的插件的名字和路径 Name:Activiti BPMN 2.0 designer ...
- tarjan算法--求无向图的割点和桥
一.基本概念 1.桥:是存在于无向图中的这样的一条边,如果去掉这一条边,那么整张无向图会分为两部分,这样的一条边称为桥无向连通图中,如果删除某边后,图变成不连通,则称该边为桥. 2.割点:无向连通图中 ...
- 【CodeForces 624C】Graph and String
题 题意 n个表示abc三个字符的点,所有a和b是相连的,所有b和c是相连的,所有相同的是相连的,现在给你n个点和他们之间的m条边,判断是否存在这样的字符串,存在则给出一个符合条件的. 分析 我的做法 ...
- GMM算法k-means算法的比较
1.EM算法 GMM算法是EM算法族的一个具体例子. EM算法解决的问题是:要对数据进行聚类,假定数据服从杂合的几个概率分布,分布的具体参数未知,涉及到的随机变量有两组,其中一组可观测另一组不可观测. ...
- OpenCV中的全景拼接例程
使用Stitcher类,通过createDefault()方法创建拼接对象,通过stitch()方法执行默认的自动拼接.自动拼接和07年Brown和Lowe发表的论文描述的步骤基本一致,只不过使用的特 ...
- codevs1225 八数码难题
题目描述 Description Yours和zero在研究A*启发式算法.拿到一道经典的A*问题,但是他们不会做,请你帮他们.问题描述 在 3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数 ...
- Linux 基础网络设置
一.查看以及测试网络 查看及测试网络配置是管理Linux网络服务的第一步,本节将学习Linux系统中的网络查看以及测试命令.其中讲解的大多数命令以普通用户权限就可以完成操作,但是普通用户在执行&quo ...
- UVA1220Party at Hali-Bula(树的最大独立集 + 唯一性判断)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105116#problem/H 紫书P282 员工和直属老板只能选一个,最多选多少人 思路 ...