1. 程式人生 > >[Bzoj 2956] 模積和 (整除分塊)

[Bzoj 2956] 模積和 (整除分塊)

scanf 方法 using 展開 sin 乘法 algorithm queue 分塊

整除分塊

 一般形式:\(\sum_{i = 1}^n \lfloor \frac{n}{i} \rfloor * f(i)\)
 需要一種高效求得函數 \(f(i)\) 的前綴和的方法,比如等差等比數列求和或對於積性函數的篩法等,然後就可以用整除分塊的思想做。
 

題目解法

 化公式變成比較方便的形式:
  \(\ \sum_{i = 1}^n \sum_{j = 1}^m (n \mod i)(m \mod j), i \ne j\)
 \(= \sum_{i = 1}^n \sum_{j = 1}^m (n - i \lfloor \frac{n}{i} \rfloor)(m - j \lfloor \frac{m}{j} \rfloor) - \sum_{i = 1}^{min(n, m)} (n - i \lfloor \frac{n}{i} \rfloor)(m - i \lfloor \frac{m}{i} \rfloor)\)


 
 乘法分配律展開,化簡,令 \(t = min(n, m)\) 得:
  \(\ \sum_{i = 1}^n \sum_{j = 1}^m (n - i \lfloor \frac{n}{i} \rfloor)(m - j \lfloor \frac{m}{j} \rfloor) - \sum_{i = 1}^t (n - i \lfloor \frac{n}{i} \rfloor)(m - i \lfloor \frac{m}{i} \rfloor)\)
 \(= \sum_{i = 1}^n \sum_{i = 1}^m nm + m\sum_{i = 1}^n \sum_{i = 1}^m j \lfloor \frac{n}{i} \rfloor + n\sum_{i = 1}^n \sum_{i = 1}^m \lfloor \frac{m}{j} \rfloor +\)

 \(\sum_{i = 1}^n \sum_{i = 1}^m ij \lfloor \frac{n}{i} \rfloor \lfloor \frac{m}{j} \rfloor - \sum_{i = 1}^t nm + m \sum_{i = 1}^t i \lfloor \frac{n}{i} \rfloor - n \sum_{i = 1}^t i \lfloor \frac{m}{i} \rfloor - \sum_{i = 1}^t i^2 \lfloor \frac{n}{i} \rfloor\)
 \(\sum_{i = 1}^n \sum_{i = 1}^m ij \lfloor \frac{n}{i} \rfloor \lfloor \frac{m}{j} \rfloor\)
等於 \(\sum_{i = 1}^n i \lfloor \frac{n}{i} \rfloor * \sum_{i = 1}^m j \lfloor \frac{m}{j} \rfloor\),一個一個算即可。
 
 代碼寫的比較長……因為用 \(unsigned ll\) 為了避免出負數也多了很多取模……

#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef unsigned long long u64;

const u64 mod = 19940417;
const u64 inv_6 = 3323403;

inline u64 Calc_1(u64 l, u64 r) { return (l + r) * (r - l + 1) / 2 % mod; }
inline u64 Calc_2(u64 x) { return ((x + 1) * (2 * x + 1) % mod) * (x * inv_6 % mod) % mod; }

int main(int argc, const char *argv[])
{
  u64 n = 0, m = 0, t = 0, ans = 0, sum_1 = 0, sum_2 = 0;
  scanf("%llu%llu", &n, &m);
  ans = ((n * m % mod) * (n * m % mod) % mod);
  t = min(n, m), ans = (ans + mod - (n * m % mod) * t % mod) % mod;
  for(u64 tmp, l = 1, r = 1; l <= n; l = r + 1) {
    tmp = n / l, r = n / tmp;
    ans = (ans + mod - (Calc_1(l, r) * tmp % mod) * (m * m % mod) % mod) % mod;
    sum_1 = (sum_1 + Calc_1(l, r) * tmp % mod) % mod;
  }
  for(u64 tmp, l = 1, r = 1; l <= m; l = r + 1) {
    tmp = m / l, r = m / tmp;
    ans = (ans + mod - (Calc_1(l, r) * tmp % mod) * (n * n % mod) % mod) % mod;
    sum_2 = (sum_2 + Calc_1(l, r) * tmp % mod) % mod;
  }
  for(u64 tmp, l = 1, r = 1; l <= t; l = r + 1) {
    tmp = n / l, r = min(t, n / tmp);
    ans = (ans + Calc_1(l, r) * (tmp * m % mod)) % mod;
  }
  for(u64 tmp, l = 1, r = 1; l <= t; l = r + 1) {
    tmp = m / l, r = min(t, m / tmp);
    ans = (ans + Calc_1(l, r) * (tmp * n % mod)) % mod;
  }
  for(u64 l = 1, r = 1; l <= t; l = r + 1) {
    r = min(t, min(n / (n / l), m / (m / l)));
    ans = (ans + mod - (mod + Calc_2(r) - Calc_2(l - 1)) * ((n / l) * (m / l) % mod) % mod) % mod;
  }
  printf("%llu\n", (ans + sum_1 * sum_2) % mod);

  return 0;
}

[Bzoj 2956] 模積和 (整除分塊)