AtCoder Grand Contest 021完整题解

提示:如果公式挂了请多刷新几次,MathJex的公式渲染速度并不是那么理想。

总的来说,还是自己太弱了啊。只做了T1,还WA了两发。今天还有一场CodeForces,晚上0点qwq... 题解还是要好好写的。

A - Digit Sum 2


Time limit : 2sec / Memory limit : 256MB

Score : 300 points

Problem Statement

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

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

  • Input

    Input is given from Standard Input in the following format:

    N
    Output

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


    Sample Input 1

    Copy

    100
    
    Sample Output 1

    Copy

    18
    

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


    Sample Input 2

    Copy

    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$,找出它最大的数字和,一个数字的数字和定义为这个数字的每一位数的和。

    题解:

    很明显贪心是一种正确的思路。因为需要最大化数字和,所以对于数字的每一位,我们都要使得这一位数尽可能大。考虑两种情况(三种?):

  • 第一种情况:对于形如$c99cdots9$的数字,我们要最大化数字和,只需要将最高位的数字减$1$,然后将其余各位变为$9$即可,可以证明这样是最优的。设数的位为$K$,则有$ans=c-1+9 imes(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"
",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^2log N)$的解法。固定一个洞$p$,计算从$p$到每个其它的洞的路径,然后将这些路径排序。$p$被选择的概率是$max{两条相邻的最长间隔-pi,0}/(2pi)$。

    注意到我们可以把复杂度改进到$O(Nlog N)$。取出所有洞构成的凸包。如果一个洞$p$不是凸包的一个顶点,$p$点被选中的可能性为$0$。否则,令$q,r$为邻接$p$的凸包的两个定点。那么,$p$被选中的概率是$(pi-angle qpr)/(2pi)$。

    题解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 imes 2$和$B$个$2 imes 1$的骨牌覆盖一个$N imes M$的矩阵,骨牌不能翻转,问能否做到,若能做到输出任意一种方案。

    题解:

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

  • $A+Bleleftlfloorfrac{NM}2 ight floor$(比较区域);
  • $Ale Nleftlfloorfrac{M}{2} ight floor$(每行最多放$leftlfloorfrac{M}{2} ight floor$个骨牌);
  • $Ble Mleftlfloorfrac{N}{2} ight floor$(每列最多放$leftlfloorfrac{N}{2} ight floor$个骨牌)。
  • 定义$S:=leftlfloorfrac{NM}{2} ight floor,S_A:=Nleftlfloorfrac{M}{2} ight floor,S_B:=Mleftlfloorfrac{N}{2} ight floor$,则$A+Ble S,Ale S_A,Ble S_B$一定成立。另一方面,我们可以想出以下方法来放置骨牌:

  • 首先,从左上角放$2 imes 2$的骨牌(最自然的想法),我们会得到$leftlfloorfrac{N}{2} ight floorleftlfloorfrac{M}{2} ight floor$个正方形。
  • 水平或垂直划分每个正方形:对于每一个正方形,得到两个同样类型的骨牌。
  • 特别地,当$N$是奇数时,可以放置$leftlfloorfrac{M}{2} ight floor$个$1 imes 2$的骨牌,当$M$是奇数时,则可以放置$leftlfloorfrac{N}{2} ight floor$个$2 imes 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,Mgeq 3$。

    在右下角取出一个$3 imes 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 imes 2$的骨牌覆盖偶数个黑色格子,用$2 imes 1$的骨牌覆盖奇数个黑色格子。因此,$2 imes 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-xle B-N+1$的区域。

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

    当$R>B$时:

    与上述情况非常类似,但是这一次我们对最后一个球的颜色不感兴趣。一个序列满足题目条件,当:

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

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

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

    因此,我们能够在$O(1)$的时间内计算出$(R,B)$的答案,算法复杂度为$O(K)$。剩下的最后的事是对满足下列条件的路径数目的计算:

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

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

    因为我们可以容易地计算出从$(0,0)$到$(X,Y)$的路径数,我们对满足至少进入一次区域$y-xle T$的路径(称为坏路径)的数量感兴趣。

    制定一条坏路径$P$,假设$P$在点$(p,q)$第一次进入区域$y-xle 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)$的路径的双射。

    因此,这个问题的答案就是$inom{X+Y}{X}-inom{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]inom{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] imes$系数"的转移,系数是在上述流程中得到新的三个矩阵$A,B,C$的方案数:显然三个矩阵的初始值不影响系数。

    如何计算系数?

    如果$q=0$,我们只关心$B,C$两矩阵的最后一个值,他们是新加入的一列中最靠上或最靠下的黑色格子的位置。有一种情况是新插入的一列是空列,有$inom{p+1}{2}$种方案插入的一列不是空列。因此系数是$inom{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$个黑球的序列,因此,系数就是$inom{p+q+2}{q+2}$

    现在我们算出了系数,这个算法时间复杂度为$O(N^2M)$,可以获得部分分。

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

    egin{equation*}y_{p+q}+=x_p imesinom{p+q+2}{q+2}end{equation*}

    我们可以忽略$q=0$的情况,因为明显地可以在$O(N)$时间内完成。)

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

    egin{equation*}inom{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)!}$,可以变形如下:

    egin{equation*}y_{p+q}+=X_p imesfrac{1}{(q+2)!}end{equation*}

    这是一个标准的复杂度,并且可以在$O(Nlog N)$地时间内完成。整个算法总的时间复杂度为$O(NMlog M)$。

原文地址:https://www.cnblogs.com/TheRoadToAu/p/AtCoder-AGC-021.html