Problem Solving Journal 3

Published Oct 13, 2025

#problem-journal #math #cs #algorithms

Problems: 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 {bn}\{b_n\} as bn=abn1b_n=a^{b_{n−1}}, with b0=1b_0=1.

We say that a positive integer aa is mm-tetrative if the sequence bb stabilizes to 1 modulo mm, that is, there exists N0N \geq 0 such that bn1(modm)b_n \equiv 1 \pmod{m} for all nNn \geq N.

For a given mm, calculate the density of the mm-tetrative integers. m1018m \leq 10^{18} is given as a product of integers m=xyzm = xyz, where x,y,z106x, y, z \leq 10^6.

Here, the density of a set SS is the limit limnS[1,2,,n]n\lim_{n \to \infty} \frac{|S \cap [1, 2, \dots, n]|}{n}. Informally, it is the “proportion” of positive integers that are mm-tetrative.

You should find this in O(polylog m)\mathcal{O}(\text{polylog } m) time.

—Source: Codeforces 2147G

Solution

First we do some number theory to characterize the mm-tetrative integers.

Consider the convergence of bn(modm)b_n \pmod m. We require

{bn1(modm)bn+1=abn1(modm)    bn0(modordm(a))\begin{cases} b_n \equiv 1 \pmod m \\ b_{n + 1} = a^{b_n} \equiv 1 \pmod m \implies b_n \equiv 0 \pmod{\text{ord}_m(a)} \end{cases}

This means that gcd(m,ordm(a))=1\gcd(m, \text{ord}_m(a)) = 1, and rad ordm(a)a\text{rad } \text{ord}_m(a) \mid a, where rad\text{rad} is the radical. Of course, we also require gcd(a,m)=1\gcd(a, m) = 1. We can see that these three conditions are necessary, but actually also sufficient. The tetration process guarantees that bnb_n eventually converges to 0(modordm(a))0 \pmod{\text{ord}_m(a)}, at which point bn1(modm)b_n \equiv 1 \pmod m is guaranteed.

That was the easy part, now we try to find the density of mm-tetrative integers. Let us fix k=rad ordm(a)k = \text{rad } \text{ord}_m(a), and then if some aa is mm-tetrative with order radical kk, then kak \mid a. But ka+tm    ktk \mid a + tm \iff k \mid t. So considering amodma \mod m, the densitiy of this equvalence class is 1/k1/k. Therefore, we are looking to find

kcount(k)k\sum_k \frac{\text{count}(k)}{k}

Where count k\text{count }{k} is the number of residues with rad ordm(a)=k\text{rad } \text{ord}_m(a) = k.

CRT Decomposition

First let’s try this using Chinese Remainder Theorem (CRT) decomposition to enumerate count(k)\text{count}(k). Write s=ordm(a)s = \text{ord}_m(a). We know that given m=piαim = \prod p_i^{\alpha_i}, since gcd(m,s)=1\gcd(m, s) = 1 and sϕ(m)s \mid \phi(m) it must be the case that s(pi1)s \mid \prod (p_i - 1). We can decompose s=sis = \prod s_i where sipi1s_i \mid p_i - 1. However, we can enforce si=ordpiαi(a)s_i = \text{ord}_{p_i^{\alpha_i}}(a), since ordpiαi(a)ϕ(piαi)\text{ord}_{p_i^{\alpha_i}}(a) \mid \phi(p_i^{\alpha_i}).

Then the question is, how many amodpiαia \mod p_i^{\alpha_i} have order sis_i? The answer is ϕ(si)\phi(s_i). This is trivial for pi=2p_i = 2, and for other primes we note that (Z/piαiZ)×(\mathbb{Z}/p_i^{\alpha_i}\mathbb{Z})^{\times} is cyclic, so it follows by considering powers of a primitive root. Therefore, this sis_i contributes ϕ(si)rad si\frac{\phi(s_i)}{\text{rad } s_i}. We can do this for each sis_i, and take a direct product by combining

ϕ(si)rad siϕ(sj)rad sj=ϕ(si)ϕ(sj)lcm(rad si,rad sj)\frac{\phi(s_i)}{\text{rad } s_i} \circ \frac{\phi(s_j)}{\text{rad } s_j} = \frac{\phi(s_i) \cdot \phi(s_j)}{\text{lcm}(\text{rad } s_i, \text{rad } s_j)}

The denominator gets the new radical of the new order ss of each aa from the CRT. Done correctly this is a O(d(si))O(\prod d(s_i)) solution, which we know can be o((rad m)ϵ)o((\text{rad } m)^{\epsilon}), 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 count k\text{count }{k}. The key here is the fact that for each Sylow pp-subgroup SpS_p of (Z/mZ)×(\mathbb{Z}/m \mathbb{Z})^{\times},

gSp    pord g(Z/mZ)×=Spg \in S_p \iff p \mid \text{ord } g \qquad (\mathbb{Z}/m \mathbb{Z})^{\times} = \prod S_p

Which is extremely useful here, because we now note that we can instead consider the count over subsets of k=qjk = \prod q_j for ϕ(m)=qiβi\phi(m) = \prod q_i^{\beta_i}.

dkcount d=Sqj=Sqj=qjβj\sum_{d \mid k} \text{count }d = \left| \prod S_{q_j} \right| = \prod \left| S_{q_j} \right| = \prod q_j^{\beta_j}

What’s left is to recover count k\text{count } k, 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.

count1=qjβj    count=qjβjμ\text{count} * 1 = \prod q_j^{\beta_j} \implies \text{count} = \prod q_j^{\beta_j} * \mu
count(k)=(qjβj1)\text{count}(k) = \prod (q_j^{\beta_j} - 1)

And since the possible kk is a set of divisors, iterating over possible kk we also get something nice.

kradϕ(m)gcd(k,m)=1count(k)k=qjK(1+qjβj1qj)\sum_{\substack{k \mid \text{rad} \phi(m) \\ \gcd(k, m) = 1}} \frac{\text{count}(k)}{k} = \prod_{q_j \mid K} \left( 1 + \frac{q_j^{\beta_j} - 1}{q_j} \right)

Where K=rad ϕ(m)/gcd(rad ϕ(m),rad m)K = \text{rad } \phi(m)/\gcd(\text{rad } \phi(m),\text{rad } m). Of course we can prime sieve this and then compute the answer in O(polylog m).\mathcal{O}(\text{polylog } m).\blacksquare

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 GG on nn 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 Hn,nH_{n, n}, with edge (u,v)(u, v) if and only if u has a directed edge to vv. 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 HH has degree 22, so we can appeal to Hall’s Marriage Theorem and the pigeonhole principle to show that WNH(W)|W| \leq |N_H(W)|. 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 F2\mathbb{F}_2. We can associate with each edge an binary variable xix_i of whether it is in the subgraph. Then for each node we have two linear restraints: the outgoing edges sum to 11 and incoming edges sum to 11.

Therefore this system is a 2n×2n2n \times 2n linear system in F2\mathbb{F}_2, and so its solution space is an affine vector space. Thus we are done! \blacksquare

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 nn vertices, with binary labels. Initially all the nodes are white. Support these query operations efficiently:

  1. Given some node, if it is white mark it black, if it is already black repeat this operation on all its children.
  2. Given some node, mark all subtrees of this node white.
  3. 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 1-1, update the node values using HLD by adding 11 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 vv is easy to add, simply reset all vv subtree values in the segment tree to -1, then perform query 3 on vv. If the answer is x0x \geq 0, subtract x+1x + 1 from vv to reset it. \blacksquare

This was a 3200 problem that I solved in under an hour!


© 2020-2025 • Last Updated 2025-10-13