zoukankan      html  css  js  c++  java
  • [SDOI 2015]约数个数和

    Description

     设d(x)为x的约数个数,给定N、M,求 $sum^N_{i=1}sum^M_{j=1}d(ij)$

    Input

    输入文件包含多组测试数据。

    第一行,一个整数T,表示测试数据的组数。
    接下来的T行,每行两个整数N、M。

    Output

     T行,每行一个整数,表示你所求的答案。

    Sample Input

    2
    7 4
    5 6

    Sample Output

    110
    121

    HINT

    1<=N, M<=50000

    1<=T<=50000

    题解

    先给出一个结论: $$ sigma_0(ij) = sum_{a | i} sum_{b | j} [gcd(a, b) = 1]$$

    证明(摘自Ken_He):

    我们令 $i = p_1^{a_1} p_2^{a_2} cdots$ , $j = p_1^{b_1} p_2^{b_2} cdots$ , $d | ij$ 且 $d = p_1^{c_1} p_2^{c_2} cdots$ , 则 $c_n le a_n + b_n$ 。

    考虑如何不重复地统计每一个 $d$ : 令 $c_n = A_n + B_n$ , 其中 $A_n$ 和 $B_n$ 分别为 $i$ 和 $j$ 对 $c_n$ 的贡献, 则我们要求 egin{cases}B_n = 0 & A_n < a_n \B_n ge 0 & A_n = a_nend{cases}

    这样一来, $c_n$ 的表示形式就变成唯一的了,因而不会被重复统计。我们再考虑如何统计这样的 $A_n$ 和 $B_n$ :我们令 $A_n' = a_n - A_n$ ,则约束条件变为egin{cases}B_n = 0 & A_n' e 0 \B_n ge 0 & A_n' = 0end{cases}

    等价于 $gcd(a, b) = 1$ 。

    因此得证。

    egin{aligned}ans&=sum_{i=1}^Nsum_{j=1}^Msum_{amid i}sum_{bmid j}[gcd(a,b)=1]\&=sum_{i=1}^Nsum_{j=1}^Msum_{amid i}sum_{bmid j}sum_{dmid gcd(a,b)}mu(d)\&=sum_{a=1}^Nsum_{b=1}^Msum_{i=1}^{leftlfloorfrac{N}{a} ight floor}sum_{j=1}^{leftlfloorfrac{M}{b} ight floor}sum_{dmid gcd(a,b)}mu(d)\&=sum_{d=1}^{min{N,M}}mu(d)sum_{a=1}^{leftlfloorfrac{N}{d} ight floor}sum_{b=1}^{leftlfloorfrac{M}{d} ight floor}leftlfloorfrac{N}{ad} ight floorleftlfloorfrac{M}{bd} ight floor\&=sum_{d=1}^{min{N,M}}mu(d)left(sum_{a=1}^{leftlfloorfrac{N}{d} ight floor}leftlfloorfrac{N}{ad} ight floor ight)left(sum_{b=1}^{leftlfloorfrac{M}{d} ight floor}leftlfloorfrac{M}{bd} ight floor ight)end{aligned}

    设 $t(x)=sum_{i=1}^x leftlfloorfrac{x}{i} ight floor$ ,

    显然 $$Rightarrow ans=sum_{d=1}^{min{N,M}}mu(d)cdot tleft(leftlfloorfrac{N}{d} ight floor ight)cdot tleft(leftlfloorfrac{M}{d} ight floor ight)$$

    显然我们用 $O(nsqrt n)$ 预处理出函数 $t$ ,再用 $O(Tsqrt n)$ 回答询问即可。

     1 //It is made by Awson on 2018.1.22
     2 #include <set>
     3 #include <map>
     4 #include <cmath>
     5 #include <ctime>
     6 #include <queue>
     7 #include <stack>
     8 #include <cstdio>
     9 #include <string>
    10 #include <vector>
    11 #include <cstdlib>
    12 #include <cstring>
    13 #include <iostream>
    14 #include <algorithm>
    15 #define LL long long
    16 #define Abs(a) ((a) < 0 ? (-(a)) : (a))
    17 #define Max(a, b) ((a) > (b) ? (a) : (b))
    18 #define Min(a, b) ((a) < (b) ? (a) : (b))
    19 #define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
    20 #define writeln(x) (write(x), putchar('
    '))
    21 #define lowbit(x) ((x)&(-(x)))
    22 using namespace std;
    23 const int N = 50000;
    24 void read(int &x) {
    25     char ch; bool flag = 0;
    26     for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
    27     for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
    28     x *= 1-2*flag;
    29 }
    30 void write(LL x) {
    31     if (x > 9) write(x/10);
    32     putchar(x%10+48);
    33 }
    34 
    35 int mu[N+5], t[N+5], n, m;
    36 
    37 void get_mu() {
    38     int prime[N+5], isprime[N+5], tot = 0;
    39     memset(isprime, 1, sizeof(isprime)); isprime[1] = 0, mu[1] = 1;
    40     for (int i = 2; i <= N; i++) {
    41     if (isprime[i]) prime[++tot] = i, mu[i] = -1;
    42     for (int j = 1; j <= tot && i*prime[j] <= N; j++) {
    43         isprime[i*prime[j]] = 0;
    44         if (i%prime[j]) mu[i*prime[j]] = -mu[i];
    45         else {mu[i*prime[j]] = 0; break; }
    46     }
    47     mu[i] += mu[i-1];
    48     }
    49 }
    50 int get_t(int x) {
    51     int ans = 0;
    52     for (int i = 1, last; i <= x; i = last+1) {
    53     last = x/(x/i); ans += (last-i+1)*(x/i);
    54     }
    55     return ans;
    56 }
    57 
    58 LL cal(int n, int m) {
    59     if (n > m) Swap(n, m); LL ans = 0;
    60     for (int i = 1, last; i <= n; i = last+1) {
    61     last = Min(n/(n/i), m/(m/i));
    62     ans += (LL)(mu[last]-mu[i-1])*t[n/i]*t[m/i];
    63     }
    64     return ans;
    65 }
    66 void work() {
    67     read(n), read(m); writeln(cal(n,m));
    68 }
    69 int main() {
    70     int T; read(T); get_mu();
    71     for (int i = 1; i <= N; i++) t[i] = get_t(i);
    72     while (T--) work();
    73     return 0;
    74 }
  • 相关阅读:
    VS工作目录,输出目录
    Google的C++开源代码项
    C++文件读写
    深拷贝浅拷贝
    Efficient Graph-Based Image Segmentation
    Graph Cut 简介
    Graph Cut
    "GrabCut" - Interactive Foreground Extraction using Iter
    EM算法
    Python图像处理库(2)
  • 原文地址:https://www.cnblogs.com/NaVi-Awson/p/8328396.html
Copyright © 2011-2022 走看看