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 Foundation
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 , the number of edge crossings for a line from to is . Careful accounting (the beam enters through a gap, so the first and last crossings are at vertices, not interior edges) yields . 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. Specifically, the point is a copy of when … More precisely, the vertex type cycles with period 3 along each axis. Since and we require the endpoint to be a copy of (not or ), the condition reduces to (equivalently ). One can verify this directly for small cases.
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 the lattice point lies on the line segment from to and corresponds to a vertex of the tessellation. The beam would exit through that vertex before reaching . 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:
For each squarefree divisor of with , substituting gives with , i.e., , yielding where .
Lemma (Factorization). Since , all squarefree divisors contribute.
Proof. Verified by trial division. Since , the Mobius function for any divisible by , but we enumerate over squarefree divisors constructed from .
Editorial
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: n = 6008819575 = 5^2 * 11 * 17 * 23 * 29 * 41 * 47. We factor n: primes = [5, 11, 17, 23, 29, 41, 47]. We then iterate over each subset T of primes. Finally, return count.
Pseudocode
Input: R = 12017639147
Output: number of valid laser paths
n = (R + 3) / 2 = 6008819575
Factor n: primes = [5, 11, 17, 23, 29, 41, 47]
count = 0
For each subset T of primes:
Return count
Complexity Analysis
- Time: for factorization, plus for the Mobius sum where is the number of distinct prime factors. The Mobius summation is , dominated by factorization at .
- Space: to store the 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(){
// PE 202: Laser beam in equilateral triangle, R = 12017639147 reflections.
// n = (R + 3) / 2 = 6008819575.
// Count b in [1, n-1] with b ≡ 2 mod 3 and gcd(b, n) = 1.
// Use Mobius inversion over squarefree divisors of n.
long long R = 12017639147LL;
long long n = (R + 3) / 2; // 6008819575
// Factor n
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);
}
int np = primes.size();
long long ans = 0;
for(int mask = 0; mask < (1 << np); mask++){
long long d = 1;
int bits = __builtin_popcount(mask);
long long mu = (bits % 2 == 0) ? 1 : -1;
for(int i = 0; i < np; i++){
if(mask & (1 << i)) d *= primes[i];
}
if(n % d != 0) continue; // d must divide n
long long nd = n / d;
long long d_mod3 = d % 3;
long long cnt = 0;
if(d_mod3 == 0){
// 3 | d => d*k ≡ 0 mod 3 ≠ 2. No valid k.
cnt = 0;
} else {
long long dinv3 = (d_mod3 == 1) ? 1 : 2;
long long r = (2 * dinv3) % 3;
long long upper = nd - 1;
if(r == 0){
if(upper >= 3)
cnt = (upper - 3) / 3 + 1;
} else {
if(upper >= r)
cnt = (upper - r) / 3 + 1;
}
}
ans += mu * cnt;
}
cout << ans << endl;
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 ≡ 2 (mod 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 # 6008819575
# Factor n (extract distinct primes)
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)
np_ = len(primes)
ans = 0
# Inclusion-exclusion over squarefree divisors (Mobius function)
for mask in range(1 << np_):
d = 1
bits = bin(mask).count('1')
mu = 1 if bits % 2 == 0 else -1
for i in range(np_):
if mask & (1 << i):
d *= primes[i]
if n % d != 0:
continue
nd = n // d
d_mod3 = d % 3
if d_mod3 == 0:
cnt = 0 # 3|d means d*k ≡ 0 mod 3, never ≡ 2
else:
dinv3 = pow(d, -1, 3)
r = (2 * dinv3) % 3
upper = nd - 1
if r == 0:
cnt = max(0, (upper - 3) // 3 + 1) if upper >= 3 else 0
else:
cnt = max(0, (upper - r) // 3 + 1) if upper >= r else 0
ans += mu * cnt
print(ans)
if __name__ == "__main__":
solve()