小 Q 与函数求和 1

∑ i = 1 n ∑ j = 1 n ϕ ( i j gcd ⁡ ( i , j ) K ) ∑ i = 1 n ∑ j = 1 n gcd ⁡ ( i , j ) K ϕ ( i j ) ∑ i = 1 n ∑ j = 1 n gcd ⁡ ( i , j ) K ϕ ( i ) ϕ ( j ) gcd ⁡ ( i , j ) ϕ ( gcd ⁡ ( i , j ) ) ∑ i = 1 n ∑ j = 1 n gcd ⁡ ( i , j ) K + 1 ϕ ( i ) ϕ ( j ) ϕ ( gcd ⁡ ( i , j ) ) ∑ d = 1 n d K + 1 i n v ( ϕ ( d ) ) ∑ i = 1 n d ∑ j = 1 n d ϕ ( i d ) ϕ ( j d ) [ gcd ⁡ ( i , j ) = 1 ] ∑ d = 1 n d K + 1 i n v ( p h i ( d ) ) ∑ k = 1 n d ϕ ( k ) ∑ i = 1 n k d ϕ ( i k d ) ∑ j = 1 n k d ϕ ( j k d ) T = k d , 设 f ( T ) = ∑ i = 1 n T ϕ ( i t ) ∑ T = 1 n f ( T ) 2 ∑ d ∣ T d K + 1 i n v ( ϕ ( d ) ) μ ( T d ) 设 g ( n ) = ∑ i ∣ n i K + 1 i n v ( ϕ ( i ) ) μ ( n i ) ∑ i = 1 n f ( i ) 2 g ( i ) 即 为 答 案 \sum_{i = 1} ^{n} \sum_{j = 1} ^{n} \phi(ij \gcd(i, j) ^ K)\\ \sum_{i = 1} ^{n} \sum_{j = 1} ^{n} \gcd(i, j) ^ K \phi(ij)\\ \sum_{i = 1} ^{n} \sum_{j = 1} ^{n} \gcd(i, j) ^ K \frac{\phi(i) \phi(j) \gcd(i, j)}{\phi(\gcd(i, j))}\\ \sum_{i = 1} ^{n} \sum_{j = 1} ^{n} \gcd(i, j) ^ {K + 1} \frac{\phi(i) \phi(j)}{ \phi(\gcd(i, j))}\\ \sum_{d = 1} ^{n} d ^{K + 1} inv(\phi(d)) \sum_{i = 1} ^{\frac{n}{d}} \sum_{j = 1} ^{\frac{n}{d}} \phi(id) \phi(jd)[\gcd(i, j) = 1]\\ \sum_{d = 1} ^{n} d ^{K + 1} inv(phi(d)) \sum_{k = 1} ^{\frac{n}{d}} \phi(k) \sum_{i = 1} ^{\frac{n}{kd}} \phi(ikd) \sum_{j = 1} ^{\frac{n}{kd}} \phi(jkd)\\ T = kd, 设f(T) = \sum_{i = 1} ^{\frac{n}{T}} \phi(it)\\ \sum_{T = 1} ^{n} f(T) ^ 2 \sum_{d \mid T} d ^{K + 1} inv(\phi(d)) \mu(\frac{T}{d})\\ 设g(n) = \sum_{i \mid n} i ^{K + 1}inv (\phi(i)) \mu(\frac{n}{i})\\ \sum_{i = 1} ^{n} f(i) ^ 2 g(i)即为答案 i=1nj=1nϕ(ijgcd(i,j)K)i=1nj=1ngcd(i,j)Kϕ(ij)i=1nj=1ngcd(i,j)Kϕ(gcd(i,j))ϕ(i)ϕ(j)gcd(i,j)i=1nj=1ngcd(i,j)K+1ϕ(gcd(i,j))ϕ(i)ϕ(j)d=1ndK+1inv(ϕ(d))i=1dnj=1dnϕ(id)ϕ(jd)[gcd(i,j)=1]d=1ndK+1inv(phi(d))k=1dnϕ(k)i=1kdnϕ(ikd)j=1kdnϕ(jkd)T=kdf(T)=i=1Tnϕ(it)T=1nf(T)2dTdK+1inv(ϕ(d))μ(dT)g(n)=iniK+1inv(ϕ(i))μ(in)i=1nf(i)2g(i)
所以预先处理 i K + 1 i ^{K + 1} iK+1次幂,及 i n v ( ϕ ( i ) ) inv(\phi(i)) inv(ϕ(i)),即可 ( n log ⁡ n ) (n \log n) (nlogn)同时算得 f ( n ) f(n) f(n),以及 g ( n ) g(n) g(n),整体复杂度 O ( n log ⁡ n ) O(n \log n) O(nlogn),稍卡常,得写 add sub 函数才能过。

#include <bits/stdc++.h>

using namespace std;

const int N = 5e6 + 10, mod = 998244353;

int prime[N], inv[N], phi[N], mu[N], f[N], g[N], a[N], n, k, cnt;

bool st[N];

inline int add(int x, int y) {
  return x + y < mod ? x + y : x + y - mod;
}

inline void Add(int &x, int y) {
  x + y < mod ? x += y : x += y - mod;
}

inline int sub(int x, int y) {
  return x >= y ? x - y : x - y + mod;
}

inline void Sub(int &x, int y) {
  x >= y ? x -= y : x += mod - y;
}

int quick_pow(int a, int n) {
  int ans = 1;
  while (n) {
    if (n & 1) {
      ans = 1ll * ans * a % mod;
    }
    a = 1ll * a * a % mod;
    n >>= 1;
  }
  return ans;
}

void init() {
  phi[1] = mu[1] = a[1] = inv[1] = 1;
  for (int i = 2; i < N; i++) {
    inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod;
    if (!st[i]) {
      prime[++cnt] = i;
      phi[i] = i - 1;
      mu[i] = -1;
      a[i] = quick_pow(i, k);
    }
    for (int j = 1; j <= cnt && 1ll * i * prime[j] < N; j++) {
      st[i * prime[j]] = 1;
      a[i * prime[j]] = 1ll * a[i] * a[prime[j]] % mod;
      if (i % prime[j] == 0) {
        phi[i * prime[j]] = phi[i] * prime[j];
        break;
      }
      phi[i * prime[j]] = phi[i] * (prime[j] - 1);
      mu[i * prime[j]] = -mu[i];
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j += i) {
      Add(f[i], phi[j]);
      if (mu[j / i] == 1) {
        Add(g[j], 1ll * a[i] * inv[phi[i]] % mod);
      }
      else if (mu[j / i] == -1) {
        Sub(g[j], 1ll * a[i] * inv[phi[i]] % mod);
      }
    }
  }
}

int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  scanf("%d %d", &n, &k);
  k++;
  init();
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    Add(ans, 1ll * f[i] * f[i] % mod * g[i] % mod);
  }
  printf("%d\n", ans);
  return 0;
}