## March 31, 2019 Single Round Match 754 Editorials

Single Round Match 754*Saturday, March 30th, 2019 *

**MissingDwarf**

Used as: Division Two – Level One:

Value250 Submission Rate141 / 155 (90.97%) Success Rate126 / 141 (89.36%) High Scorewinterflamefor 248.62 points (2 mins 7 secs)Average Score214.04 (for 126 correct submissions)

#### Statement

You’re given [latex]6[/latex] numbers [latex]h_1,\dots,h_6[/latex]. You need to find smallest number [latex]h_7[/latex] such that [latex]h_7 > h_k[/latex] for [latex]k=1..6[/latex] and mean value of all numbers [latex]\frac{h_1 + \dots + h_7}{7}[/latex] is integer.#### Solution

Find maximum [latex]h_k[/latex], start with [latex]h_7[/latex] being equal this number plus one, increase it until mean value becomes integer. You will do at most [latex]6[/latex] increases, thus running time is [latex]O(1)[/latex].int getHeight(vectorotherHeights) { int sm = accumulate(begin(otherHeights), end(otherHeights), 0); int ans = 1 + *max_element(begin(otherHeights), end(otherHeights)); while((ans + sm) % 7) { ans++; } return ans; }

**SeventhPowers**

Used as: Division Two – Level Two:

Value500 Submission Rate114 / 155 (73.55%) Success Rate100 / 114 (87.72%) High Scoreongchuviettelfor 486.67 points (4 mins 43 secs)Average Score339.35 (for 100 correct submissions)

#### Statement

Consider integer [latex]A=a_{n-1}\dots a_1a_0[/latex], You’re given [latex]B=a_{0}^{7}+a_{1}^{7}+\dots+a_{n-1}^{7}[/latex]. Construct any [latex]A[/latex] which gives such [latex]B[/latex] having no leading zeroes and length at most [latex]500[/latex]. [latex]B[/latex] itself is at most [latex]10^7[/latex].#### Solution

One of possible ways to solve the problem is to greedily pick the largest digit [latex]x[/latex] such that [latex]x^7[/latex] is less than [latex]B[/latex], then subtract [latex]x^7[/latex] from [latex]B[/latex] and append it to the answer. It may be directly checked that this will provide short enough output for all possible inputsstring reconstructA(int B) { string ans; int t = 9; while(B > 0) { while(pow(t, 7) > B) { t--; } ans += '0' + t; B -= pow(t, 7); } return ans; }

**MoreSquares**

Used as: Division Two – Level Three:

Value1000 Submission Rate20 / 155 (12.90%) Success Rate4 / 20 (20.00%) High Scorekektakfor 586.75 points (28 mins 28 secs)Average Score500.18 (for 4 correct submissions)

Used as: Division One – Level One:

Value250 Submission Rate104 / 120 (86.67%) Success Rate56 / 104 (53.85%) High ScoreEgorfor 237.29 points (6 mins 38 secs)Average Score171.60 (for 56 correct submissions)

#### Statement

You’re given set of [latex]N \leq 3000[/latex] points [latex](x_1, y_1), \dots, (x_N, y_N)[/latex]. You have to calculate the number of point [latex](x,y)[/latex] such that if you add them to the set, the number of quadruples of points which form square will increase.#### Solution

If point [latex](x,y)[/latex] increases the amount of squares among points, there should be two points [latex](x_i,y_i)[/latex] and [latex](x_j,y_j)[/latex] among the set forming the diagonal of that square, we can iterate over all such pairs in [latex]O(N^2)[/latex].For given pair we can determine that center of the square is at the point [latex]\frac{(x_i+x_j,y_i+y_j)}{2}[/latex] and two other corners are in positions [latex]\frac{(x_i+x_j,y_i+y_j) \pm (y_i-y_j,x_j-x_i)}{2}[/latex], thus if one of such points is present in the set and the other is not, you should add the one which is not present to the set of points constituting the answer. Note that you can’t simply increment answer because single point may add several squares and it will be counted multiple times in such a case.

typedef int ftype; typedef complexpoint; #define x real #define y imag auto comp = [](const point& a, const point &b) { return make_pair(a.x(), a.y()) < make_pair(b.x(), b.y()); }; int countLocations(int N, int SX, int SY, vector Xprefix, vector Yprefix) { vector X(N), Y(N); int L = Xprefix.size(); for(int i = 0; i < L; i++) { X[i] = Xprefix[i]; Y[i] = Yprefix[i]; } for(int i = L; i < N; i++) { X[i] = ( X[i-1] * 47 + 42 ) % SX; Y[i] = ( Y[i-1] * 47 + 42 ) % SY; } set S(comp); for(int i = 0; i < N; i++) { S.insert(point{X[i], Y[i]}); } set T(comp); for(auto it: S) { for(auto jt: S) { if(it != jt) { point dir = jt - it; if((dir.x() & 1) != (dir.y() & 1)) { continue; } point kt = it + (dir + point(0, 1) * dir) / 2; point lt = it + (dir - point(0, 1) * dir) / 2; if(S.count(kt) && !S.count(lt)) { T.insert(lt); } } } } return T.size(); }

