Problem Solving Journal 3
Published Oct 13, 2025
#problem-journal #math #cs #algorithmsProblems: Modular Tetration · 1-Regular Subgraphs of 2-Regular Directed Graphs · Propagation Queries on Trees, Revisited
Over the summer I indeed made it to Master on Codeforces, was quite happy about it. Still, Master isn’t nearly good enough to make it to ICPC WF or even qualify for the UBC team (there’s many strong people here). I’m first hoping I can make the team and then my goal will be to reach Grandmaster level by the time NAC comes around. I think I have the aptitude for GM.
Unfortunately my math olympiad skill has greatly deteriorated since high school. I don’t want to practice but I don’t want to regress on Putnam, that would be quite sad. Current plan is to lightly practice and see :)
I am too lazy to write things up which is why this “journal” is so infrequently updated.
Modular Tetration
For a positive integer a, we define a recurrence as , with .
We say that a positive integer is -tetrative if the sequence stabilizes to 1 modulo , that is, there exists such that for all .
For a given , calculate the density of the -tetrative integers. is given as a product of integers , where .
Here, the density of a set is the limit . Informally, it is the “proportion” of positive integers that are -tetrative.
You should find this in time.
—Source: Codeforces 2147G
Solution
First we do some number theory to characterize the -tetrative integers.
Consider the convergence of . We require
This means that , and , where is the radical. Of course, we also require . We can see that these three conditions are necessary, but actually also sufficient. The tetration process guarantees that eventually converges to , at which point is guaranteed.
That was the easy part, now we try to find the density of -tetrative integers. Let us fix , and then if some is -tetrative with order radical , then . But . So considering , the densitiy of this equvalence class is . Therefore, we are looking to find
Where is the number of residues with .
CRT Decomposition
First let’s try this using Chinese Remainder Theorem (CRT) decomposition to enumerate . Write . We know that given , since and it must be the case that . We can decompose where . However, we can enforce , since .
Then the question is, how many have order ? The answer is . This is trivial for , and for other primes we note that is cyclic, so it follows by considering powers of a primitive root. Therefore, this contributes . We can do this for each , and take a direct product by combining
The denominator gets the new radical of the new order of each from the CRT. Done correctly this is a solution, which we know can be , but in practice computing and storing the direct sums can take a lot of memory.
Sylow Decomposition
We can try to a different way of resolving . The key here is the fact that for each Sylow -subgroup of ,
Which is extremely useful here, because we now note that we can instead consider the count over subsets of for .
What’s left is to recover , which is an immediate application of mobius inversion. In fact at this point we know we are done since the set of multiplicative functions is a group under Dirichlet convolution. We therefore must obtain a multiplicative function, so we can easily calculate it after a prime sieve.
And since the possible is a set of divisors, iterating over possible we also get something nice.
Where . Of course we can prime sieve this and then compute the answer in
This is the first time I really got the chance to use university math on a CP problem and was also my first LGM (3000+) rating solve!!!
1-Regular Subgraphs of 2-Regular Directed Graphs
Given a 2-regular directed graph on vertices, how many 1-regular directed subgraphs does it have?
Specifically, show that it is a power of two greater than 1.
—USA TSTST 2018 Problem 2
We construct the bipartition , with edge if and only if u has a directed edge to . Although an injective mapping from the orignal graph, this helps to see the existance of such a subgraph, in the form of a perfect matching that corresponds to the original graph!
Note that each vertex in has degree , so we can appeal to Hall’s Marriage Theorem and the pigeonhole principle to show that . Note that the complement subgraph of any 1-regular subgraph is also 1-regular, it remains to prove the powers of two condition.
To realize the powers of two, here is a nice linear algebraic way to force it in . We can associate with each edge an binary variable of whether it is in the subgraph. Then for each node we have two linear restraints: the outgoing edges sum to and incoming edges sum to .
Therefore this system is a linear system in , and so its solution space is an affine vector space. Thus we are done!
Propagation Queries on Trees, Revisited
Half a year ago in my first problem solving journal I outlined a cool approach for supporting “propagation queries” on trees. However, it didn’t fully work because I had yet to learn data structures that support efficient path arithmetic with updates on trees.
I recently learned Heavy-Light Decomposition, and now the approach works! This led me to solve a 3200 in less than an hour.
You have a rooted tree on vertices, with binary labels. Initially all the nodes are white. Support these query operations efficiently:
- Given some node, if it is white mark it black, if it is already black repeat this operation on all its children.
- Given some node, mark all subtrees of this node white.
- Given some node, find its color.
—Source: Codeforces 1017G
Solution
Read the original post!
We no longer try Euler tour but instead use Heavy-Light Decomposition (HLD) to support the root path suffix maximums.
What we can do now is initialize all nodes to , update the node values using HLD by adding and compute suffix maximums with HLD and the corresponding Lazy segment tree, with the sign determining color. This supports query 1 and query 3. Query 2 on vertex is easy to add, simply reset all subtree values in the segment tree to -1, then perform query 3 on . If the answer is , subtract from to reset it.
This was a 3200 problem that I solved in under an hour!