G. GCD Festival(莫比乌斯、欧拉函数)

G. GCD Festival

∑ i = 1 n ∑ j = 1 n gcd ⁡ ( a i , a j ) gcd ⁡ ( i , j ) ∑ d = 1 n d ∑ i = 1 n d ∑ j = 1 n d gcd ⁡ ( a i d , a j d ) [ gcd ⁡ ( i , j ) = 1 ] ∑ d = 1 n d ∑ k = 1 n d μ ( k ) ∑ i = 1 n k d ∑ j = 1 n k d gcd ⁡ ( a i k d , a j k d ) T = k d ∑ T = 1 n ∑ i = 1 n T ∑ j = 1 n T gcd ⁡ ( a i T , a j T ) ∑ d ∣ T d μ ( T d ) ∑ T = 1 n ϕ ( T ) ∑ i = 1 n T ∑ j = 1 n T gcd ⁡ ( a i T , a j T ) \sum_{i = 1} ^{n} \sum_{j = 1} ^{n} \gcd(a_i, a_j) \gcd(i, j)\\ \sum_{d = 1} ^{n} d \sum_{i = 1} ^{\frac{n}{d}} \sum_{j = 1} ^{\frac{n}{d}} \gcd(a_{id}, a_{jd})[\gcd(i, j) = 1]\\ \sum_{d = 1} ^{n} d \sum_{k = 1} ^{\frac{n}{d}} \mu(k) \sum_{i = 1} ^{\frac{n}{kd}} \sum_{j = 1} ^{\frac{n}{kd}} \gcd(a_{i kd}, a_{jkd})\\ T = kd\\ \sum_{T = 1} ^{n} \sum_{i = 1} ^{\frac{n}{T}} \sum_{j = 1} ^{\frac{n}{T}} \gcd(a_{iT}, a_{jT}) \sum_{d \mid T} d \mu(\frac{T}{d})\\ \sum_{T = 1} ^{n} \phi(T) \sum_{i = 1} ^{\frac{n}{T}} \sum_{j = 1} ^{\frac{n}{T}} \gcd(a_{iT}, a_{jT})\\ i=1nj=1ngcd(ai,aj)gcd(i,j)d=1ndi=1dnj=1dngcd(aid,ajd)[gcd(i,j)=1]d=1ndk=1dnμ(k)i=1kdnj=1kdngcd(aikd,ajkd)T=kdT=1ni=1Tnj=1Tngcd(aiT,ajT)dTdμ(dT)T=1nϕ(T)i=1Tnj=1Tngcd(aiT,ajT)
我们考虑设 f ( n , T ) = ∑ i = 1 n T ∑ j = 1 n T gcd ⁡ ( a i T , a j T ) f(n, T) = \sum\limits_{i = 1} ^{\frac{n}{T}} \sum\limits_{j = 1} ^{\frac{n}{T}} \gcd(a_{iT}, a_{jT}) f(n,T)=i=1Tnj=1Tngcd(aiT,ajT) g ( x ) g(x) g(x) i ∈ [ T , 2 T , … , n T T ] i \in [T, 2T, \dots, \frac{n}{T} T] i[T,2T,,TnT] x x x的出现次数。
f ( n , T ) = ∑ i = 1 m ∑ j = 1 m g ( i ) g ( j ) gcd ⁡ ( i , j ) , ( m = 1 0 5 ) ∑ d = 1 m d ∑ i = 1 m d ∑ j = 1 m d g ( i d ) g ( j d ) [ gcd ⁡ ( i , j ) = 1 ] ∑ d = 1 m d ∑ k = 1 n d μ ( k ) ( ∑ i = 1 m k d g ( i k d ) ) 2 T = k d ∑ T = 1 m ϕ ( T ) ( ∑ i = 1 m T g ( i T ) ) 2 f(n, T) = \sum_{i = 1} ^{m} \sum_{j = 1} ^{m} g(i) g(j) \gcd(i, j), (m = 10 ^ 5)\\ \sum_{d = 1} ^{m} d \sum_{i = 1} ^{\frac{m}{d}} \sum_{j = 1} ^{\frac{m}{d}} g(id) g(jd) [\gcd(i, j) = 1]\\ \sum_{d = 1} ^{m} d \sum_{k = 1} ^{\frac{n}{d}} \mu(k) \left( \sum_{i = 1} ^{\frac{m}{kd}} g(ikd) \right) ^ 2\\ T = kd\\ \sum_{T = 1} ^{m} \phi(T) \left( \sum_{i = 1} ^{\frac{m}{T}} g(iT) \right) ^ 2\\ f(n,T)=i=1mj=1mg(i)g(j)gcd(i,j),(m=105)d=1mdi=1dmj=1dmg(id)g(jd)[gcd(i,j)=1]d=1mdk=1dnμ(k)i=1kdmg(ikd)2T=kdT=1mϕ(T)i=1Tmg(iT)2
考虑重新定义 g ( n ) g(n) g(n)表示为是 n n n的倍数的数字有多少个,则上式可以直接写成:
∑ T = 1 m ϕ ( T ) g ( T ) 2 \sum_{T = 1} ^{m} \phi(T) g(T) ^ 2\\ T=1mϕ(T)g(T)2
由此我们可以在 O ( n log ⁡ 2 n ) O(n \log ^ 2n) O(nlog2n)的时间内完成这题。

#include 

using namespace std;

const int N = 1e5 + 10, mod = 1e9 + 7;

int prime[N], phi[N], a[N], n, cnt;

int sum[N], m;

bool st[N];

vector<int> fac[N];

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

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

void init() {
  phi[1] = 1;
  for (int i = 2; i < N; i++) {
    if (!st[i]) {
      prime[++cnt] = i;
      phi[i] = i - 1;
    }
    for (int j = 1; j <= cnt && 1ll * i * prime[j] < N; j++) {
      st[i * prime[j]] = 1;
      if (i % prime[j] == 0) {
        phi[i * prime[j]] = phi[i] * prime[j];
        break;
      }
      phi[i * prime[j]] = phi[i] * (prime[j] - 1);
    }
  }
  for (int i = 1; i < N; i++) {
    for (int j = i; j < N; j += i) {
      fac[j].push_back(i);
    }
  }
}

int f(int n, int T) {
  int ans = 0;
  for (int i = T; i <= n; i += T) {
    for (auto it : fac[a[i]]) {
      ans = sub(ans, 1ll * phi[it] * sum[it] % mod * sum[it] % mod);
      sum[it]++;
      ans = add(ans, 1ll * phi[it] * sum[it] % mod * sum[it] % mod);
    }
  }
  for (int i = T; i <= n; i += T) {
    for (auto it : fac[a[i]]) {
      sum[it]--;
    }
  }
  return ans;
}

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

你可能感兴趣的:(数论,算法,概率论,线性代数)