爆标?
luogu_gza
·
·
题解
感谢来自可爱喵喵 @Glacia_official 的指导。
考虑分一位,两位,三位,大于三位的情况。
- 一位
参见其他题解,复杂度 O(1)。
- 两位
相当于求 \sum_{p=l}^{r}(n \bmod p)p+\lfloor \frac{n}{p} \rfloor=\sum_{p=l}^{r}(np-(p-1)\lfloor \frac np \rfloor)。
3. 三位
相当于求 $\sum_{p=l}^{r}(n \bmod p)p^2+(\lfloor \frac np \rfloor \bmod p)p+\lfloor \frac n{p^2} \rfloor=\sum_{p=l}^{r}(n-\lfloor \frac np \rfloor)p^2+(\lfloor \frac np \rfloor-\lfloor \frac n{p^2} \rfloor p)p+\lfloor \frac n{p^2} \rfloor$。
$\lfloor \frac np \rfloor p^2$ 也可以用 DIVCNT1 套类欧解决,$\lfloor \frac np \rfloor p$ 上文已经说过了。
$\lfloor \frac n{p^2} \rfloor$,$\lfloor \frac n{p^2} \rfloor p^2$ 都可以用普通的整除分块解决,因为 $\lfloor \frac n{p^2} \rfloor$ 只有 $O(n^{\frac 13})$ 段。
复杂度 $O(n^{\frac 13}\log n)$。
4. 大于三位
直接暴力,复杂度 $O(n^{\frac 13
}\log n)$。
---
总复杂度 $O(n^{\frac 13}\log n)$,代码也太难了,我让 AI 帮我写了一份。
```cpp
#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
using int128 = __int128_t;
constexpr int DEGREE = 4;
constexpr int64 MOD = 1'000'000'007;
constexpr int64 INV2 = 500'000'004;
constexpr int64 INV3 = 333'333'336;
constexpr int64 INV4 = 250'000'002;
constexpr int64 INV6 = 166'666'668;
constexpr int64 INV30 = 233'333'335;
constexpr int BINOM[DEGREE + 1][DEGREE + 1] = {
{1, 0, 0, 0, 0},
{1, 1, 0, 0, 0},
{1, 2, 1, 0, 0},
{1, 3, 3, 1, 0},
{1, 4, 6, 4, 1},
};
// Coefficients of sum_{i=1}^x i^k as a polynomial in x, for 0 <= k <= 3.
constexpr int64 POWER_SUM_COEFF[4][DEGREE + 1] = {
{0, 1, 0, 0, 0},
{0, INV2, INV2, 0, 0},
{0, INV6, INV2, INV3, 0},
{0, 0, INV4, INV2, INV4},
};
// Coefficients of sum_{i=0}^x i^k. Only k <= 3 is needed in reciprocity.
constexpr int64 ZERO_POWER_SUM_COEFF[4][DEGREE + 1] = {
{1, 1, 0, 0, 0},
{0, INV2, INV2, 0, 0},
{0, INV6, INV2, INV3, 0},
{0, 0, INV4, INV2, INV4},
};
struct MomentTable {
int64 value[DEGREE + 1][DEGREE + 1]{};
};
int64 normalize(int64 value) {
value %= MOD;
if (value < 0) value += MOD;
return value;
}
int64 mod_pow(int64 base, int exponent) {
base %= MOD;
int64 result = 1;
while (exponent-- > 0) result = result * base % MOD;
return result;
}
int64 power_sum(int exponent, int64 x) {
if (x <= 0) return 0;
int64 a = x % MOD;
int64 b = (x + 1) % MOD;
if (exponent == 0) return a;
if (exponent == 1) return a * b % MOD * INV2 % MOD;
if (exponent == 2) {
return a * b % MOD * ((2 * a + 1) % MOD) % MOD * INV6 % MOD;
}
if (exponent == 3) {
int64 half = a * b % MOD * INV2 % MOD;
return half * half % MOD;
}
int64 quadratic = (3 * a % MOD * a + 3 * a - 1) % MOD;
return normalize(a * b % MOD * ((2 * a + 1) % MOD) % MOD * quadratic % MOD * INV30);
}
int64 zero_based_power_sum(int64 count, int exponent) {
if (count <= 0) return 0;
if (exponent == 0) return count % MOD;
return power_sum(exponent, count - 1);
}
int64 range_power_sum(int exponent, int64 left, int64 right) {
return normalize(power_sum(exponent, right) - power_sum(exponent, left - 1));
}
// Returns M[p][q] = sum_{i=0}^{count-1} i^p floor((a*i+b)/m)^q
// for every p+q <= DEGREE. This is the generalized Euclidean algorithm.
MomentTable euclidean_moments(int64 count, int64 m, int64 a, int64 b) {
MomentTable result;
if (count == 0) return result;
if (count <= 6) {
for (int64 i = 0; i < count; ++i) {
int64 floor_value = static_cast<int64>(((int128)a * i + b) / m);
int64 i_power[DEGREE + 1]{1};
int64 floor_power[DEGREE + 1]{1};
for (int exponent = 1; exponent <= DEGREE; ++exponent) {
i_power[exponent] = i_power[exponent - 1] * (i % MOD) % MOD;
floor_power[exponent] = floor_power[exponent - 1]
* (floor_value % MOD) % MOD;
}
for (int p = 0; p <= DEGREE; ++p) {
for (int q = 0; p + q <= DEGREE; ++q) {
result.value[p][q] += i_power[p] * floor_power[q] % MOD;
if (result.value[p][q] >= MOD) result.value[p][q] -= MOD;
}
}
}
return result;
}
int64 quotient_a = a / m;
int64 quotient_b = b / m;
if (quotient_a != 0 || quotient_b != 0) {
MomentTable reduced = euclidean_moments(count, m, a % m, b % m);
int64 quotient_a_power[DEGREE + 1]{1};
int64 quotient_b_power[DEGREE + 1]{1};
for (int exponent = 1; exponent <= DEGREE; ++exponent) {
quotient_a_power[exponent] = quotient_a_power[exponent - 1] * (quotient_a % MOD) % MOD;
quotient_b_power[exponent] = quotient_b_power[exponent - 1] * (quotient_b % MOD) % MOD;
}
for (int p = 0; p <= DEGREE; ++p) {
for (int q = 0; p + q <= DEGREE; ++q) {
int64 current = 0;
for (int floor_power = 0; floor_power <= q; ++floor_power) {
int polynomial_power = q - floor_power;
for (int i_power = 0; i_power <= polynomial_power; ++i_power) {
int64 coefficient = BINOM[q][floor_power];
coefficient = coefficient * BINOM[polynomial_power][i_power] % MOD;
coefficient = coefficient * quotient_a_power[i_power] % MOD;
coefficient = coefficient * quotient_b_power[polynomial_power - i_power] % MOD;
current += coefficient * reduced.value[p + i_power][floor_power] % MOD;
if (current >= MOD) current -= MOD;
}
}
result.value[p][q] = current;
}
}
return result;
}
for (int p = 0; p <= DEGREE; ++p) {
result.value[p][0] = zero_based_power_sum(count, p);
}
if (a == 0) return result;
int64 maximum_floor = static_cast<int64>(((int128)a * (count - 1) + b) / m);
if (maximum_floor == 0) return result;
MomentTable reciprocal = euclidean_moments(maximum_floor, a, m, m - b - 1);
int64 maximum_floor_power[DEGREE + 1]{1};
for (int exponent = 1; exponent <= DEGREE; ++exponent) {
maximum_floor_power[exponent] = maximum_floor_power[exponent - 1]
* (maximum_floor % MOD) % MOD;
}
for (int p = 0; p < DEGREE; ++p) {
for (int q = 1; p + q <= DEGREE; ++q) {
int64 current = zero_based_power_sum(count, p) * maximum_floor_power[q] % MOD;
for (int y_power = 0; y_power < q; ++y_power) {
int64 delta_coefficient = BINOM[q][y_power];
for (int floor_power = 0; floor_power <= p + 1; ++floor_power) {
int64 coefficient = delta_coefficient * ZERO_POWER_SUM_COEFF[p][floor_power] % MOD;
current -= coefficient * reciprocal.value[y_power][floor_power] % MOD;
if (current < 0) current += MOD;
}
}
result.value[p][q] = current;
}
}
return result;
}
struct Vector {
int64 x;
int64 y;
};
Vector operator+(Vector lhs, Vector rhs) {
return {lhs.x + rhs.x, lhs.y + rhs.y};
}
void add_path_rows(
MomentTable& total,
Vector point,
Vector step,
int64 clipped_low,
int64 clipped_high
) {
// Let i start at zero on the first row crossed by this vector.
// floor(n / (point.y-1-i)) = point.x + floor((step.x*i+step.x-1)/step.y).
int64 index_begin = point.y - clipped_high - 1;
int64 index_end = point.y - clipped_low - 1;
MomentTable right = euclidean_moments(index_end + 1, step.y, step.x, step.x - 1);
MomentTable left;
if (index_begin != 0) {
left = euclidean_moments(index_begin, step.y, step.x, step.x - 1);
}
int64 row_origin = point.y - 1;
int64 row_origin_power[DEGREE + 1]{1};
int64 point_x_power[DEGREE + 1]{1};
for (int exponent = 1; exponent <= DEGREE; ++exponent) {
row_origin_power[exponent] = row_origin_power[exponent - 1] * (row_origin % MOD) % MOD;
point_x_power[exponent] = point_x_power[exponent - 1] * (point.x % MOD) % MOD;
}
constexpr int REQUIRED_MOMENTS[][2] = {
{3, 1}, {1, 1}, {0, 1}, {0, 2}, {0, 3},
};
for (auto [row_power, floor_power] : REQUIRED_MOMENTS) {
int64 contribution = 0;
for (int i_power = 0; i_power <= row_power; ++i_power) {
int64 row_coefficient = BINOM[row_power][i_power]
* row_origin_power[row_power - i_power] % MOD;
if (i_power & 1) row_coefficient = normalize(-row_coefficient);
for (int inner_power = 0; inner_power <= floor_power; ++inner_power) {
int64 coefficient = row_coefficient * BINOM[floor_power][inner_power] % MOD;
coefficient = coefficient * point_x_power[floor_power - inner_power] % MOD;
int64 moments = normalize(
right.value[i_power][inner_power] - left.value[i_power][inner_power]
);
contribution += coefficient * moments % MOD;
if (contribution >= MOD) contribution -= MOD;
}
}
total.value[row_power][floor_power] += contribution;
if (total.value[row_power][floor_power] >= MOD) {
total.value[row_power][floor_power] -= MOD;
}
}
}
int integer_square_root(int64 value) {
int result = static_cast<int>(sqrt(static_cast<long double>(value)));
while (1LL * (result + 1) * (result + 1) <= value) ++result;
while (1LL * result * result > value) --result;
return result;
}
int integer_cube_root(int64 value) {
int result = static_cast<int>(cbrt(static_cast<long double>(value)));
while (1LL * (result + 1) * (result + 1) * (result + 1) <= value) ++result;
while (1LL * result * result * result > value) --result;
return result;
}
void add_direct_row(MomentTable& table, int row, int quotient) {
int64 row_power[DEGREE + 1]{1};
int64 quotient_power[DEGREE + 1]{1};
for (int exponent = 1; exponent <= DEGREE; ++exponent) {
row_power[exponent] = row_power[exponent - 1] * row % MOD;
quotient_power[exponent] = quotient_power[exponent - 1] * quotient % MOD;
}
constexpr int REQUIRED_MOMENTS[][2] = {
{3, 1}, {1, 1}, {0, 1}, {0, 2}, {0, 3},
};
for (auto [a, b] : REQUIRED_MOMENTS) {
table.value[a][b] += row_power[a] * quotient_power[b] % MOD;
if (table.value[a][b] >= MOD) table.value[a][b] -= MOD;
}
}
MomentTable subtract_tables(const MomentTable& lhs, const MomentTable& rhs) {
MomentTable result;
constexpr int REQUIRED_MOMENTS[][2] = {
{3, 1}, {1, 1}, {0, 1}, {0, 2}, {0, 3},
};
for (auto [a, b] : REQUIRED_MOMENTS) {
result.value[a][b] = normalize(lhs.value[a][b] - rhs.value[a][b]);
}
return result;
}
// Answers all requested prefixes in one Stern-Brocot traversal.
map<int, MomentTable> hyperbola_moments(int n, vector<int> limits) {
sort(limits.begin(), limits.end());
limits.erase(unique(limits.begin(), limits.end()), limits.end());
map<int, MomentTable> result;
if (limits.empty()) return result;
int cube_root = integer_cube_root(n);
int square_root = integer_square_root(n);
MomentTable direct_prefix;
size_t direct_index = 0;
while (direct_index < limits.size() && limits[direct_index] <= 0) {
result[limits[direct_index++]] = direct_prefix;
}
for (int row = 1; row <= cube_root; ++row) {
add_direct_row(direct_prefix, row, n / row);
while (direct_index < limits.size() && limits[direct_index] == row) {
result[row] = direct_prefix;
++direct_index;
}
}
if (direct_index == limits.size()) return result;
vector<int> high_limits(limits.begin() + direct_index, limits.end());
sort(high_limits.rbegin(), high_limits.rend());
map<int, MomentTable> suffix;
size_t next_limit = 0;
MomentTable processed;
while (next_limit < high_limits.size() && high_limits[next_limit] == square_root) {
suffix[high_limits[next_limit++]] = processed;
}
Vector point{n / square_root, square_root + 1};
vector<Vector> stack{{1, 0}, {1, 1}};
auto outside = [n](Vector candidate) {
return (int128)candidate.x * candidate.y > n;
};
auto slope_is_steep_enough = [n](int64 x, Vector direction) {
return (int128)n * direction.x <= (int128)x * x * direction.y;
};
while (true) {
Vector lower = stack.back();
stack.pop_back();
while (outside(point + Vector{lower.x, -lower.y})) {
int64 row_low = point.y - lower.y;
int64 current_high = point.y - 1;
while (next_limit < high_limits.size()
&& high_limits[next_limit] >= row_low
&& high_limits[next_limit] <= current_high) {
int cut = high_limits[next_limit];
if (cut < current_high) {
add_path_rows(processed, point, lower, cut + 1, current_high);
}
suffix[cut] = processed;
current_high = cut;
++next_limit;
}
if (row_low <= current_high) {
add_path_rows(processed, point, lower, row_low, current_high);
}
point.x += lower.x;
point.y -= lower.y;
}
if (point.y <= cube_root) break;
Vector upper = stack.back();
while (!outside(point + Vector{upper.x, -upper.y})) {
lower = upper;
stack.pop_back();
upper = stack.back();
}
while (true) {
Vector middle = lower + upper;
if (outside(point + Vector{middle.x, -middle.y})) {
stack.push_back(middle);
upper = middle;
} else if (slope_is_steep_enough(point.x + middle.x, upper)) {
break;
} else {
lower = middle;
}
}
}
for (int row = 1; row < point.y; ++row) {
add_direct_row(processed, row, n / row);
}
for (int limit : high_limits) {
result[limit] = subtract_tables(processed, suffix[limit]);
}
return result;
}
int64 reverse_in_base(int value, int base) {
int64 result = 0;
while (value > 0) {
result = (result * base + value % base) % MOD;
value /= base;
}
return result;
}
// Computes sum_{p=sqrt(n)+1}^right p^power floor(n/p) by swapping
// the two coordinates of the lattice points under x*y <= n.
int64 high_base_floor_sum(
int n,
int square_root,
int right,
int power,
const MomentTable& maximum_moments,
const MomentTable& full_block_moments
) {
int maximum_quotient = n / (square_root + 1);
int full_block_quotient = n / right;
int64 whole_range = range_power_sum(power, square_root + 1, right);
int64 result = full_block_quotient * whole_range % MOD;
int64 boundary_sum = 0;
for (int exponent = 0; exponent <= power + 1; ++exponent) {
int64 difference = normalize(
maximum_moments.value[0][exponent] - full_block_moments.value[0][exponent]
);
boundary_sum += POWER_SUM_COEFF[power][exponent] * difference % MOD;
if (boundary_sum >= MOD) boundary_sum -= MOD;
}
int64 partial_blocks = maximum_quotient - full_block_quotient;
boundary_sum -= partial_blocks % MOD * power_sum(power, square_root) % MOD;
result += normalize(boundary_sum);
return result % MOD;
}
int64 solve(int n, int64 k) {
int cube_root = integer_cube_root(n);
int square_root = integer_square_root(n);
int64 answer = 0;
int direct_right = static_cast<int>(min<int64>(k, cube_root));
for (int base = 2; base <= direct_right; ++base) {
answer += reverse_in_base(n, base);
if (answer >= MOD) answer -= MOD;
}
int three_digit_right = static_cast<int>(min<int64>(k, square_root));
int two_digit_right = static_cast<int>(min<int64>(k, n));
vector<int> required_limits;
if (three_digit_right > cube_root) {
required_limits.push_back(cube_root);
required_limits.push_back(three_digit_right);
}
if (two_digit_right > square_root) {
required_limits.push_back(n / (square_root + 1));
required_limits.push_back(n / two_digit_right);
}
map<int, MomentTable> moments = hyperbola_moments(n, required_limits);
// Exactly three digits: n = a*p^2 + b*p + c.
// rev(n,p) = c*p^2 + b*p + a
// = n*p^2 - floor(n/p)*p^3 + floor(n/p)*p
// - floor(n/p^2)*p^2 + floor(n/p^2).
if (three_digit_right > cube_root) {
const MomentTable& right_moments = moments.at(three_digit_right);
const MomentTable& left_moments = moments.at(cube_root);
int64 contribution = n * range_power_sum(2, cube_root + 1, three_digit_right) % MOD;
contribution -= normalize(right_moments.value[3][1] - left_moments.value[3][1]);
contribution += normalize(right_moments.value[1][1] - left_moments.value[1][1]);
contribution = normalize(contribution);
int left = cube_root + 1;
while (left <= three_digit_right) {
int quotient = n / (left * left);
int block_right = min(three_digit_right, integer_square_root(n / quotient));
contribution -= quotient * range_power_sum(2, left, block_right) % MOD;
contribution += quotient * range_power_sum(0, left, block_right) % MOD;
contribution = normalize(contribution);
left = block_right + 1;
}
answer += contribution;
answer %= MOD;
}
// Exactly two digits: rev(n,p) = n*p - floor(n/p)*p^2 + floor(n/p).
if (two_digit_right > square_root) {
int maximum_quotient = n / (square_root + 1);
int full_block_quotient = n / two_digit_right;
const MomentTable& maximum_moments = moments.at(maximum_quotient);
const MomentTable& full_block_moments = moments.at(full_block_quotient);
int64 contribution = n * range_power_sum(1, square_root + 1, two_digit_right) % MOD;
contribution -= high_base_floor_sum(
n, square_root, two_digit_right, 2, maximum_moments, full_block_moments
);
contribution += high_base_floor_sum(
n, square_root, two_digit_right, 0, maximum_moments, full_block_moments
);
answer += normalize(contribution);
answer %= MOD;
}
if (k > n) {
answer += (k - n) % MOD * n % MOD;
answer %= MOD;
}
return normalize(answer);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int test_count;
cin >> test_count;
while (test_count--) {
int n;
int64 k;
cin >> n >> k;
cout << solve(n, k) << '\n';
}
return 0;
}
```