**OrthogonalProjections**

Used as: Division One – Level Two:

Value600 Submission Rate15 / 120 (12.50%) Success Rate6 / 15 (40.00%) High ScoreStonefeangfor 277.42 points (43 mins 9 secs)Average Score260.52 (for 6 correct submissions)

#### Statement

Consider set of [latex]N[/latex] distinct points [latex](x_1,y_1), \dots, (x_N,y_N)[/latex] and a line [latex]L[/latex]. If point [latex]X[/latex] lies on the line [latex]L[/latex], the orthogonal projection of [latex]X[/latex] onto [latex]L[/latex] is [latex]X[/latex] itself. Otherwise, the orthogonal projection of [latex]X[/latex] onto [latex]L[/latex] is the unique point [latex]Y[/latex] on [latex]L[/latex] such that [latex]XY[/latex] is orthogonal to [latex]L[/latex].Suppose you are given a finite sequence [latex]S[/latex] of points in the plane. Two lines [latex]L_1[/latex] and [latex]L_2[/latex] are equivalent if the orthogonal projections of points of [latex]S[/latex] onto [latex]L_1[/latex] are in the same order as the projections of points of [latex]S[latex] onto [latex]L_2[/latex]. You have to construct the set of [latex]N \leq 500[/latex] points having exactly given number [latex]n \leq 10^5[/latex] of equivalence classes.

#### Solution

First of all we have to determine how to calculate the number of equivalence classes for given set. Let’s look on two particular points [latex]X_i[/latex] and [latex]X_j[/latex]. What relative configuration can they have? If the line is orthogonal to [latex]X_i – X_j[/latex] then they have same projection on it, let’s call this line [latex]L_0[/latex]. Otherwise they lie in one order if line goes counter-clockwise from [latex]L_0[/latex] and in the other order if it goes clockwise from [latex]L_0[/latex].If we consider lines [latex]L_0[/latex] for all possible pairs of [latex](X_i, X_j)[/latex], they will split the unit circle in [latex]2k[/latex] segments in such a way that lines going through same segment have same configuration with respect to given set of [latex]N[/latex] points. Also each endpoint will have its own configuration different from configurations in segments. It would provide you with [latex]4k[/latex] configurations, but it also counts inverted configurations which should not be counted, thus the number of configurations will be exactly [latex]2k[/latex] where [latex]k[/latex] is the number of distinct [latex]X_i-X_j[/latex] directions up to [latex]-1[/latex] multiplier.

Some manual check also tells us that it’s impossible to obtain [latex]4[/latex] configurations and it’s possible to obtain exactly [latex]1[/latex] configuration if you use only one point. Otherwise making [latex]n[/latex] configurations is possible if and only if [latex]n[/latex] is even. Let’s consider one of possible explicit constructions to obtain [latex]n[/latex] configurations. Let [latex]t = \lfloor \sqrt{n} \rfloor[/latex], begin with [latex]t[/latex] points [latex](0,0), (0,1), \dots, (0,t-1)[/latex]. It will initialize our number of directions with [latex]1[/latex]. Now if we add arbitrary point [latex](x,y)[/latex] it will give us another [latex]t[/latex] directions. To fairly control number of new directions we will add points [latex](1,0), (1,t),(1,2t),\dots,(1,kt)[/latex], so with each new point we will have exactly [latex]t[/latex] new directions. But for final point you will have to add some number [latex]x[/latex] which is less or equal to [latex]t[/latex]. It can be done by placing point [latex](1,(k-1)t+x)[/latex] instead of [latex]kt[/latex].

For example, look on the following diagram:

- [latex]A-B-C[/latex] and [latex]D-E-F[/latex] lie on same lines having pairwise direction [latex](0,1)[/latex].
- [latex]DC,DB,DA[/latex] have directions [latex](-5,2),(-5,1),(-5,0)[/latex] respectively.
- [latex]EA,EB,EC[/latex] have directions [latex](-5,-1),(-5,-2),(-5,-3)[/latex] respectively.
- [latex]FC,FB,FA[/latex] have directions [latex](-5,-3),(-5,-4),(-5,-5)[/latex] respectively.

Note that because we took [latex]F[/latex] to be [latex]2[/latex] points above [latex]E[/latex] instead of [latex]3[/latex], exactly one direction to set of points [latex]\{A,B,C\}[/latex] was repeated. This solution works in [latex]O(\sqrt n)[/latex].

