Project Euler 388
题目
Distinct Lines
Consider all lattice points with .
From the origin all
lines are drawn to the other lattice points.
Let be the number of
distinct such lines.
You are given that .
Find . Give as your
answer the first nine digits followed by the last nine digits.
解决方案
令 。
在这个三维空间中,一共有 个点,其坐标满足 。
在这些点中,如果一个点不被前面的点 "阻挡",当且仅当其坐标满足 。
那么有:
那么我们可以得到关于 的递推式,直接使用数论分块的方式进行求解即可:
代码
本代码使用了 GMP
库实现。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| # include <bits/stdc++.h> # include "gmpxx.h" using namespace std; typedef long long ll; const ll N=1e10; const int M=1000000; const int O = 9;
mpz_class d[M+2]; unordered_map<ll,mpz_class>mpD; mpz_class D(ll n) { if (n <= M && d[n] > 0) return d[n]; else if (n > M && mpD.count(n)) return mpD[n]; mpz_class w = mpz_class(n+1)*(n+1)*(n+1) - 1; for (ll l = 2, r; l <= n; l = r + 1) { r = n / (n / l); w -= D(n / l) * (r - l + 1); } if (n <= M) return d[n] = w; else return mpD[n] = w; } int main() { mpz_class ans2 = D(N); string s = ans2.get_str(); string ans = s.substr(0, O) + s.substr(max(0, int(s.size()) - O)); cout << ans << endl; }
|