AtCoder Grand Contest 021完整题解

提示:若是公式挂了请多刷新几回,MathJex的公式渲染速度并非那么理想。

总的来讲,仍是本身太弱了啊。只作了T1,还WA了两发。今天还有一场CodeForces,晚上0点qwq... 题解仍是要好好写的。git

A - Digit Sum 2


Time limit : 2sec / Memory limit : 256MB算法

Score : 300 pointsapp

Problem Statement

Find the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.dom

Constraints

 

  • 1≤N≤$10^{16}$
  • N is an integer.

  • Input

    Input is given from Standard Input in the following format:ide

    N
    Output

    Print the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.this


    Sample Input 1

    Copyidea

    100
    Sample Output 1

    Copyspa

    18

    For example, the sum of the digits in 99 is 18, which turns out to be the maximum value.rest


    Sample Input 2

    Copycode

    9995
    Sample Output 2

    Copy

    35

    For example, the sum of the digits in 9989 is 35, which turns out to be the maximum value.


    Sample Input 3

    Copy

    3141592653589793
    Sample Output 3

    Copy

    137

    一句话题意:给定一个数$N$,找出它最大的数字和,一个数字的数字和定义为这个数字的每一位数的和。

    题解:

    很明显贪心是一种正确的思路。由于须要最大化数字和,因此对于数字的每一位,咱们都要使得这一位数尽量大。考虑两种状况(三种?):

  • 第一种状况:对于形如$c99\cdots9$的数字,咱们要最大化数字和,只须要将最高位的数字减$1$,而后将其他各位变为$9$便可,能够证实这样是最优的。设数的位为$K$,则有$ans=c-1+9\times(K-1)$。
  • 第二种状况:对于形如$99,\,999$的由若干个$9$组成的数字,明显的答案即为其自己,若是按照第一种状况作答案小了$1$,这个时候咱们须要加回来。
  • 第三种状况(多是我太弱了):对于$N<9$的状况,按照上述算法运行,答案比正确答案小了$1$,这个时候答案直接是这个数自己,即当某个数是一位数的时候,答案等于其自己。
  • 就这样,在WA了两发后终于A了这题。用时34分钟。
#include<cstdio>
#include<cstring>
using namespace std;
long long n;
char num[100];
#ifdef WIN32
#define lld "%I64d"
#else
#define lld "%lld"
#endif
int main()
{
	long long ans=0;
	scanf(lld,&n);
	sprintf(num,lld,n);
	ans=1ll*(strlen(num)-1)*9;
	if((n+1)%10==0)ans++;
	ans+=num[0]-'0'-1;
	if(n<10)ans=n;
	printf(lld"\n",ans);
	return 0;
}

B - Holes


Time limit : 2sec / Memory limit : 256MB

Score : 600 points

Problem Statement

There are N holes in a two-dimensional plane. The coordinates of the i-th hole are (xi,yi).