vectorgenerate(int n) { if(n == 1) { return {0, 0}; } else if(n == 2) { return {0, 0, 1, 1}; } else if(n == 4 || n % 2 == 1) { return {}; } else { n /= 2; vector ans; int t = 1; while((t + 1) * (t + 1) < n) { t++; } for(int i = 0; i < t; i++) { ans.insert(end(ans), {0, i}); } int cur = 0; ans.insert(end(ans), {1, 0}); n -= 1 + t; while(n) { int step = min(n, t); cur += step; n -= step; ans.insert(end(ans), {1, cur}); } return ans; } }

**RestoreDrawing**

Used as: Division One - Level Three:

Value900 Submission Rate5 / 120 (4.17%) Success Rate1 / 5 (20.00%) High Scoretouristfor 521.02 points (29 mins 9 secs)Average Score521.02 (for 1 correct submission)

#### Statement

You have to construct [latex]N \times M[/latex] grid [latex]N, M \leq 100[/latex] such that sizes of its 4-connected components are [latex]a_1,\dots,a_n[/latex] and sizes of its 8-connected components are [latex]b_1,\dots,b_m[/latex]. Where for arrays holds [latex]n,m \leq 20[/latex] and [latex]a_1 + \dots + a_n \leq 1500[/latex].#### Solution

Note that you may take arbitrary set of [latex]4[/latex]-connected components, join it via diagonals and obtain single [latex]8[/latex]-connected component. Thus the whole solution splits in two parts: splitting sizes of [latex]8[/latex]-connected components into sizes of [latex]4[/latex]-connected components and constructing the answer.First part is an instance of bin packing problem. Author's approach to solve it is to fix some order on set [latex]b[/latex] and calculate [latex]dp[mask][/latex] which is equal to [latex]1[/latex] if it's possible to construct prefix of [latex]b[/latex] (maybe with some remainder) such that it splits on two distinct subsets on [latex]b_1,b_1+b_2,b_1+b_2+b_3[/latex] and so on, it may be calculated in [latex]2^n \times n[/latex].

Tester approach is to use recursive function split(L, R, mask) which tries to split segment [latex]b_L, b_{L+1},\dots, b_{R-1}[/latex] using only items presented in [latex]mask[/latex]. To do this you split [latex][L,R)[/latex] into [latex][L,M)[/latex] and [latex][M,R)[/latex], iterate over all submasks of [latex]mask[/latex] and try to split subsegment [latex][L,M)[/latex] with such submask. If it was possible, you also try to split [latex][M,R)[/latex] with its complement. It's hard to estimate working time of such solution but rough upper bound is something like [latex]3^n[/latex] which, obviously, always way less on practice.

After you calculated possible splitting, you should compose [latex]4[/latex]-connected components with such sizes and make them into [latex]8[/latex]-connected components. One of possible ways to do it is to split the whole table by vertical line in half and fill 4-connected components alternatively, aligning them to the center. Another possible way is by using following pattern alternatively: Thus each 4-connected component is either single `#` or it is preceded and succeeded by single `#`, so that those single `#`'s maintain connection between 4-components into 8-component. Please feel free to inspect the code for better understanding.

const int maxn = 1 << 20; int sum[maxn]; templatevector split(T from, T to, int mask) { if(sum[mask] != accumulate(from, to, 0)) { return {}; } else if(to - from == 1) { return {mask}; } else { T mid = from + (to - from) / 2; for(int msk = mask; msk; msk = (msk - 1) & mask) { auto A = split(from, mid, msk); if(!A.empty()) { auto B = split(mid, to, mask ^ msk); if(!B.empty()) { A.insert(end(A), all(B)); return A; } } } return {}; } } vector restore(vector sizes4, vector sizes8) { sort(begin(sizes4), end(sizes4)); sort(begin(sizes8), end(sizes8)); int n = sizes4.size(); int mask_sz = 1 << n; for(int i = 0; i < n; i++) { sum[1 << i] = sizes4[i]; } for(int i = 1; i < mask_sz; i++) { int j = i & (i - 1); sum[i] = sum[j] + sum[i ^ j]; } auto msks = split(begin(sizes8), end(sizes8), mask_sz - 1); if(msks.empty()) { return {}; } else { const int MX = 100; vector ans(MX, string(MX, '.')); int h = 0; for(size_t i = 0; i < sizes8.size(); i++) { int cur = 0; for(size_t j = 0; j < sizes4.size(); j++) { if((msks[i] >> j) & 1) { ans[h][cur] = '#'; h++; if(sizes4[j] == 1) { cur ^= 1; } else { int t = cur; for(int k = 2; k < sizes4[j]; k++) { if(t >= MX) { t = cur; h++; } ans[h][t++] = '#'; } if(ans[h][cur] == '#') { h++; } ans[h++][cur] = '#'; cur ^= 1; } } } h++; } return ans; } }

By adamant

*Topcoder Member*

**Harshit Mehta**

Sr. Community Evangelist