提示:若是公式掛了請多刷新幾回,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≤i≤N), 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≤i≤N)
- 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:
.
,<
,>
,^
andv
. 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, andB
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≤i≤N) is the minimum j such that (i,j) is painted black, or M+1 if it does not exist.
- Bi(1≤i≤M) is the minimum k such that (k,i) is painted black, or N+1 if it does not exist.
- Ci(1≤i≤M) 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)$。