Problem
jzptab
T i m e L i m i t : 10 S e c \mathrm{Time\;Limit:\;10\;Sec} T i m e L i m i t : 1 0 S e c
M e m o r y L i m i t : 512 M B \mathrm{Memory\;Limit:\;512\;MB} M e m o r y L i m i t : 5 1 2 M B
Description
求∑ i = 1 n ∑ j = 1 m l c m ( i , j ) \sum_{i=1}^{n}\sum_{j=1}^{m}lcm(i,j) ∑ i = 1 n ∑ j = 1 m l c m ( i , j ) ,答案模1 0 9 + 9 10^9+9 1 0 9 + 9 输出。
多组询问。
一个正整数T T T 表示数据组数。
接下来T T T 行,每行两个正整数 表示N , M N,M N , M 。
Output
T T T 行,每行一个整数,表示第i i i 组数据的结果。
Sample Output
HINT
T ≤ 1 0 4 T\le 10^4 T ≤ 1 0 4
N , M ≤ 1 0 7 N,M\le 10^7 N , M ≤ 1 0 7
Sourse
版权所有者:倪泽堃
标签:莫比乌斯反演
Solution
此题和B Z O J 2154 \mathrm{BZOJ2154} B Z O J 2 1 5 4 所求相同,只是又多组询问,如果每次都像那B Z O J 2154 \mathrm{BZOJ2154} B Z O J 2 1 5 4 样O ( n ) O(n) O ( n ) 做为T L E \mathrm{TLE} T L E 。故需要改变求和方式。这里将使用的最终推B Z O J 2154 \mathrm{BZOJ2154} B Z O J 2 1 5 4 导结果来继续恒等变形。前面的推导见:BZOJ2154 。
A n s = ∑ d = 1 min ( n , m ) d ∑ k = 1 min ( ⌊ n d ⌋ , ⌊ m d ⌋ ) μ ( k ) × k 2 × ⌊ n d × k ⌋ × ( ⌊ n d × k ⌋ + 1 ) 2 × ⌊ m d × k ⌋ × ( ⌊ m d × k ⌋ + 1 ) 2 = ∑ d = 1 min ( n , m ) ∑ t = k × d ( k ∈ N ∗ ) min ( n , m ) μ ( t d ) × t 2 d 2 × ⌊ n t ⌋ × ( ⌊ n t ⌋ + 1 ) 2 × ⌊ m t ⌋ × ( ⌊ m t ⌋ + 1 ) 2 × d = ∑ t = 1 min ( n , m ) ⌊ n t ⌋ × ( ⌊ n t ⌋ + 1 ) 2 × ⌊ m t ⌋ × ( ⌊ m t ⌋ + 1 ) 2 ∑ k ∣ t μ ( k ) × k 2 × t k \begin{aligned}
Ans&=\sum_{d=1}^{\min(n,m)}{d}\sum_{k=1}^{\min(\lfloor\frac{n}{d}\rfloor,\lfloor\frac{m}{d}\rfloor)}\mu(k)\times k^2\times\frac{\lfloor\frac{n}{d\times k}\rfloor\times(\lfloor\frac{n}{d\times k}\rfloor+1)}{2}\times\frac{\lfloor\frac{m}{d\times k}\rfloor\times(\lfloor\frac{m}{d\times k}\rfloor+1)}{2}\\
&=\sum_{d=1}^{\min(n,m)}\sum_{t=k\times d(k\in\mathbb{N^*})}^{\min(n,m)}\mu(\frac{t}{d})\times\frac{t^2}{d^2}\times\frac{\lfloor\frac{n}{t}\rfloor\times(\lfloor\frac{n}{t}\rfloor+1)}{2}\times\frac{\lfloor\frac{m}{t}\rfloor\times(\lfloor\frac{m}{t}\rfloor+1)}{2}\times{d}\\
&=\sum_{t=1}^{\min(n,m)}{\frac{\lfloor\frac{n}{t}\rfloor\times(\lfloor\frac{n}{t}\rfloor+1)}{2}\times\frac{\lfloor\frac{m}{t}\rfloor\times(\lfloor\frac{m}{t}\rfloor+1)}{2}}\sum_{k|t}\mu(k)\times k^2\times\frac{t}{k}\\
\end{aligned}
A n s = d = 1 ∑ m i n ( n , m ) d k = 1 ∑ m i n ( ⌊ d n ⌋ , ⌊ d m ⌋ ) μ ( k ) × k 2 × 2 ⌊ d × k n ⌋ × ( ⌊ d × k n ⌋ + 1 ) × 2 ⌊ d × k m ⌋ × ( ⌊ d × k m ⌋ + 1 ) = d = 1 ∑ m i n ( n , m ) t = k × d ( k ∈ N ∗ ) ∑ m i n ( n , m ) μ ( d t ) × d 2 t 2 × 2 ⌊ t n ⌋ × ( ⌊ t n ⌋ + 1 ) × 2 ⌊ t m ⌋ × ( ⌊ t m ⌋ + 1 ) × d = t = 1 ∑ m i n ( n , m ) 2 ⌊ t n ⌋ × ( ⌊ t n ⌋ + 1 ) × 2 ⌊ t m ⌋ × ( ⌊ t m ⌋ + 1 ) k ∣ t ∑ μ ( k ) × k 2 × k t
L e t F ( t ) = ∑ k ∣ t μ ( k ) × k 2 × t k , t h e n S = ∑ t = 1 min ( n , m ) ⌊ n t ⌋ × ( ⌊ n t ⌋ + 1 ) 2 × ⌊ m t ⌋ × ( ⌊ m t ⌋ + 1 ) 2 × F ( t ) \begin{aligned}
&Let\;F(t)=\sum_{k|t}\mu(k)\times k^2\times \frac{t}{k}\ ,\\
&then\;S=\sum_{t=1}^{\min(n,m)}{\frac{\lfloor\frac{n}{t}\rfloor\times(\lfloor\frac{n}{t}\rfloor+1)}{2}\times\frac{\lfloor\frac{m}{t}\rfloor\times(\lfloor\frac{m}{t}\rfloor+1)}{2}}\times F(t)\\
\end{aligned}
L e t F ( t ) = k ∣ t ∑ μ ( k ) × k 2 × k t , t h e n S = t = 1 ∑ m i n ( n , m ) 2 ⌊ t n ⌋ × ( ⌊ t n ⌋ + 1 ) × 2 ⌊ t m ⌋ × ( ⌊ t m ⌋ + 1 ) × F ( t )
∵ f ( x ) = μ ( x ) , g ( x ) = x 2 , h ( x ) = t x a r e a l l m u l t i p l i c a t i v e f u n c t i o n s ∴ F ( x ) = ∑ t ∣ x f ( t ) × g ( t ) × h ( t ) i s a m u l t i p l i c a t i v e f u n c t i o n ⟹ W e c a n u s e a L i n e a r S e i v e t o c a l c u l a t e F ( x ) I f x ≡ 1 ∼ y − 1 m o d y ( y i s a p r i m e n u m b e r ) t h e n F ( x × y ) = F ( x ) × F ( y ) I f x ≡ 0 m o d y ( y i s a p r i m e n u m b e r ) t h e n μ ( x × y ) = 0 , F ( x × y ) = F ( x ) × y \begin{aligned}
&\because f(x)=\mu(x),\;g(x)=x^2,\;h(x)=\frac{t}{x}\;are\;all\;multiplicative\;functions\\
&\therefore F(x)=\sum_{t|x}f(t)\times g(t)\times h(t)\;is\;a\;multiplicative\;function\\
&\Longrightarrow We\;can\;use\;a\;Linear\;Seive\;to\;calculate\;F(x)\\
&If\;x\equiv1\sim y-1\mod{y}\;\;(y\;is\;a\;prime\;number)\\
&\;\;\;\;then\;F(x\times y)=F(x)\times F(y)\\
&If\;x\equiv0\mod{y}\;\;(y\;is\;a\;prime\;number)\\
&\;\;\;\;then\;\mu(x\times y)=0,\;F(x\times y)=F(x)\times y
\end{aligned}
∵ f ( x ) = μ ( x ) , g ( x ) = x 2 , h ( x ) = x t a r e a l l m u l t i p l i c a t i v e f u n c t i o n s ∴ F ( x ) = t ∣ x ∑ f ( t ) × g ( t ) × h ( t ) i s a m u l t i p l i c a t i v e f u n c t i o n ⟹ W e c a n u s e a L i n e a r S e i v e t o c a l c u l a t e F ( x ) I f x ≡ 1 ∼ y − 1 m o d y ( y i s a p r i m e n u m b e r ) t h e n F ( x × y ) = F ( x ) × F ( y ) I f x ≡ 0 m o d y ( y i s a p r i m e n u m b e r ) t h e n μ ( x × y ) = 0 , F ( x × y ) = F ( x ) × y
综上,F ( x ) F(x) F ( x ) 的前缀和可用线性筛预处理,对于每次询问对⌊ n t ⌋ × ( ⌊ n t ⌋ + 1 ) 2 × ⌊ m t ⌋ × ( ⌊ m t ⌋ + 1 ) 2 \frac{\lfloor\frac{n}{t}\rfloor\times(\lfloor\frac{n}{t}\rfloor+1)}{2}\times\frac{\lfloor\frac{m}{t}\rfloor\times(\lfloor\frac{m}{t}\rfloor+1)}{2} 2 ⌊ t n ⌋ × ( ⌊ t n ⌋ + 1 ) × 2 ⌊ t m ⌋ × ( ⌊ t m ⌋ + 1 ) 根号分块,即可做到O ( T n ) O(T\sqrt{n}) O ( T n ) 的复杂度。
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include <bits/stdc++.h> #define MAX_N 10000000 #define MOD 100000009 using namespace std;typedef long long lnt;template <class T >inline void read (T &x) { x = 0 ; int c = getchar (), f = 1 ; for (; !isdigit (c); c = getchar ()) if (c == 45 ) f = -1 ; for (; isdigit (c); c = getchar ()) (x *= 10 ) += f*(c-'0' ); } lnt n, m, cnt, ans, s[MAX_N+5 ], pri[MAX_N+5 ]; bool NotPri[MAX_N+5 ]; void init () { NotPri[1 ] = true , s[1 ] = 1 ; for (lnt i = 2 ; i <= MAX_N; i++) { if (!NotPri[i]) pri[cnt++] = i, s[i] = (i-i*i%MOD)%MOD; for (int j = 0 ; j < cnt; j++) { if (i*pri[j] > MAX_N) break ; NotPri[i*pri[j]] = true ; if (i%pri[j]) s[i*pri[j]] = s[i]*s[pri[j]]%MOD; else {s[i*pri[j]] = s[i]*pri[j]; break ;} } } for (int i = 1 ; i <= MAX_N; i++) (s[i] += s[i-1 ]) %= MOD; } int main () { int T; read (T), init (); while (T--) { lnt n, m; read (n), read (m), ans = 0 ; for (lnt l = 1 , r; l <= min (n, m); l = r+1 ) r = min (n/(n/l), m/(m/l)), (ans += (n/l*(n/l+1 )/2 %MOD)*(m/l*(m/l+1 )/2 %MOD)%MOD*(s[r]-s[l-1 ])%MOD) %= MOD; printf ("%lld\n" , (ans+MOD)%MOD); } return 0 ; }