Let $R=10^{10^{10^{10}}}$. Ringo performs the following operation:

  • Randomly choose a point from the interior of a circle of radius R centered at the origin, and put Snuke there. Snuke will move to the hole with the smallest Euclidean distance from the point, and fall into that hole. If there are multiple such holes, the hole with the smallest index will be chosen.
  • For every i(1≤iN), find the probability that Snuke falls into the i-th hole.

    Here, the operation of randomly choosing a point from the interior of a circle of radius R is defined as follows:

  • Pick two real numbers x and y independently according to uniform distribution on [−R,R].
  • If x2+y2≤R2, the point (x,y) is chosen. Otherwise, repeat picking the real numbers x,y until the condition is met.
  • Constraints
  • 2≤N≤100
  • |xi|,|yi|≤$10^6$(1≤iN)
  • All given points are pairwise distinct.
  • All input values are integers.

  • Input

    Input is given from Standard Input in the following format:

    Nx1y1:xNyN
    Output

    Print N real numbers. The i-th real number must represent the probability that Snuke falls into the i-th hole.

    The output will be judged correct when, for all output values, the absolute or relative error is at most $10^{−5}$.


    Sample Input 1

    Copy

    2
    0 0
    1 1
    Sample Output 1

    Copy

    0.5
    0.5

    If Ringo put Snuke in the region x+y≤1, Snuke will fall into the first hole. The probability of this happening is very close to 0.5. Otherwise, Snuke will fall into the second hole, the probability of which happening is also very close to 0.5.


    Sample Input 2

    Copy

    5
    0 0
    2 8
    4 5
    2 6
    3 10
    Sample Output 2

    Copy

    0.43160120892732328768
    0.03480224363653196956
    0.13880483535586193855
    0.00000000000000000000
    0.39479171208028279727

    一句话题意:二维平面内有$N$个洞,第$i$个洞的坐标为$(xi,yi)$,Ringo在这个平面内画了一个半径近乎无限大的圆,圆心为原点,在圆内随机选择一些点放置一些小球,小球会滚到距离它最近的一个洞里,问小球滚动到每一个洞内的几率。

    题解1:

    因为$R$很是大,咱们能够假设一个随机选择的点距离洞的位置很远。

    将被选点成为$s$。他何时会落入一个洞$p$?对于每个洞$q$(而不是$p$),$dist(s,p)<dist(s,q)$必定成立。由于$s$距离洞很远,当且仅当$\angle spq>\pi/2$.

    这给出了一个很是简单的$O(N^2\log N)$的解法。固定一个洞$p$,计算从$p$到每一个其它的洞的路径,而后将这些路径排序。$p$被选择的几率是$\max\{两条相邻的最长间隔-\pi,0\}/(2\pi)$。

    注意到咱们能够把复杂度改进到$O(N\log N)$。取出全部洞构成的凸包。若是一个洞$p$不是凸包的一个顶点,$p$点被选中的可能性为$0$。不然,令$q,r$为邻接$p$的凸包的两个定点。那么,$p$被选中的几率是$(\pi-\angle qpr)/(2\pi)$。

    题解2:

    注意到平面很是大,精度要求为1e-5不是太大,$N$的值也不大。咱们能够把这个圆平均划分为1e6个部分,对于每一个划分出来的部分,咱们都用线性时间来判断这个部份内的全部小球最终会落入哪个洞内。而后对于每个洞统计一下有多少个部份内的点会落入其中便可计算出答案。若是不放心的话,因为时间限制比较大,能够将这个圆继续划分为更多的部分,从而获得更准确的答案。

    C - Tiling


    Time limit : 2sec / Memory limit : 256MB

    Score : 900 points

    Problem Statement

    Takahashi has an N×M grid, with N horizontal rows and M vertical columns. Determine if we can place A1×2 tiles (1 vertical, 2 horizontal) and B2×1 tiles (2vertical, 1 horizontal) satisfying the following conditions, and construct one arrangement of the tiles if it is possible:

  • All the tiles must be placed on the grid.
  • Tiles must not stick out of the grid, and no two different tiles may intersect.
  • Neither the grid nor the tiles may be rotated.
  • Every tile completely covers exactly two squares.
  • Constraints
  • 1≤N,M≤1000
  • 0≤A,B≤500000
  • N, M, A and B are integers.

  • Input

    Input is given from Standard Input in the following format:

    NMAB
    Output

    If it is impossible to place all the tiles, print NO. Otherwise, print the following:

    YES
    c11…c1M:cN1…cNM

    Here, cij must be one of the following characters: ., <, >, ^ and v. Represent an arrangement by using each of these characters as follows:

  • When cij is ., it indicates that the square at the i-th row and j-th column is empty;
  • When cij is <, it indicates that the square at the i-th row and j-th column is covered by the left half of a 1×2 tile;
  • When cij is >, it indicates that the square at the i-th row and j-th column is covered by the right half of a 1×2 tile;
  • When cij is ^, it indicates that the square at the i-th row and j-th column is covered by the top half of a 2×1 tile;
  • When cij is v, it indicates that the square at the i-th row and j-th column is covered by the bottom half of a 2×1 tile.

  • Sample Input 1

    Copy

    3 4 4 2
    Sample Output 1

    Copy

    YES
    <><>
    ^<>^
    v<>v

    This is one example of a way to place four 1×2 tiles and three 2×1 tiles on a 3×4 grid.


    Sample Input 2

    Copy

    4 5 5 3
    Sample Output 2

    Copy

    YES
    <>..^
    ^.<>v
    v<>.^
    <><>v

    Sample Input 3

    Copy

    7 9 20 20
    Sample Output 3

    Copy

    NO

    一句话题意:用$A$个$1\times 2$和$B$个$2\times 1$的骨牌覆盖一个$N\times M$的矩阵,骨牌不能翻转,问可否作到,若能作到输出任意一种方案。

    题解:

    为了解决这个题目,下列一些结论是显然的:

  • $A+B\le\left\lfloor\frac{NM}2\right\rfloor$(比较区域);
  • $A\le N\left\lfloor\frac{M}{2}\right\rfloor$(每行最多放$\left\lfloor\frac{M}{2}\right\rfloor$个骨牌);
  • $B\le M\left\lfloor\frac{N}{2}\right\rfloor$(每列最多放$\left\lfloor\frac{N}{2}\right\rfloor$个骨牌)。
  • 定义$S:=\left\lfloor\frac{NM}{2}\right\rfloor,S_A:=N\left\lfloor\frac{M}{2}\right\rfloor,S_B:=M\left\lfloor\frac{N}{2}\right\rfloor$,则$A+B\le S,A\le S_A,B\le S_B$必定成立。另外一方面,咱们能够想出如下方法来放置骨牌:

  • 首先,从左上角放$2\times 2$的骨牌(最天然的想法),咱们会获得$\left\lfloor\frac{N}{2}\right\rfloor\left\lfloor\frac{M}{2}\right\rfloor$个正方形。
  • 水平或垂直划分每一个正方形:对于每个正方形,获得两个一样类型的骨牌。
  • 特别地,当$N$是奇数时,能够放置$\left\lfloor\frac{M}{2}\right\rfloor$个$1\times 2$的骨牌,当$M$是奇数时,则能够放置$\left\lfloor\frac{N}{2}\right\rfloor$个$2\times 1$的骨牌。
  • 经过这种方式,一种可行状况是$(A,B)=(S_A,S-S_A),(S_A-2,S-S_A+2),(S_A-4,S-S_A+4),\ldots,(S-S_B,S_B)$。(明显地,若是$A'\le A$且$B'\le B$,若$(A,B)$可行,则$(A',B')$也可行)。

    因此,惟一非平凡状况是$(A,B)=(S_A-1,S-S_A+1),(S_A-3,S-S_A+3),\ldots,(S-S_B+1,S_B-1)$。

    事实证实,这些状况的答案依赖于$NM$的奇偶性。

    当$NM$是奇数时:

    注意到当$N=1$或$M=1$时这种特殊状况不存在,从而能够判定若$N$和$M$是奇数时,$N,M\geq 3$。

    在右下角取出一个$3\times 3$的正方形,两种骨牌都放一个,在以前的方式里,这个部分会被奇数个两种不一样的骨牌覆盖。所以,咱们能够用这种方式实现构造不一样的奇偶性。经过相似的方法填充剩余的部分,$(A,B)=(S_A-1,S-S_A+1),(S_A-3,S-S_A+3),\ldots,(S-S_B+1,S_B-1)$可行。

    当$NM$是偶数时:

    对整个网格图进行黑白染色:奇数行的格子染成黑色,其余格子为白色。而后用$1\times 2$的骨牌覆盖偶数个黑色格子,用$2\times 1$的骨牌覆盖奇数个黑色格子。所以,$2\times 1$的骨牌总数的奇偶性必须知足黑色格子总数的奇偶性。

    事实证实,在这种状况下,不存在非特殊状况无解。

    D - Reversed LCS


    Time limit : 2sec / Memory limit : 256MB

    Score : 900 points

    Problem Statement

    Takahashi has decided to give a string to his mother.

    The value of a string T is the length of the longest common subsequence of T and T', where T' is the string obtained by reversing T. That is, the value is the longest length of the following two strings that are equal: a subsequence of T (possibly non-contiguous), and a subsequence of T' (possibly non-contiguous).

    Takahashi has a string S. He wants to give her mother a string of the highest possible value, so he would like to change at most K characters in S to any other characters in order to obtain a string of the highest possible value. Find the highest possible value achievable.

    Constraints
    • 1≤|S|≤300
    • 0≤K≤|S|
    • S consists of lowercase English letters.
    • K is an integer.

    Input

    Input is given from Standard Input in the following format:

    SK
    Output

    Print the highest possible value achievable.


    Sample Input 1

    Copy

    abcabcabc
    1
    Sample Output 1

    Copy

    7

    Changing the first character to c results in cbcabcabc. Let this tring be T, then one longest common subsequence of T and T' is cbabcbc, whose length is 7.


    Sample Input 2

    Copy

    atcodergrandcontest
    3
    Sample Output 2

    Copy

    15

    题解:

    首先,要找出一个简单的方法计算$T$和$T'$的LCS的长度。假定LCS的长度为$k$,则存在下标$i_1<\cdots<i_k$与$j_1>\cdots>j_k$使得序列$T_{i_1},T_{i_2},\cdots,T_{i_k}$与$T_{j_1},T_{j_2},\cdots,T_{j_k}$是相同的。而后,存在一个整数$p$使得$i_p<j_p$且$i_{p+1}\le j_{p+1}$。(方便起见,令$i_{k+1}=|T|+1,j_{k+1}=-1$。)若$i_{p+1}>j_{p+1}$,子序列$T_{i_1},\cdots,T_{i_p},T_{j_p},\cdots,T_{j_1}$与子序列$T_{j_k},\cdots,T_{j_{p+1}},\cdots,T_{i_k}$都是回文的,且它们长度的和为$2k$,那么,$T$含有一个长度至少为$k$子回文串。相似地,若是$i_{p+1}=j_{p+1}$是子序列$T_{i_1},T_{i_p},T_{i_{p+1}},T_{j_p},\cdots,T_{j_1}$与子序列$T_{j_k},\cdots,T_{j_{p+1}},\cdots,T_{i_k}$都是回文的,且它们的长度的和为$2k$,那么$T$含有一个长度至少为$k$的子回文串。所以,咱们证实了$T$与$T'$的LCS的长度其实是$T$的最长(不必定连续)子回文串的长度。如今,咱们对改变至多$K$个$T$中的字符能构成的最长回文子的串长度感兴趣。定义$DP[l][r][x]$为在$T[l..r)$($T$的一个子串)经过修改至多$x$个$T$中的字符构成的最长可能回文子串长度。时间复杂度为$O(N^3)$。

    E - Ball Eat Chameleons


    Time limit : 2sec / Memory limit : 256MB

    Score : 1200 points

    Problem Statement

    In Republic of AtCoder, Snuke Chameleons (Family: Chamaeleonidae, Genus: Bartaberia) are very popular pets. Ringo keeps N Snuke Chameleons in a cage.

    A Snuke Chameleon that has not eaten anything is blue. It changes its color according to the following rules:

    • A Snuke Chameleon that is blue will change its color to red when the number of red balls it has eaten becomes strictly larger than the number of blue balls it has eaten.
    • A Snuke Chameleon that is red will change its color to blue when the number of blue balls it has eaten becomes strictly larger than the number of red balls it has eaten.

    Initially, every Snuke Chameleon had not eaten anything. Ringo fed them by repeating the following process K times:

    • Grab either a red ball or a blue ball.
    • Throw that ball into the cage. Then, one of the chameleons eats it.

    After Ringo threw in K balls, all the chameleons were red. We are interested in the possible ways Ringo could have thrown in K balls. How many such ways are there? Find the count modulo 998244353. Here, two ways to throw in balls are considered different when there exists i such that the color of the ball that are thrown in the i-th throw is different.

    Constraints
    • 1≤N,K≤5×105
    • N and K are integers.

    Input

    Input is given from Standard Input in the following format:

    NK
    Output

    Print the possible ways Ringo could have thrown in K balls, modulo 998244353.


    Sample Input 1

    Copy

    2 4
    Sample Output 1

    Copy

    7

    We will use R to represent a red ball, and B to represent a blue ball. There are seven ways to throw in balls that satisfy the condition: BRRR’, RBRB’, RBRR’, RRBB’, RRBR’, RRRB’ and RRRR’.


    Sample Input 2

    Copy

    3 7
    Sample Output 2

    Copy

    57

    Sample Input 3

    Copy

    8 3
    Sample Output 3

    Copy

    0

    Sample Input 4

    Copy

    8 10
    Sample Output 4

    Copy

    46

    Sample Input 5

    Copy

    123456 234567
    Sample Output 5

    Copy

    857617983

    题解:

    若下列条件之一成立,则一只蝴蝶会变红:
    • 它吃的红球比蓝球多。
    • 它吃的红球与蓝球数量(非零)同样多,且它吃的最后一个球为蓝色。

    假设共有$R$个红球,$B$个蓝球。对于每一对$(R,B)$使得$R+B=K$,咱们将会计算$R$个红球与$B$个蓝球的合法子序列个数。若一个$R$个红球的序列与$B$个蓝球的序列能够被分红$N$个不相交(可能不连续)的子序列使得对于每一个子序列知足上面的一个或两个条件,则这个序列是合法的。而后须要计算这样的序列的个数。

    当$R<B$时:

    明显地,答案为$0$。

    当$R=B$时:

    在这种状况下,全部子序列必定包含相等数目的红球和蓝球,且以一个蓝球结尾。所以,下列结论是显然的:

    • 序列的最后一个元素必定是一个蓝球。
    • 必定能够选出$N$个不相交子序列“红 蓝”。

    对称地,能够证实这些条件成立:选择$N$对不一样的蝴蝶中的每一对,选择剩下的其它的最后一个球是蓝色的。

    如今,在平面直角坐标系中画出子序列的图像。一个子序列链接一条由$(0,0)$到$(R,B)$的路径。咱们从左到右看这个序列,当看到一个红色(蓝色)的元素时,向右(上)扩展路径。

    而后,上面的条件与下列条件有关:

    • 路径是一条从$(0,0)$到$(R,B)$且只向上和右走的路径,且通过点$(R,B-1)$。
    • 路径不会到达知足$y-x\le B-N+1$的区域。

    路径数目能够在$O(1)$的时间内计算,稍后将会描述。

    当$R>B$时:

    与上述状况很是相似,可是这一次咱们对最后一个球的颜色不感兴趣。一个序列知足题目条件,当:

    • 序列必须能够选择$\max\{N-(R-B),0\}$个不相交子序列“红 蓝”。

    (注意这一次任意红球数目大于蓝球数目的蝴蝶是“其它蝴蝶”),若是用路径的语言描述,则:

    • 路径是一条从$(0,0)$到$(R,B)$且只向上和向右走的的路径。
    • 路径不会到达知足$y-x\le R-N+1$的区域。

    所以,咱们可以在$O(1)$的时间内计算出$(R,B)$的答案,算法复杂度为$O(K)$。剩下的最后的事是对知足下列条件的路径数目的计算:

    • 路径是一条从$(0,0)$到$(X,Y)$且只向上或向右走的路径。
    • 路径不会到达知足$y-x\le T$的区域。

    这是一个经典问题(卡特兰数)。

    由于咱们能够容易地计算出从$(0,0)$到$(X,Y)$的路径数,咱们对知足至少进入一次区域$y-x\le T$的路径(称为坏路径)的数量感兴趣。

    制定一条坏路径$P$,假设$P$在点$(p,q)$第一次进入区域$y-x\le T$,定义一条路径$Q$以下:

    • 从$(0,0)$到$(p,q)$,$Q$与$P$一致。
    • 在此以后,$Q$是$P$的反射:当$P$向右走,$Q$向上走,当$P$向上走,$Q$向右走。

    而后,$Q$是一条从$(0,0)$到$(Y-T,T+X)$的路径(注意$p-q=T$)。

    这给出了一个坏路径与一条从$(0,0)$到$(Y-T,T+X)$的路径的双射。

    所以,这个问题的答案就是$\binom{X+Y}{X}-\binom{X+Y}{Y-T}$。

    F - Trinity


    Time limit : 6sec / Memory limit : 256MB

    Score : 1800 points

    Problem Statement

    We have an N×M grid. The square at the i-th row and j-th column will be denoted as (i,j). Particularly, the top-left square will be denoted as (1,1), and the bottom-right square will be denoted as (N,M). Takahashi painted some of the squares (possibly zero) black, and painted the other squares white.

    We will define an integer sequence A of length N, and two integer sequences B and C of length M each, as follows:

    • Ai(1≤iN) is the minimum j such that (i,j) is painted black, or M+1 if it does not exist.
    • Bi(1≤iM) is the minimum k such that (k,i) is painted black, or N+1 if it does not exist.
    • Ci(1≤iM) is the maximum k such that (k,i) is painted black, or 0 if it does not exist.

    How many triples (A,B,C) can occur? Find the count modulo 998244353.

    Notice

    In this problem, the length of your source code must be at most 20000 B. Note that we will invalidate submissions that exceed the maximum length.


    Constraints
    • 1≤N≤8000
    • 1≤M≤200
    • N and M are integers.
    Partial Score
    • 1500 points will be awarded for passing the test set satisfying N≤300.

    Input

    Input is given from Standard Input in the following format:

    NM
    Output

    Print the number of triples (A,B,C), modulo 998244353.


    Sample Input 1

    Copy

    2 3
    Sample Output 1

    Copy

    64

    Since N=2, given Bi and Ci, we can uniquely determine the arrangement of black squares in each column. For each i, there are four possible pairs (Bi,Ci): (1,1), (1,2), (2,2) and (3,0). Thus, the answer is 4M=64.


    Sample Input 2

    Copy

    4 3
    Sample Output 2

    Copy

    2588

    Sample Input 3

    Copy

    17 13
    Sample Output 3

    Copy

    229876268

    Sample Input 4

    Copy

    5000 100
    Sample Output 4

    Copy

    57613837

    题解:

    令$DP[k][p]$是$p$行$k$列网格图中的答案,附加条件是每一行至少有一个格子被涂黑。由于答案是$\sum DP[M][p]\binom{N}{P}$,从如今起,咱们将聚焦于对DP表的计算。固定一个$k$行$p$列的网格图(这一次,有些行多是空的)。咱们构造与网格图有关的三个矩阵$A,B,C$。若是咱们在右侧附加上第$(p+1)$列,这三个矩阵会发生什么?
    • 若是第$i$行在附加前非空,$A_i$会是$p+1$,不然$A_i$的值不变。
    • $B,C$的前$p$个元素不变。
    • 咱们获得两个值$B_{p+1},C_{p+1}$:这两个值依赖于新添加的一列。

    若是一个网格图每一行都非空,则咱们称这个网格图是好的。咱们能够按下述方法使用一个有$p$行$k$列的网格图构造一个$p+q$行$k+1$列的好的网格图。

    • 最初咱们有一个$p$行$k$列的好的网格图。
    • 在某些位置插入$q$个空行,如今咱们有了一个$p+q$行$k$列的网格图。
    • 在右边附加上一个新(没必要为空)列。在空行的格子必定是黑色的。如今咱们获得了一个有$p+q$行$k$列的好的网格图。

    按上述流程更新DP表。对每个三元组$(k,p,q)$按$k$的升序进行方程为"$DP[k+1][p+q]+=DP[k][p]\times$系数"的转移,系数是在上述流程中获得新的三个矩阵$A,B,C$的方案数:显然三个矩阵的初始值不影响系数。

    如何计算系数?

    若是$q=0$,咱们只关心$B,C$两矩阵的最后一个值,他们是新加入的一列中最靠上或最靠下的黑色格子的位置。有一种状况是新插入的一列是空列,有$\binom{p+1}{2}$种方案插入的一列不是空列。所以系数是$\binom{p+1}{2}+1$。

    若是$q>0$会发生什么?咱们在$p$个已有的行中插入$q$个新行。能够视做这是一个$p$个灰球和$q$个黑球的序列(黑球知足新插入的行),黑球的位置$A_l$知足$A_l=k+1$。

    更多的,咱们应该考虑矩阵$B,C$的最后一个元素,用球的语言就是知足黑球最靠左或最靠右的位置。然而,问题是,咱们不知道灰球的信息:它既能够是黑的,也能够不是黑的。数量的排列分析以下:

    • 想象一个有$p$个灰球和$q$个黑球的序列。
    • 考虑全部在最右边的一个黑球右边的全部灰球,能够从中选择至多一个并把它涂成红色。
    • 考虑全部在最左边的一个黑球左边的全部回球,能够从中选择至多一个并把它涂成红色。

    麻烦的部分是“最多”。咱们在两个端点各附加一个灰球,如今它变成如下所述:

    • 想象一个有$p+2$个灰球和$q$个白球的序列。两端点都是灰色的。
    • 考虑全部在最右边的一个黑球右边的全部灰球,能够从中选择一个并把它涂成红色。
    • 考虑全部在最左边的一个黑球左边的全部灰球,能够从中选择一个并把它涂成红色。

    如今咱们能够把红球看成黑球,如今就是一个有$p$个灰球和$q+2$个黑球的序列,所以,系数就是$\binom{p+q+2}{q+2}$

    如今咱们算出了系数,这个算法时间复杂度为$O(N^2M)$,能够得到部分分。

    如何改进?定义矩阵$x,y$,初始$x$为给定,$y$为空。对于每对$(p,q)$知足$q>0$,咱们按如下进行运算:

    \begin{equation*}y_{p+q}+=x_p\times\binom{p+q+2}{q+2}\end{equation*}

    咱们能够忽略$q=0$的状况,由于明显地能够在$O(N)$时间内完成。)

    咱们想快速地进行这个操做。仔细观察系数:

    \begin{equation*}\binom{p+q+2}{q+2}=\frac{(p+q+2)!}{p!}\end{equation*}

    所以,令$X_p=\frac{x_p}{p!},Y_q=\frac{y_q}{(q+2)!}$,能够变形以下:

    \begin{equation*}y_{p+q}+=X_p\times\frac{1}{(q+2)!}\end{equation*}

    这是一个标准的复杂度,而且能够在$O(N\log N)$地时间内完成。整个算法总的时间复杂度为$O(NM\log M)$。

相关文章
相关标签/搜索