Skip to content

Commit 06f33b1

Browse files
committed
add lattice_counting
1 parent e3335c0 commit 06f33b1

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

mathematics/lattice_counting.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using uint64 = unsigned long long;
2+
using uint128 = __uint128_t;
3+
4+
// \sum_{i=0}^{n-1} \lfloor \frac{a+bi}{m} \rfloor
5+
uint128 lattice_count(uint64 n, uint64 a, uint64 b, uint64 m) {
6+
if (!b) return (uint128)n * (a / m);
7+
if (a >= m) return (uint128)n * (a / m) + lattice_count(n, a % m, b, m);
8+
if (b >= m) return uint128(n - 1) * n / 2 * (b / m) + lattice_count(n, a, b % m, m);
9+
return lattice_count((a + b * n) / m, (a + b * n) % m, m, b);
10+
}
11+
12+
// \sum_{i=0}^{n-1} i^x (\lfloor \frac{a+bi}{m} \rfloor)^y
13+
uint128 lattice_count(uint64 n, uint64 a, uint64 b, uint64 m, uint64 x, uint64 y) {
14+
if (x == 0 && y == 1) return lattice_count(n, a, b, m);
15+
}

0 commit comments

Comments
 (0)