|
| 1 | +#ifndef CP_ALGO_NUMBER_THEORY_DIRICHLET_HPP |
| 2 | +#define CP_ALGO_NUMBER_THEORY_DIRICHLET_HPP |
| 3 | +#include <numeric> |
| 4 | +#include <cstdint> |
| 5 | +#include <vector> |
| 6 | +#include <cmath> |
| 7 | +namespace cp_algo::math { |
| 8 | + auto floor_stats(int64_t n) { |
| 9 | + auto rt_n = int(sqrtl(n)); |
| 10 | + return std::pair{rt_n, 2 * rt_n - (n / rt_n == rt_n)}; |
| 11 | + } |
| 12 | + |
| 13 | + struct interval { |
| 14 | + int lo, hi; |
| 15 | + }; |
| 16 | + |
| 17 | + void exec_on_blocks(int64_t n, auto &&callback) { |
| 18 | + auto [rt_n, num_floors] = floor_stats(n); |
| 19 | + |
| 20 | + auto to_ord = [&](int64_t k) { |
| 21 | + return k <= rt_n ? int(k) : num_floors - int(n / k) + 1; |
| 22 | + }; |
| 23 | + |
| 24 | + callback({1, 1}, {1, 1}, {1, num_floors}); |
| 25 | + |
| 26 | + for (int k = 2; k <= num_floors; ++k) { |
| 27 | + if(k > rt_n) { |
| 28 | + int z = num_floors - k + 1; |
| 29 | + for (int x = 2; ; x++) { |
| 30 | + int y_lo_ord = std::max(x, z) + 1; |
| 31 | + int y_hi_ord = to_ord(n / (x * z)); |
| 32 | + if (y_hi_ord < y_lo_ord) break; |
| 33 | + callback({x, x}, {y_lo_ord, y_hi_ord}, {k, k}); |
| 34 | + callback({y_lo_ord, y_hi_ord}, {x, x}, {k, k}); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + callback({1, 1}, {k, k}, {k, num_floors}); |
| 39 | + callback({k, k}, {1, 1}, {k, num_floors}); |
| 40 | + |
| 41 | + if(k <= rt_n) { |
| 42 | + int x = k; |
| 43 | + for (int y = 2; y < k; ++y) { |
| 44 | + int z_lo_ord = to_ord(1LL * x * y); |
| 45 | + int z_hi_ord = to_ord(n / x); |
| 46 | + if (z_hi_ord < z_lo_ord) break; |
| 47 | + callback({x, x}, {y, y}, {z_lo_ord, z_hi_ord}); |
| 48 | + callback({y, y}, {x, x}, {z_lo_ord, z_hi_ord}); |
| 49 | + } |
| 50 | + int z_lo_ord = to_ord(1LL * x * x); |
| 51 | + callback({x, x}, {x, x}, {z_lo_ord, num_floors}); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + auto Dirichlet_mul(auto &&F, auto &&G, int64_t n) { |
| 57 | + auto m = size(F); |
| 58 | + std::decay_t<decltype(F)> H(m+1); |
| 59 | + exec_on_blocks(n, [&](interval x, interval y, interval z) { |
| 60 | + auto sum_x = F[x.hi] - F[x.lo - 1]; |
| 61 | + auto sum_y = G[y.hi] - G[y.lo - 1]; |
| 62 | + auto t = sum_x * sum_y; |
| 63 | + H[z.lo] += t; |
| 64 | + H[z.hi + 1] -= t; |
| 65 | + }); |
| 66 | + std::partial_sum(begin(H), end(H), begin(H)); |
| 67 | + H.pop_back(); |
| 68 | + return H; |
| 69 | + } |
| 70 | + |
| 71 | + auto Dirichlet_div(auto &&H, auto &&G, int64_t n) { |
| 72 | + H.push_back(0); |
| 73 | + std::adjacent_difference(begin(H), end(H), begin(H)); |
| 74 | + auto m = size(G); |
| 75 | + std::decay_t<decltype(G)> F(m); |
| 76 | + std::vector<bool> assigned(m); |
| 77 | + exec_on_blocks(n, [&](interval x, interval y, interval z) { |
| 78 | + auto sum_y = G[y.hi] - G[y.lo - 1]; |
| 79 | + if (!assigned[x.hi]) { |
| 80 | + F[x.hi] = F[x.lo - 1] + H[z.lo] / sum_y; |
| 81 | + assigned[x.hi] = true; |
| 82 | + } |
| 83 | + auto sum_x = F[x.hi] - F[x.lo - 1]; |
| 84 | + auto t = sum_y * sum_x; |
| 85 | + H[z.lo] -= t; |
| 86 | + H[z.hi + 1] += t; |
| 87 | + }); |
| 88 | + return F; |
| 89 | + } |
| 90 | + |
| 91 | +} |
| 92 | +#endif // CP_ALGO_NUMBER_THEORY_DIRICHLET_HPP |
0 commit comments