huntian oy HDU - 6706 (+ Du teaching number theory sieve)

huntian oy (HDU - 6706)

Meaning of the questions:

For \ (n-, A, B \) , seeking \ (f (n, a, b) = \ sum_ {i = 1} ^ n \ sum_ {j = 1} ^ igcd (i ^ aj ^ a, i ^ B ^ BJ) [GCD (I, J) =. 1] \% (+ 10 ^. 9. 7) \) . Ensure \ (GCD (A, B) =. 1 \) . \ (n-<= 1E9 \) .

answer:

  • \(gcd(i^a-j^a,i^b-j^b) = i^{gcd(a,b)}-j^{gcd(a,b)}\)
  • \ (\ Sum_ {i = 1} ^ ni [gcd (i, n) = 1] = \ frac {n \ phi (n) + [n = 1]} {2} \)

The above two formulas.

\[f(n,a,b)=\sum_{i=1}^n\sum_{j=1}^igcd(i^a-j^a,i^b-j^b)[gcd(i,j)=1]\]

\[=\sum_{i=1}^n\sum_{j=1}^i(i-j)[gcd(i,j)=1]\]

\[=\sum_{i=1}^n(i*\phi(i)-\sum_{j=1}^ij[gcd(i,j)=1])\]

\ [= \ Sum_ {i = 1} ^ n (i * \ phi (i) - \ frac {i * \ phi (i)} {2}) - \ frac {1} {2} (here, to subtract \ frac {1} {2}, is a special case of considering i = 1) \]

\[=\frac{1}{2}(\sum_{i=1}^{n}(i*\phi(i))-1)\]

For \ (\ sum_ {i = 1} ^ {i} the n-* \ Phi (i) \) , can be used to teach Du Du teach sieve sieve seek :( study recommended this blog )

For \ (F (n-) = I * \ Phi (I) \) , configured \ (G (n-) = n-\) . The convolution disposed Deeley Cray \ (F * G = H \) , then

\[h(n)=\sum_{d|n}f(d)*g(\frac{n}{d})\]

\[=\sum_{d|n}(d*\phi(d))*\frac{n}{d}\]

\[=\sum_{d|n}n*\phi(d)\]

\ [= N \ sum_ {d | n} \ phi (d) = n ^ 2 \]

Therefore, in accordance with teachings Du sieve set \ (S (n-) = \ sum_. 1 = {I}} ^ {n-I * \ Phi (I) \) , then

\[S(n)=\sum_{i=1}^{n}i^2-\sum_{d=2}^{n}d*S(n)\]

On the block number of the recursive computation + \ (S (n) \) like.

Code:

#include <bits/stdc++.h>
#define fopi freopen("in.txt", "r", stdin)
#define fopo freopen("out.txt", "w", stdout)
using namespace std;
typedef long long LL;
const int MOD = 1e9+7;
const LL inv2 = 5e8+4;
const LL inv6 = 166666668;
typedef long long LL;

const int maxn = 1e6 + 10;
int check[maxn], phi[maxn], prime[maxn];
LL sum[maxn];
unordered_map<int, LL> d;

void init(int N) {
    memset(check, false, sizeof(check));
    phi[1] = 1;
    int tot = 0;
    for (int i = 2; i <= N; i++) {
        if (!check[i]) {
            prime[tot++] = i;
            phi[i] = i-1;
        }
        for (int j = 0; j < tot; j++) {
            if (i * prime[j] > N) break;
            check[i * prime[j]] = true;
            if (i % prime[j] == 0) {
                phi[i * prime[j]] = phi[i] * prime[j];
                break;
            }
            else {
                phi[i * prime[j]] = phi[i] * (prime[j]-1);
            }
        }
    }
    for (int i = 1; i <= N; i++)
        sum[i] = (sum[i-1] + 1ll*i*phi[i] % MOD) % MOD;
}

LL solve(int x) {
    return 1ll * x * (x+1) % MOD * (2*x+1) % MOD * inv6 % MOD;
}

int dfs(int x) {
    if (x <= 1000000) return sum[x];
    if (d[x]) return d[x];
    LL ans = solve(x);
    for (int l = 2, r; l <= x; l = r+1) {
        r = x/(x/l);
        ans = (ans - (1LL*(r-l+1)*(l+r)/2) % MOD * dfs(x/l) % MOD + MOD) % MOD;
    }
    return d[x] = ans;
}

int T;
int n, a, b;
int main() {
    init(1000000);
    scanf("%d", &T);
    for (int ca = 1; ca <= T; ca++) {
        scanf("%d%d%d", &n, &a, &b);
        printf("%lld\n", inv2 * (dfs(n) - 1 + MOD) % MOD);
    }
}

Guess you like

Origin www.cnblogs.com/ruthank/p/11469173.html