AtCoder Regular Contest 059

Updated:

AtCoder Regular Contest 059

Source codesPermalink

SolutionsPermalink

C - いっしょ / Be TogetherPermalink

Solution #1Permalink

100t100 の全ての値について、 t に揃えるコストを全探索する。

Solution #2Permalink

Let f:RR as follows. f(x)=iN(xix)2. This is a convex quadratic function. We have the following. f(x)=iN2(xix). Let x0=(iNxi)/N be its unique root. f attains its minimum when x=x0.

Let n=(iNxi)/N, where the division is Z’s. Since f is a convex quadratic function, f:ZZ 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=B0x0=A0BN1xN1=AN1a0++aN1=CiNxaii. Here, a0++aN1=C means that (a0,,aN1)NN runs under the constraint a0++aN1=C. First, we execute deformation of A as follows. A=a0++aN1=CB0x0=A0BN1xN1=AN1iNxaii =a0++aN1=CiN(Bixi=Aixaii). Here, for iN, let gi:NN be defined as follows. gi(ai)=Bixi=Aixaii. Then, A is computed as follows. A=a0++aN1=CkNgk(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]=j1x=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,,M1, 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++ai1=jkigk(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 ai1 be fixed as l with l running [0,j]. Then, a0++ai2=jl, i.e., we have the following. dp[i][j]=a0++ai1=jkigk(ak) =jl=0gi1(l)a0++ai2=jlk(i1)gk(ak) =jl=0gi1(l)dp[i1][jl].

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 kN 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)PN1(X).

Remark #2Permalink

If Ak=Bk=xk holds for all kN, we have the following equation for i=1,,N and j=0,,C1. dp[i][j+1]=dp[i1][j+1]+xi1dp[i][j]. This is because, from (j+1) candies, we give one candy to (i1)-th child to obtain the latter term. If we give no candy to (i1)-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,,N1, for j=0,,i, we execute the followings. dp[i+1][j+1]+=2dp[i][j], dp[i+1][max(0,j1)]+=dp[i][j]. Here, the former line represents 0 and 1 key, wheres the latter one does B key.

OthersPermalink