tama-editorial
tama-editorial
Cisco Ortega
Contents
A) Nostalgia 2
B) Holy Molé 5
D) SRS 11
F) Digivisible 17
G) Supersetter 20
I) Stratosphere 27
1
A) Nostalgia
Subtasks 1 & 2
2
Subtask 3
You can compute the leftmost window in O(k) time, then each subsequent
window can be computed from the previous one in O(1) time by subtracting
out the old leftmost element and adding in the new rightmost element (of
the new window).
3
Subtask 4
which will give us the value of an that we want (if we speed up repeated
matrix multiplication using fast exponentiation).
Your solution should smell like this, or at least something close to this, though
you may choose to have slightly different “implementation details” with the
matrix you choose to construct.
4
B) Holy Molé
Subtasks 1 & 2
For small n, write a brute forcer that directly simulates the problem (or do
that on pen and paper).
So, to count how many bowls have been touched by r angels, we can equiva-
lently count how many ways there are to choose r out of the n angels (to be
the ones to touch those bowls). Thus, the answer is
n
X n
.
r=m
r
Because n is small, you can compute the binomial coefficients by just about
any method and it’ll probably work out.
5
Subtask 3
But we can precompute all values of k! for k from 1 to n in just O(n) time—
use the fact that k! = k · (k − 1)!
6
Subtask 4
Do the digits of n and m look peculiar? Note that m is really close to n/2.
Specifically, m − n/2 = 106 only.
We know the sum of all the binomial coefficients is 28 . We can subtract out
8
+ 84 + 85 (the central blue part), then divide the result by two (to get
3
the total of just one orange part).
We only have to evaluate 2 × 106 binomial coefficients for the “central blue
part” of our actual problem, which sounds reasonable. But we still need to
evaluate gigantic binomial coefficients very quickly.
The key is that p = 107 + 19 is also quite small, and also it is prime. So,
leverage this small prime modulus. Here are some theorems you could use:
7
C) The Amazons in their Prime
Subtasks 1 & 2
n n
In one session, Maxi completes k
reps and Mini completes k
reps.
and X X jnk
m=
ℓn ≤n≤rn ℓk ≤k≤r
k
k
then compute M − m.
8
Subtask 3
However, nk and nk are almost exactly the same, except one rounds up
and the other rounds down. In fact, they are only exactly the same when k
divides n exactly and there is no remainder. So,
(
lnm jnk 0, k divides n
− =
k k 1, otherwise.
Thus, for each n from ℓn to rn , we want to count how many of its divisors
lie in the range [ℓk , rk ], which can be computed in O(rn lg rn ) by a slight
modification of a standard divisor sieve.
9
Subtask 4
(that is, both: n ranges from 1 to N , and k from 1 to K) then we can solve
the original problem (kind of similar to 2D prefix sums’ flavor).
Let’s unpack how the divisor sieve works, mathematically. It’s just an inter-
change of summations:
X X X X
(1 if k divides n, else 0) = (1 if k divides n, else 0)
1≤n≤N 1≤k≤K 1≤k≤K 1≤n≤N
X
= (count of multiples of k that are in [1, N ]).
1≤k≤K
It turns out this is nicer, because enumerating multiples is much easier than
finding divisors. The multiples of k go: k, 2k, 3k, . . . , therefore:
X X N
(count of multiples of k that are in [1, N ]) = .
1≤k≤K 1≤k≤K
k
√
We can evaluate this sum in O( N ) using the following “standard” fact:
• Let √
N be fixed and let k vary. Then, the expression Nk takes on only
≈ 2 N different values:
√
◦ Each of k = 1, 2, 3, . . . , N yields a different unique value of Nk
√
◦ For each of v = 1, 2, 3, . . . , N , consider the equation Nk = v;
10
D) SRS
Subtasks 1 & 2
(s1 + s2 + s3 + · · · + sn )(s1 + s2 + s3 + · · · + sn ).
So from now on, we’ll only care about computing the sum of the first n entries
in the SRS. Just square that result to get the true final answer.
Fun fact: In the OEIS, you’ll find that the SRS’ “proper” name is the
Golomb sequence!
11
Subtask 3
Naively compute the first few terms of the sequence, just like you would for
Subtasks 1 and 2. These first few terms tell us more about an even longer
prefix of the sequence.
More precisely, the first t terms of the sequence allow us to then infer proper-
ties about the first ti=1 si terms of the sequence, because we know the SRS
P
Concretely, for example, knowing that the first five terms are [1, 2, 2, 3, 3]
allows us to deduce that the first 1 + 2 + 2 + 3 + 3 = 11 terms
are [1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5] (the appearances of the values up to 5).
Pt
Let t be the first index such that i=1 si ≥ n. Then, the sum of the first n
terms of the SRS consists of
t−1
X
si × i + (some “incomplete” portion of the subsegment of t’s)
i=1
12
Subtask 4
Take this reasoning and apply it one level deeper. As a concrete example:
In general:
• From here, we can compute the ℓ such that sj = i for all j ∈ [ℓ, ℓ + si ).
• From here we know then that there is a part of the sequence that goes,
That is, knowing the first t terms of the sequence then lets us compute the
first ti=1 i × si terms of the sequence. Just as before, take the first t such
P
that ti=1 i × si ≥ n. Then, the sum of the first n terms in the SRS is again
P
It turns out that t that is just a little bit bigger than 107 is enough to reach
n = 1018 using this method.
13
E) Sussy Spanning Trees
Subtasks 1 & 2
14
Subtask 3
We prime factorize,
15
Subtask 4
We’re still going to use the Chinese Remainder Theorem. We prime factorize,
The largest prime factor is now too large for quadratic time solutions.
Now, what if we focus on all terms in the sequence that have the same
remainder r modulo m? Concretely, for example, suppose m = 9, and we
look at adding up only the terms that are congruent to r = 4, modulo 9:
This now gives a solution that runs in linearithmic time for each prime factor.
16
F) Digivisible
Subtask 1
This makes it tractable as a classic “How many n-digit numbers satisfy this
property...?” introductory combinatorics problem.
17
Subtask 2
18
Subtasks 3 & 4
You can maybe come up with some ad hoc casework solution for Subtask 3
which uses the choose function (considering there are divisibility tests for 3
and 6 and 9). But I’ll just jump to talking about the general method now.
Consider this simpler task: How many n-digit numbers using only the digits
from some set S are divisible by m?
This has a pretty classical solution using DP. Construct the number digit-by-
digit, at each step maintaining only the value modulo m of the prefix you’ve
constructed so far. Your state should look something like dp(i, r), where i
is the index of the digit we’re currently at, and r ∈ [0, m) is the value of the
prefix so far (modulo m).
For the transition, consider each possible next digit to append. To append
a new digit, recall that concatenating d to the end of some number n is
equivalently to performing 10n + d.
For digivisibility, we just need to modify this solution slightly. This is enough
to solve Subtask 3.
• Keep track of the value of the prefix so far, modulo m := 2520, which
is lcm(1, 2, . . . , 9).
◦ It can be shown that if d | m, then: n mod d = (n mod m) mod d.
• Also include in our state a bitmask mask which keeps track of which
digits we’ve used so far, since this tells us the digits whose divisibility
we need to check when we’re done constructing the number.
19
G) Supersetter
Subtasks 1 & 2
20
Subtask 3
For example, suppose we have 1234567890 and focus on what if 3456 formed
a subsegment. Then, the number would look something like
????(34563456)????????
Next, we need to find out how many partitions there are that have e.g. 3456
as one contiguous block. This is not too hard either—it consists of, indepen-
dently, determining how to partition the digits to its left, and determining
how to partition the digits to its right.
For example, there are 22−1 ×24−1 partitions of the digits of 1234567890 that
have 3456 as a contiguous block, therefore we must add 3456345600000000
to our sum a total of that many times.
Putting that all together, we have a formula for the contribution of some
[ℓ, r] subsegment, so we can just do a summation that ranges over all O(s2 )
such subsegments:
s X
X s
duplicate(n[l..r]) × 10appropriate no. of trailing zeros
×
l=1 r=ℓ
Even if you implement these functions somewhat naively, cubic time solutions
should also be acceptable.
21
Subtask 4
Take the idea from Subtask 3 one step further. For each individual index i,
what is the contribution of the digit at this index i to the overall sum?
We once again consider all subsegments [ℓ, r] that contain i. For example,
suppose we have 1234567890 and focus on the digit 4, and suppose we con-
sider all cases where 3456 form a subsegment. Then, the contribution of
specifically 4 in this case is:
????(?4???4??)????????
In this case, similar to before: the values of ℓ and r determine the number
of trailing zeros in the contribution of the digit and of the duplicated digit.
So, we get a similar summation as the one in Subtask 3:
s X
X s
i X
duplicate(n[i], ℓ, r) × 10appropriate no. of trailing zeros
×
i=1 l=1 r=i
Fully write out all of the terms shown above, and then just do a lot of pen-
and-paper algebra on summations: interchange their order, perform index
shifts, factor out terms independent of the dummy variable, etc. etc. After
a lot of work, you should eventually up with something like this:
s
X s
X
n[i] · (some geometric series) + n[i] · (some other geometric series)
i=1 i=1
(or possibly more, depending on how messy you are with handling edge cases)
where each geometric series can be evaluate in logarithmic time, using its
summation formula (and modular multiplicative inverses).
This yields a solution which runs in linearithmic time (with respect to the
number of digits).
22
H) Expected Value of Civil War
Subtasks 1 & 2
23
Subtasks 3 & 4
First things first, you can generate primes using the Sieve of Eratosthenes.
Now, onto the actual problem. Let s be the sum of the first n prime numbers.
Then, express sum(T ′ ) as s − sum(T).
Let H be the set of all n/2 sized subsets of the first n prime numbers. Now,
we just do summation algebra with the given expression.
X X
(sum(T ) − sum(T ′ ))2 = (2 sum(T ) − s)2
T ∈H T ∈H
X
= (4 sum(T )2 − 4s sum(T ) + s2 )
T ∈H
X X X
=4 sum(T )2 − 4s sum(T ) + s2 1.
T ∈H T ∈H T ∈H
P
Part I: T ∈H 1
This is just counting the number of elements of H, i.e. the number of subsets
n
with n/2 elements out of a larger set of size n. But we know that’s just n/2 .
24
P
Part II: T ∈H sum(T )
Expand out sum(T ) and interchange the order of summations. Let P be the
set of the first n primes.
XX XX
p= p
T ∈H p∈P p∈P T ∈H
p∈T T ∋P
X X
= p 1.
p∈P T ∈H
T ∋p
25
sum(T )2
P
Part III: T ∈H
Again, expand out the inner summation and interchange the order of sum-
mations again.
2
X X X XX
p =
pq
T ∈H p∈P T ∈H p∈P q∈P
p∈T p∈T q∈T
XX X
= pq
p∈P q∈P T ∈H
T ∋p,q
XX X
= pq 1.
p∈P q∈P T ∈H
T ∋p,q
That is to say, consider all pairs of primes (p, q). We wish to count the
number of sets T which contain both p and q. But that’s again (mostly)
independent of p and q:
Directly evaluating the summation on the right in O(n2 ) gets you Subtask 3.
Computing it in linear time (which isn’t too hard) gets you Subtask 4.
!2
X
Hint: Consider again the expression p .
p∈P
26
I) Stratosphere
Subtask 1
You can enumerate all paths using a computer program (or by hand). Or,
you can do the Subtask 2 solution by hand.
27
Subtask 2
This is a classic DP problem. Let dp(i, j) be the sum of the thrills of all
paths that start at (i, j) and end on the bottom right corner.
Construct the path step by step. From some position (i, j), ask: Do I go
down, or do I go right? Either way, the path has already passed through cell
(i, j). Then, after the move, we need to consider the sum of the thrills of all
paths from our new position.
28
Subtasks 3 & 4
Note that each path can be uniquely identified by how many squares of our
path are in each row. For example, the path in the sample is the unique one
that passes through 1 thrice, through 2 once, and through 3 twice.
• 1 ≤ it ≤ c for each t;
•
P
it = r + c − 1.
This is some kind of convolution, and smells like a perfect place to apply
generating functions! Specifically, what we want is the coefficient of xr+c−1
in the polynomial:
29
introduce extra terms in the polynomial, the answer won’t change, as long
as all terms have degree c or higher. So, we can equivalently consider:
r
Y
r! ((tx)0 + (tx)1 + · · · + (tx)c−1 + (tx)c + (tx)c+1 + . . . )
t=1
Our finite geometric series becomes an infinite one, which has a nicer formula!
r
Y 1
r! .
t=1
1 − tx
At this point, you may recognize this as one of the generating functions of
the Stirling numbers of the second kind! Then, you can go to Wikipedia and
find an explicit formula that can be computed in O(r). But if not, we can
proceed—these next steps essentially provide a proof of that explicit formula.
Then, the coefficient of xc−1 overall can be computed as the sum of the
coefficient of xc−1 in each individual summand. But each individual summand
is an infinite geometric series, so it contributes kt · tc−1 to the sum.
To get Subtask 4, you have to compute them more smartly, which can be
done by noticing shared computations between each kt (instead of starting
over from scratch every time).
30
J) Roll the Dice
Subtasks 1 & 2
Note that for Subtask 2, cubic time solutions won’t pass. You can afford to
iterate over all subarrays, but you cannot afford to literally compute each
subarray’s value from scratch.
Fortunately, it’s not too hard to fix, and rather intuitively too—the result
of applying commands ℓ through r can be found by: taking the results from
applying commands ℓ through r − 1, and then applying command r.
So, each of the O(n2 ) subarrays can be processed in O(1) time only.
31
Subtask 3
Let dpr (x) count the number of subarrays whose right endpoint is r, and
which result in face x facing upwards. Let y be the face such that: if face y
is on top, then performing a right hand twist around face ar results in face
x now being on top.
Here, the (1 if y = 1, else 0) term corresponds to the fact that we can start a
new subarray here whose left endpoint is r (but as per the problem statement,
a fresh die always start with 1 facing up).
32
Subtask 4
Note that the sequence b (and thus also the sequence a) is periodic, with
period 218 . To solve the problem for n = 1018 , we just abuse this periodicity
with standard period-bashing techniques, since the same 218 actions will be
repeated over and over again.
We have six linear recurrence relations that evolve in lockstep with each
other. Thus, we can encode each possible transition as one of six possible
matrices (one for each possible value of ai , i.e. which face we’re twisting
around).
In order to get the sum of these values for all r from 1 to n, we have a few
options:
• Augment the matrices so that we also keep track of the running sum
for each face.
33