Codeforces Educational #187

A

The question asks how to stack towers such that each block carries on top of it blocks with sum of at most the durability of that block. In particular, the blocks all have the same weight and durability. Hence, the limiting factor for how many blocks can be stacked is the bottom block, namely it can carryd/m\lfloor d/m\rfloorblocks for a maximum ofd/m+1\lfloor d/m\rfloor+1blocks per tower. Ceil-Divide the total number of blocks by that and get the minimum number of towers required.

B

A number is beautiful iff F(F(x))=F(x)F(F(x))=F(x) where F(x)F(x) is the sum of digits of xx. In particular, we claim that F(x)=x    x[0,9]F(x)=x\iff x\in[0,9]. The reverse direction is trivial. For the forward direction, consider the contrapositive, namely x∉[0,9]    F(x)xx\not\in[0,9]\implies F(x)\ne x. Specifically, consider arbitrary x=xnxn10x=x_nx_{n-1}\dotsx_0 with xn0x_n\ne 0 and n1n\ge 1. We claim that i=0nxix=i=0n10ixi\sum_{i=0}^n x_i\ne x=\sum_{i=0}^n10^ix_i This is because this requires xi(10i1)=0x_i(10^i-1)=0 for all ii, which is impossible since xn0x_n\ne 0 and 10n1010^n-1\ne 0.

Hence from above, we know xx is beautiful     F(x)[0,9]\iff F(x)\in[0,9].

To reduce the sum of the digits to under 9, we greedily reduce the sum of the digits by removing the largest ones first. We remove the digits until there are no more left.

Remember the edge case that the leading digit cannot be less than 1. Hence, we register the leading digit as one less in the frequency table, namely treating it as if we could remove n-1 from the sum.

C

We can solve this problem with binary search. Namely, we can find nn as whether a sequence of aia_i of length nn can meet the conditions is monotonic.

Consider a particular nn. We will try to kill all of the bits of ss seperatly. In particular, for a given set bit of ss, namely sis_i (set bit at position ii), we find some mjm_j such that ij0i\ge j\ge 0. Namely, we need 2ji2^{j-i} copies of mjm_j. In the beginning, we say there are nn copies of mjm_j to use for each jj (in particular each aia_i can have up to all mjm_j bits set). We subtract as described above, and if and only if there is always enough mjm_js to subtract then there is a solution.

D

We solve this problem with an approach similar to a prime number sieve. We keep sieve[i]sieve[i] such that sieve[i]sieve[i] gives the number of unique elements of aa which divide ii. This can be constructed by iterating through all elements of aa (namely aia_i), then incrementing sieve[ai],sieve[2ai],sieve[a_i], sieve[2a_i], \dots.

We can consider three types of elements, elements which are divisible by all aa, elements which are divisible by some aa, and all of the elements. We let x3x_3 be the elements in the first set, x2x_2 be the elements in the second set but not in the first set (divisible by some aa but not all aa), and x1x_1 which are the rest (namely divisible by no aa). Alice can kill a number if there is some element of aa which divides it. Namely, Alice can kill the elements in x2,x3x_2,x_3. Bob can kill a number if NOT all elements of aa can divide it. Namely, Bob can kill the elements in x1,x2x_1,x_2.

Suppose there were only elements in x2x_2. We claim that the first grabber wins     x21(mod2)\iff |x_2|\equiv1\pmod 2.

If x3=x1|x_3|=|x_1|, then the winner of x2x_2 grabbing (described above) can force a win by forcing a win condition on x2x_2, and copying the opponent on x1,x3x_1,x_3. If x3>x1|x_3|>|x_1| or x3<x1|x_3|<|x_1|, then the greater of x3x_3 and x1x_1 can force a win by using their extra number to force the other player to be the disadvantaged grabber in x2x_2.

A

cpp

B

cpp

C

cpp

D

cpp