Problem
Number Challenge
T i m e L i m i t p e r t e s t : 3 s e c o n d s \mathrm{Time\;Limit\;per\;test:\;3\;seconds} T i m e L i m i t p e r t e s t : 3 s e c o n d s
M e m o r y L i m i t p e r t e s t : 512 m e g a b y t e s \mathrm{Memory\;Limit\;per\;test:\;512\;megabytes} M e m o r y L i m i t p e r t e s t : 5 1 2 m e g a b y t e s
I n p u t : s t a n d a r d i n p u t \mathrm{Input:\;standard\;input} I n p u t : s t a n d a r d i n p u t
O u t p u t : s t a n d a r d o u t p u t \mathrm{Output:\;standard\;output} O u t p u t : s t a n d a r d o u t p u t
Description
Let’s denote d ( n ) d(n) d ( n ) as the number of divisors of a positive integer n n n . You are given three integers a a a , b b b and c c c . Your task is to calculate the following sum:
∑ i = 1 a ∑ j = 1 b ∑ k = 1 c d ( i ⋅ j ⋅ k ) \sum_{i=1}^{a}\sum_{j=1}^{b}\sum_{k=1}^{c}d(i·j·k) ∑ i = 1 a ∑ j = 1 b ∑ k = 1 c d ( i ⋅ j ⋅ k )
Find the sum modulo 1073741824 ( 2 30 ) 1073741824 (2^{30}) 1 0 7 3 7 4 1 8 2 4 ( 2 3 0 ) .
The first line contains three space-separated integers a a a , b b b and c c c ( 1 ≤ a , b , c ≤ 2000 ) (1\le a,b,c\le 2000) ( 1 ≤ a , b , c ≤ 2 0 0 0 ) .
Output
Print a single integer — the required sum modulo 1073741824 ( 2 30 ) 1073741824 (2^{30}) 1 0 7 3 7 4 1 8 2 4 ( 2 3 0 ) .
Examples
Input 1
Output 1
Input 2
Output 2
Input 3
Output 3
Note
For the first example.
d ( 1 ⋅ 1 ⋅ 1 ) = d ( 1 ) = 1 ; d(1·1·1)=d(1)=1; d ( 1 ⋅ 1 ⋅ 1 ) = d ( 1 ) = 1 ;
d ( 1 ⋅ 1 ⋅ 2 ) = d ( 2 ) = 2 ; d(1·1·2)=d(2)=2; d ( 1 ⋅ 1 ⋅ 2 ) = d ( 2 ) = 2 ;
d ( 1 ⋅ 2 ⋅ 1 ) = d ( 2 ) = 2 ; d(1·2·1)=d(2)=2; d ( 1 ⋅ 2 ⋅ 1 ) = d ( 2 ) = 2 ;
d ( 1 ⋅ 2 ⋅ 2 ) = d ( 4 ) = 3 ; d(1·2·2)=d(4)=3; d ( 1 ⋅ 2 ⋅ 2 ) = d ( 4 ) = 3 ;
d ( 2 ⋅ 1 ⋅ 1 ) = d ( 2 ) = 2 ; d(2·1·1)=d(2)=2; d ( 2 ⋅ 1 ⋅ 1 ) = d ( 2 ) = 2 ;
d ( 2 ⋅ 1 ⋅ 2 ) = d ( 4 ) = 3 ; d(2·1·2)=d(4)=3; d ( 2 ⋅ 1 ⋅ 2 ) = d ( 4 ) = 3 ;
d ( 2 ⋅ 2 ⋅ 1 ) = d ( 4 ) = 3 ; d(2·2·1)=d(4)=3; d ( 2 ⋅ 2 ⋅ 1 ) = d ( 4 ) = 3 ;
d ( 2 ⋅ 2 ⋅ 2 ) = d ( 8 ) = 4. d(2·2·2)=d(8)=4. d ( 2 ⋅ 2 ⋅ 2 ) = d ( 8 ) = 4 .
So the result is 1 + 2 + 2 + 3 + 2 + 3 + 3 + 4 = 20 1+2+2+3+2+3+3+4=20 1 + 2 + 2 + 3 + 2 + 3 + 3 + 4 = 2 0 .
标签:莫比乌斯反演
Translation
给出三个整数a , b , c a,b,c a , b , c (a , b , c ≤ 2000 a,b,c\le2000 a , b , c ≤ 2 0 0 0 ),求∑ i = 1 a ∑ j = 1 b ∑ k = 1 c d ( i j k ) \sum_{i=1}^{a}\sum_{j=1}^{b}\sum_{k=1}^{c}d(ijk) ∑ i = 1 a ∑ j = 1 b ∑ k = 1 c d ( i j k ) 。
Solution
还记得BZOJ3994【SDOI2015】约束个数和 吗?这是那道题的加强版本。
做那道题的时候,我们得到了一个重要结论,即d ( x ⋅ y ) = ∑ i ∣ x ∑ j ∣ y [ gcd ( i , j ) = 1 ] d(x\cdot y)=\sum_{i|x}\sum_{j|y}[\gcd(i,j)=1] d ( x ⋅ y ) = ∑ i ∣ x ∑ j ∣ y [ g cd( i , j ) = 1 ] 。
这个结论可以推广到三维,即
d ( x ⋅ y ⋅ z ) = ∑ i ∣ x ∑ j ∣ y ∑ k ∣ z [ gcd ( i , j ) = 1 ] [ gcd ( j , k ) = 1 ] [ gcd ( i , k ) = 1 ] d(x\cdot y\cdot z)=\sum_{i|x}\sum_{j|y}\sum_{k|z}[\gcd(i,j)=1][\gcd(j,k)=1][\gcd(i,k)=1]
d ( x ⋅ y ⋅ z ) = i ∣ x ∑ j ∣ y ∑ k ∣ z ∑ [ g cd( i , j ) = 1 ] [ g cd( j , k ) = 1 ] [ g cd( i , k ) = 1 ]
具体证明见rng_68的证明 。
然后就可以很笨拙地套路反演将一个[ gcd = 1 ] [\gcd=1] [ g cd= 1 ] 化为μ \mu μ 的和式移到前面了。
∑ p = 1 a ∑ q = 1 b ∑ r = 1 c d ( p q r ) = ∑ p = 1 a ∑ q = 1 b ∑ r = 1 c ∑ i ∣ p ∑ j ∣ q ∑ k ∣ r [ gcd ( i , j ) = 1 ] [ gcd ( i , k ) = 1 ] [ gcd ( j , k ) = 1 ] = ∑ i = 1 a ∑ j = 1 b ∑ k = 1 c [ gcd ( i , j ) = 1 ] [ gcd ( i , k ) = 1 ] [ gcd ( j , k ) = 1 ] ⌊ a i ⌋ ⌊ b j ⌋ ⌊ c k ⌋ = ∑ i = 1 a ∑ j = 1 b ∑ k = 1 c ∑ d ∣ i , j μ ( d ) [ gcd ( i , k ) = 1 ] [ gcd ( j , k ) = 1 ] ⌊ a i ⌋ ⌊ b j ⌋ ⌊ c k ⌋ = ∑ k = 1 c ⌊ c k ⌋ ∑ i = 1 a ∑ j = 1 b ∑ d ∣ i , j μ ( d ) [ gcd ( i , k ) = 1 ] [ gcd ( j , k ) = 1 ] ⌊ a i ⌋ ⌊ b j ⌋ = ∑ k = 1 c ⌊ c k ⌋ ∑ d = 1 min ( a , b ) μ ( d ) ∑ x = 1 ⌊ a d ⌋ ∑ y = 1 ⌊ b d ⌋ [ gcd ( x ⋅ d , k ) = 1 ] [ gcd ( y ⋅ d , k ) = 1 ] ⌊ a x ⋅ d ⌋ ⌊ b y ⋅ d ⌋ = ∑ k = 1 c ⌊ c k ⌋ ∑ d = 1 min ( a , b ) μ ( d ) ∑ x = 1 ⌊ a d ⌋ ⌊ a x ⋅ d ⌋ [ gcd ( x ⋅ d , k ) = 1 ] ∑ y = 1 ⌊ b d ⌋ ⌊ b y ⋅ d ⌋ [ gcd ( y ⋅ d , k ) = 1 ] = ∑ k = 1 c ⌊ c k ⌋ ∑ d = 1 min ( a , b ) [ gcd ( d , k ) = 1 ] μ ( d ) ∑ x = 1 ⌊ a d ⌋ ⌊ a x ⋅ d ⌋ [ gcd ( x , k ) = 1 ] ∑ y = 1 ⌊ b d ⌋ ⌊ b y ⋅ d ⌋ [ gcd ( y , k ) = 1 ] \begin{aligned}
&\;\;\;\;\sum_{p=1}^{a}\sum_{q=1}^{b}\sum_{r=1}^{c}d(pqr)\\
&=\sum_{p=1}^{a}\sum_{q=1}^{b}\sum_{r=1}^{c}\sum_{i|p}\sum_{j|q}\sum_{k|r}[\gcd(i,j)=1][\gcd(i,k)=1][\gcd(j,k)=1]\\
&=\sum_{i=1}^{a}\sum_{j=1}^{b}\sum_{k=1}^{c}[\gcd(i,j)=1][\gcd(i,k)=1][\gcd(j,k)=1]\lfloor\frac{a}{i}\rfloor\lfloor\frac{b}{j}\rfloor\lfloor\frac{c}{k}\rfloor\\
&=\sum_{i=1}^{a}\sum_{j=1}^{b}\sum_{k=1}^{c}\sum_{d|i,j}\mu(d)[\gcd(i,k)=1][\gcd(j,k)=1]\lfloor\frac{a}{i}\rfloor\lfloor\frac{b}{j}\rfloor\lfloor\frac{c}{k}\rfloor\\
&=\sum_{k=1}^{c}\lfloor\frac{c}{k}\rfloor\sum_{i=1}^{a}\sum_{j=1}^{b}\sum_{d|i,j}\mu(d)[\gcd(i,k)=1][\gcd(j,k)=1]\lfloor\frac{a}{i}\rfloor\lfloor\frac{b}{j}\rfloor\\
&=\sum_{k=1}^{c}\lfloor\frac{c}{k}\rfloor\sum_{d=1}^{\min(a,b)}\mu(d)\sum_{x=1}^{\lfloor\frac{a}{d}\rfloor}\sum_{y=1}^{\lfloor\frac{b}{d}\rfloor}[\gcd(x\cdot d,k)=1][\gcd(y\cdot d,k)=1]\lfloor\frac{a}{x\cdot d}\rfloor\lfloor\frac{b}{y\cdot d}\rfloor\\
&=\sum_{k=1}^{c}\lfloor\frac{c}{k}\rfloor\sum_{d=1}^{\min(a,b)}\mu(d)\sum_{x=1}^{\lfloor\frac{a}{d}\rfloor}\lfloor\frac{a}{x\cdot d}\rfloor[\gcd(x\cdot d,k)=1]\sum_{y=1}^{\lfloor\frac{b}{d}\rfloor}\lfloor\frac{b}{y\cdot d}\rfloor[\gcd(y\cdot d,k)=1]\\
&=\sum_{k=1}^{c}\lfloor\frac{c}{k}\rfloor\sum_{d=1}^{\min(a,b)}[\gcd(d,k)=1]\mu(d)\sum_{x=1}^{\lfloor\frac{a}{d}\rfloor}\lfloor\frac{a}{x\cdot d}\rfloor[\gcd(x,k)=1]\sum_{y=1}^{\lfloor\frac{b}{d}\rfloor}\lfloor\frac{b}{y\cdot d}\rfloor[\gcd(y,k)=1]\\
\end{aligned}
p = 1 ∑ a q = 1 ∑ b r = 1 ∑ c d ( p q r ) = p = 1 ∑ a q = 1 ∑ b r = 1 ∑ c i ∣ p ∑ j ∣ q ∑ k ∣ r ∑ [ g cd( i , j ) = 1 ] [ g cd( i , k ) = 1 ] [ g cd( j , k ) = 1 ] = i = 1 ∑ a j = 1 ∑ b k = 1 ∑ c [ g cd( i , j ) = 1 ] [ g cd( i , k ) = 1 ] [ g cd( j , k ) = 1 ] ⌊ i a ⌋ ⌊ j b ⌋ ⌊ k c ⌋ = i = 1 ∑ a j = 1 ∑ b k = 1 ∑ c d ∣ i , j ∑ μ ( d ) [ g cd( i , k ) = 1 ] [ g cd( j , k ) = 1 ] ⌊ i a ⌋ ⌊ j b ⌋ ⌊ k c ⌋ = k = 1 ∑ c ⌊ k c ⌋ i = 1 ∑ a j = 1 ∑ b d ∣ i , j ∑ μ ( d ) [ g cd( i , k ) = 1 ] [ g cd( j , k ) = 1 ] ⌊ i a ⌋ ⌊ j b ⌋ = k = 1 ∑ c ⌊ k c ⌋ d = 1 ∑ m i n ( a , b ) μ ( d ) x = 1 ∑ ⌊ d a ⌋ y = 1 ∑ ⌊ d b ⌋ [ g cd( x ⋅ d , k ) = 1 ] [ g cd( y ⋅ d , k ) = 1 ] ⌊ x ⋅ d a ⌋ ⌊ y ⋅ d b ⌋ = k = 1 ∑ c ⌊ k c ⌋ d = 1 ∑ m i n ( a , b ) μ ( d ) x = 1 ∑ ⌊ d a ⌋ ⌊ x ⋅ d a ⌋ [ g cd( x ⋅ d , k ) = 1 ] y = 1 ∑ ⌊ d b ⌋ ⌊ y ⋅ d b ⌋ [ g cd( y ⋅ d , k ) = 1 ] = k = 1 ∑ c ⌊ k c ⌋ d = 1 ∑ m i n ( a , b ) [ g cd( d , k ) = 1 ] μ ( d ) x = 1 ∑ ⌊ d a ⌋ ⌊ x ⋅ d a ⌋ [ g cd( x , k ) = 1 ] y = 1 ∑ ⌊ d b ⌋ ⌊ y ⋅ d b ⌋ [ g cd( y , k ) = 1 ]
令f ( n , x ) = ∑ i = 1 n [ gcd ( i , x ) = 1 ] ⌊ n i ⌋ f(n,x)=\sum_{i=1}^{n}[\gcd(i,x)=1]\lfloor\frac{n}{i}\rfloor f ( n , x ) = ∑ i = 1 n [ g cd( i , x ) = 1 ] ⌊ i n ⌋ ,那么
A n s = ∑ k = 1 c ∑ d = 1 min ( a , b ) [ gcd ( d , k ) = 1 ] μ ( d ) f ( ⌊ a d ⌋ , k ) f ( ⌊ b d ⌋ , k ) Ans=\sum_{k=1}^{c}\sum_{d=1}^{\min(a,b)}[\gcd(d,k)=1]\mu(d)f(\lfloor\frac{a}{d}\rfloor,k)f(\lfloor\frac{b}{d}\rfloor,k)
A n s = k = 1 ∑ c d = 1 ∑ m i n ( a , b ) [ g cd( d , k ) = 1 ] μ ( d ) f ( ⌊ d a ⌋ , k ) f ( ⌊ d b ⌋ , k )
到这里就可以暴力做了。由于当且仅当d , k d,k d , k 互质时才会计算f f f ,所以直接暴力计算是可以过2000 2000 2 0 0 0 这种比较小的数据的。
据说可以做到a , b , c ≤ 1 0 5 a,b,c\le10^5 a , b , c ≤ 1 0 5 ,不过我不会。
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 36 37 38 39 40 41 42 43 #include <bits/stdc++.h> #define MAX_N 2000 #define MOD 1073741824 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' ); } bool NotPri[MAX_N+5 ];int cnt, pri[MAX_N+5 ], mu[MAX_N+5 ];int cache[MAX_N+5 ][MAX_N+5 ];int gcd (int a, int b) { if (cache[a][b]) return cache[a][b]; return cache[a][b] = (b ? gcd (b, a%b) : a); } void init () { mu[1 ] = 1 ; for (int i = 2 ; i <= MAX_N; i++) { if (!NotPri[i]) pri[cnt++] = i, mu[i] = -1 ; for (int j = 0 ; j < cnt; j++) { if (i*pri[j] > MAX_N) break ; NotPri[i*pri[j]] = true ; if (i%pri[j]) mu[i*pri[j]] = -mu[i]; else {mu[i*pri[j]] = 0 ; break ;} } } } lnt f (int n, int x) { lnt ret = 0 ; for (int i = 1 ; i <= n; i++) if (gcd (i, x) == 1 ) (ret += n/i) %= MOD; return ret; } int main () { int a, b, c; lnt ans = 0 ; read (a), read (b), read (c), init (); for (int k = 1 ; k <= c; k++) for (int d = 1 ; d <= min (a, b); d++) if (gcd (d, k) == 1 ) (ans += 1LL *(c/k)*mu[d]%MOD*f (a/d, k)%MOD*f (b/d, k)%MOD) %= MOD; return printf ("%lld\n" , (ans+MOD)%MOD), 0 ; }