Laserbeam
Three mirrors are arranged in the shape of an equilateral triangle, with their reflective surfaces pointing inwards. There is an infinitesimal gap at each vertex through which a laser beam may pass...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Three mirrors are arranged in the shape of an equilateral triangle, with their reflective surfaces pointing inwards. There is an infinitesimal gap at each vertex of the triangle through which a laser beam may pass.
Label the vertices \(A\), \(B\) and \(C\). There are \(2\) ways in which a laser beam may enter vertex \(C\), bounce off \(11\) surfaces, then exit through the same vertex: one way is shown below; the other is the reverse of that.

There are \(80840\) ways in which a laser beam may enter vertex \(C\), bounce off \(1000001\) surfaces, then exit through the same vertex.
In how many ways can a laser beam enter at vertex \(C\), bounce off \(12017639147\) surfaces, then exit through the same vertex?
Problem 202: Laserbeam
Mathematical Development
Theorem (Unfolding Principle). A laser beam making reflections inside an equilateral triangle corresponds, via the standard unfolding technique, to a straight line in the triangular tessellation of the plane. The line starts at the origin and terminates at a lattice point in the oblique coordinate system, where with .
Proof. Each reflection off a mirror wall is equivalent to reflecting the triangle across that wall and continuing the beam in a straight line. After reflections, the beam has crossed triangle edges. The beam enters at vertex and must exit at a vertex, crossing edges total. In the oblique coordinate system where the three families of parallel lines in the tessellation are indexed by coordinates with , careful counting of crossings gives . For , we get .
Theorem (Exit Vertex Classification). The beam exits through vertex if and only if and , which (given with ) is equivalent to .
Proof. In the triangular tessellation, a lattice point corresponds to a copy of vertex , , or depending on the residues of and modulo 3. Since and we require the endpoint to be a copy of , the condition reduces to .
Theorem (No Intermediate Vertex Condition). The beam passes through no intermediate vertex, and thus does not exit prematurely, if and only if , equivalently .
Proof. If , then is an intermediate lattice point on the line segment from to , so the beam would reach a vertex earlier. Conversely, if , no intermediate lattice point lies on the segment. Since , we have .
Theorem (Counting via Mobius Inversion). The number of valid beam paths is
where and is the Mobius function.
Proof. We seek
By Mobius inversion, . Exchanging the order of summation gives
For each squarefree divisor of with , writing gives with , so the inner count is a simple arithmetic progression count.
Lemma (Factorization). Since , all squarefree divisors built from the distinct prime factors may contribute.
Proof. Verified by trial division. Since , the Mobius function vanishes on divisors containing , so it is enough to enumerate squarefree divisors built from .
Editorial
The geometric part is handled by unfolding the reflections. Instead of following a bouncing beam inside one triangle, we follow a straight segment in the triangular tiling. For the given reflection count, every admissible beam corresponds to an endpoint on the line , where .
Two arithmetic conditions remain. The endpoint must be a copy of vertex , which becomes the congruence , and the segment must not hit an earlier lattice vertex, which becomes . That leaves a clean number-theoretic count: integers in one residue class modulo 3 that are also coprime to . Mobius inversion removes the coprimality condition by inclusion-exclusion over the squarefree divisors of .
Pseudocode
Set R = 12017639147 and n = (R + 3) / 2.
Factor n and keep its distinct prime divisors p_1, ..., p_k.
answer = 0
For each bitmask from 0 to 2^k - 1:
Let d be the product of the primes selected by the bitmask.
Let mu be +1 if the number of selected primes is even, else -1.
We need multiples b = d * t with 1 <= t <= n / d - 1
and d * t congruent to 2 modulo 3.
Solve the congruence for t modulo 3.
Count how many t in the interval satisfy that residue class.
Add mu times that count to answer.
Return answer
Complexity Analysis
- Time: for factorization, plus for the Mobius sum where is the number of distinct prime factors.
- Space: to store the distinct prime factors.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
int main() {
long long R = 12017639147LL;
long long n = (R + 3) / 2;
vector<long long> primes;
long long tmp = n;
for (long long p = 2; p * p <= tmp; p++) {
if (tmp % p == 0) {
primes.push_back(p);
while (tmp % p == 0) {
tmp /= p;
}
}
}
if (tmp > 1) {
primes.push_back(tmp);
}
long long answer = 0;
for (int mask = 0; mask < (1 << static_cast<int>(primes.size())); mask++) {
long long d = 1;
int bits = 0;
for (int i = 0; i < static_cast<int>(primes.size()); i++) {
if (mask & (1 << i)) {
d *= primes[i];
bits++;
}
}
long long mu = (bits % 2 == 0) ? 1 : -1;
long long nd = n / d;
long long count = 0;
if (d % 3 != 0) {
long long inverseMod3 = (d % 3 == 1) ? 1 : 2;
long long residue = (2 * inverseMod3) % 3;
long long upper = nd - 1;
if (residue == 0) {
if (upper >= 3) {
count = (upper - 3) / 3 + 1;
}
} else {
if (upper >= residue) {
count = (upper - residue) / 3 + 1;
}
}
}
answer += mu * count;
}
cout << answer << '\n';
return 0;
}
"""
Problem 202: Laserbeam
A laser beam enters vertex C of an equilateral triangle with mirrored sides,
reflects exactly 12017639147 times off the internal surfaces, and exits
through vertex C. Count the number of distinct beam paths.
Approach:
- Unfold reflections into a triangular tessellation.
- n = (R + 3) / 2 = 6008819575 is the lattice parameter.
- Count b in [1, n-1] with b congruent to 2 modulo 3 and gcd(b, n) = 1.
- Use Mobius inversion over squarefree divisors of n.
n = 6008819575 = 5^2 * 11 * 17 * 23 * 29 * 41 * 47
"""
def solve():
r = 12017639147
n = (r + 3) // 2
primes = []
tmp = n
p = 2
while p * p <= tmp:
if tmp % p == 0:
primes.append(p)
while tmp % p == 0:
tmp //= p
p += 1
if tmp > 1:
primes.append(tmp)
answer = 0
for mask in range(1 << len(primes)):
d = 1
bits = 0
for i, prime in enumerate(primes):
if mask & (1 << i):
d *= prime
bits += 1
mu = 1 if bits % 2 == 0 else -1
nd = n // d
if d % 3 == 0:
count = 0
else:
inverse_mod_3 = 1 if d % 3 == 1 else 2
residue = (2 * inverse_mod_3) % 3
upper = nd - 1
if residue == 0:
count = (upper - 3) // 3 + 1 if upper >= 3 else 0
else:
count = (upper - residue) // 3 + 1 if upper >= residue else 0
answer += mu * count
print(answer)
if __name__ == "__main__":
solve()