AtCoder Regular Contest 059
Updated:
Source codesPermalink
SolutionsPermalink
C - いっしょ / Be TogetherPermalink
Solution #1Permalink
−100≤t≤100 の全ての値について、 t に揃えるコストを全探索する。
Solution #2Permalink
Let f:R→R as follows. f(x)=∑i∈N(xi−x)2. This is a convex quadratic function. We have the following. f′(x)=∑i∈N−2(xi−x). Let x0=(∑i∈Nxi)/N be its unique root. f attains its minimum when x=x0.
Let n=(∑i∈Nxi)/N, where the division is Z’s. Since f is a convex quadratic function, f:Z→Z attains its minimum at x=n or x=n+1.
D - アンバランス / UnbalancedPermalink
SolutionPermalink
アンバランスな文字数の定義には、過半数が同じ文字と書いてある。ということは、偶数文字数なら隣と同じ文字になっている部分があり、奇数文字数なら、隣と同じ文字になっている部分があるか、または 1 個飛ばしで同じ文字が積み上がっているかである。前者は 2 文字のアンバランスな部分文字列、後者は 3 文字のアンバランスな部分文字列を見つけたことになる。だから最初からこの 2 通りだけチェックしてやればよろしい。
Roughly speaking, if S+T is unbalanced, S or T is unbalanced. This is a key idea to consider the substrings whose length is 2 or 3.
E - キャンディーとN人の子供 / Children and CandiesPermalink
Assume 0-indexed.
ObservationPermalink
We would like to compute A=B0∑x0=A0…BN−1∑xN−1=AN−1∑a0+⋯+aN−1=C∏i∈Nxaii. Here, ∑a0+⋯+aN−1=C means that (a0,…,aN−1)∈NN runs under the constraint a0+⋯+aN−1=C. First, we execute deformation of A as follows. A=∑a0+⋯+aN−1=CB0∑x0=A0…BN−1∑xN−1=AN−1∏i∈Nxaii =∑a0+⋯+aN−1=C∏i∈N(Bi∑xi=Aixaii). Here, for i∈N, let gi:N→N be defined as follows. gi(ai)=Bi∑xi=Aixaii. Then, A is computed as follows. A=∑a0+⋯+aN−1=C∏k∈Ngk(ak).
Let M=410 for the upper bound for Bi’s.
DP for the coefficientPermalink
Second, we consider how to compute gi. Let vector<vector<mint>>
h be defined as follows.
h[i][j]=j−1∑x=0xi.
Then, it follows that
gi(ai)=h[ai][Bi+1]−h[ai][Ai].
What remains is to fill h.
DefinitionPermalink
The table h defined as (E.2).
Initial statePermalink
h[i][0]=0.
AnswerPermalink
The table h itself.
TransitionPermalink
For i=0,…,C, this problem is separated for each i. For j=0,…,M−1, we execute the following. h[i][j+1]=h[i][j]+ji.
DP for the answerPermalink
To compute (E.1), we convert it to DP.
DefinitionPermalink
vector<vector<mint>>
dp[i][j]= defined as follows.
dp[i][j]=∑a0+⋯+ai−1=j∏k∈igk(ak).
Initial statePermalink
dp[0][0]=1; otherwise 0.
AnswerPermalink
A=dp[N][C].
TransitionPermalink
For i=1,…,N, for j=0,…,C, we consider making ai−1 be fixed as l with l running [0,j]. Then, a0+⋯+ai−2=j−l, i.e., we have the following. dp[i][j]=∑a0+⋯+ai−1=j∏k∈igk(ak) =j∑l=0gi−1(l)∑a0+⋯+ai−2=j−l∏k∈(i−1)gk(ak) =j∑l=0gi−1(l)dp[i−1][j−l].
Time complexityPermalink
For DP for h, it takes O(CM)-time. For DP for dp, each transition takes O(C)-time, which result in O(NC2)-time. Hence, the total time complexity is O(CM+NC2).
Remark #1Permalink
We interpret the DP for dp from the viewpoint of polynomial. Let Pk(x)∈N[X] for k∈N as follow. Pk(X)=gk(0)+gk(1)X+⋯+gk(C)XC. Then, the answer is the coefficient of XC of P0(X)P1(X)…PN−1(X).
Remark #2Permalink
If Ak=Bk=xk holds for all k∈N, we have the following equation for i=1,…,N and j=0,…,C−1. dp[i][j+1]=dp[i−1][j+1]+xi−1dp[i][j]. This is because, from (j+1) candies, we give one candy to (i−1)-th child to obtain the latter term. If we give no candy to (i−1)-th child, it result in the former term.
F - バイナリハック / Unhappy HackingPermalink
SolutionPermalink
Let M=|S|. First, we calculate how many ways of typing that generate a string of the length M. This is done by DP argued later. Let A be this value. Then, how many of them generates the string S? These A ways generates 2M strings in the same number. Thus the answer is A/2M.
DP partPermalink
DefinitionPermalink
vector<vector<mint>>
dp[i][j]= the number of ways to generate strings whose length is j by using i types.
Initial statePermalink
dp[0][0]=1; otherwise 0.
AnswerPermalink
dp[N][M]/2M.
TransitionPermalink
For i=0,…,N−1, for j=0,…,i, we execute the followings.
dp[i+1][j+1]+=2dp[i][j], dp[i+1][max(0,j−1)]+=dp[i][j].
Here, the former line represents 0
and 1
key, wheres the latter one does B
key.