[BZOJ 2154]Crash的数字表格

Description

今天的数学课上,Crash小朋友学习了最小公倍数(Least Common Multiple)。对于两个正整数a和b,LCM(a, b)表示能同时被a和b整除的最小正整数。例如,LCM(6, 8) = 24。回到家后,Crash还在想着课上学的东西,为了研究最小公倍数,他画了一张N*M的表格。每个格子里写了一个数字,其中第i行第j列的那个格子里写着数为LCM(i, j)。一个4*5的表格如下: 1 2 3 4 5 2 2 6 4 10 3 6 3 12 15 4 4 12 4 20 看着这个表格,Crash想到了很多可以思考的问题。不过他最想解决的问题却是一个十分简单的问题:这个表格中所有数的和是多少。当N和M很大时,Crash就束手无策了,因此他找到了聪明的你用程序帮他解决这个问题。由于最终结果可能会很大,Crash只想知道表格里所有数的和mod 20101009的值。

Input

输入的第一行包含两个正整数,分别表示N和M。

Output

输出一个正整数,表示表格中所有数的和mod 20101009的值。

Sample Input

4 5

Sample Output

122

HINT

100%的数据满足N, M ≤ 10^7。

题解

\begin{aligned}ans&=\sum_{i=1}^N\sum_{j=1}^Mlcm(i,j)\\&=\sum_{i=1}^N\sum_{j=1}^M\frac{ij}{gcd(i,j)}\end{aligned}

枚举 $gcd(i,j)$ \begin{aligned}\Rightarrow ans&=\sum_{d=1}^{min\{N,M\}}\sum_{i=1}^{\left\lfloor\frac{N}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{M}{d}\right\rfloor}\frac{ijd^2}{d}[gcd(i,j)=1]\\&=\sum_{d=1}^{min\{N,M\}}d\sum_{i=1}^{\left\lfloor\frac{N}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{M}{d}\right\rfloor}ij\sum_{k\mid gcd(i,j)}\mu(k)\\&=\sum_{d=1}^{min\{N,M\}}d\sum_{k=1}^{min\left\{\left\lfloor\frac{N}{d}\right\rfloor,\left\lfloor\frac{M}{d}\right\rfloor\right\}}\mu(k)\sum_{i=1}^{\left\lfloor\frac{N}{dk}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{M}{dk}\right\rfloor}(ik)\cdot(jk)\\&=\sum_{d=1}^{min\{N,M\}}d\sum_{k=1}^{min\left\{\left\lfloor\frac{N}{d}\right\rfloor,\left\lfloor\frac{M}{d}\right\rfloor\right\}}\mu(k)\cdot k^2\sum_{i=1}^{\left\lfloor\frac{N}{dk}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{M}{dk}\right\rfloor}ij\\&=\sum_{d=1}^{min\{N,M\}}d\sum_{k=1}^{min\left\{\left\lfloor\frac{N}{d}\right\rfloor,\left\lfloor\frac{M}{d}\right\rfloor\right\}}\mu(k)\cdot k^2\left(\sum_{i=1}^{\left\lfloor\frac{N}{dk}\right\rfloor}i\right)\left(\sum_{j=1}^{\left\lfloor\frac{M}{dk}\right\rfloor}j\right)\end{aligned}

设 $g(x)=\mu(x)\cdot x^2$ , $t(x)=\sum_{i=1}^{x}i=\frac{x\cdot(x+1)}{2}$ $$\Rightarrow ans=\sum_{d=1}^{min\{N,M\}}d\sum_{k=1}^{min\left\{\left\lfloor\frac{N}{d}\right\rfloor,\left\lfloor\frac{M}{d}\right\rfloor\right\}}g(k)\cdot t\left(\left\lfloor\frac{N}{dk}\right\rfloor\right)\cdot t\left(\left\lfloor\frac{M}{dk}\right\rfloor\right)$$

现在函数 $g$ 可以线性筛出,函数 $t$ 可以 $O(1)$ 求出,第二个 $\sum$ 中的式子可以 $O(\sqrt N)$ 求,最外层也可以 $O(\sqrt N)$ 求。总复杂度 $O(N)$ 。

 1 //It is made by Awson on 2018.1.23
 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('\n'))
21 #define lowbit(x) ((x)&(-(x)))
22 using namespace std;
23 const int MOD = 20101009;
24 const int N = 1e7;
25 void read(int &x) {
26     char ch; bool flag = 0;
27     for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
28     for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
29     x *= 1-2*flag;
30 }
31 void write(int x) {
32     if (x > 9) write(x/10);
33     putchar(x%10+48);
34 }
35 
36 int n, m, g[N+5];
37 int isprime[N+5], prime[N+5], tot, mu[N+5];
38 
39 void get_g(int N) {
40     memset(isprime, 1, sizeof(isprime)); isprime[1] = 0; mu[1] = g[1] = 1;
41     for (int i = 2; i <= N; i++) {
42     if (isprime[i]) prime[++tot] = i, mu[i] = -1;
43     for (int j = 1; j <= tot && i*prime[j] <= N; j++) {
44         isprime[i*prime[j]] = 0;
45         if (i%prime[j]) mu[i*prime[j]] = -mu[i];
46         else {mu[i*prime[j]] = 0; break; }
47     }
48     g[i] = (g[i-1]+(LL)i*i%MOD*mu[i])%MOD;
49     }
50 }
51 int t(int x) {return (LL)(x+1)*x/2%MOD; }
52 int F(int n, int m) {
53     if (n > m) Swap(n, m); int ans = 0;
54     for (int i = 1, last; i <= n; i = last+1) {
55     last = Min(n/(n/i), m/(m/i));
56     ans = (ans+(LL)(g[last]-g[i-1])*t(n/i)%MOD*t(m/i)%MOD)%MOD;
57     }
58     return ans;
59 }
60 int cal(int n, int m) {
61     if (n > m) Swap(n, m); int ans = 0;
62     for (int i = 1, last; i <= n; i = last+1) {
63     last = Min(n/(n/i), m/(m/i));
64     ans = (ans+(LL)(i+last)*(last-i+1)/2%MOD*F(n/i, m/i)%MOD)%MOD;
65     }
66     return ans;
67 }
68 void work() {
69     read(n), read(m); get_g(Max(n, m)); writeln((cal(n ,m)+MOD)%MOD);
70 }
71 int main() {
72     work();
73     return 0;
74 }

 

posted @ 2018-01-25 16:41  NaVi_Awson  阅读(297)  评论(0编辑  收藏  举报