题目链接
题目描述
求
∑ ∑ ( ( n m o d i ) ∗ ( m m o d j ) ) 其 中 1 <= i <= n , 1 <= j <= m , i ≠ j " role="presentation" style="position: relative;">∑ ∑ ( ( n m o d i ) ∗ ( m m o d j ) ) 其 中 1 <= i <= n , 1 <= j <= m , i ≠ j ∑ ∑ ( ( n m o d i ) ∗ ( m m o d j ) ) 其 中 1 <= i <= n , 1 <= j <= m , i ≠ j 。
例如对于 n=3,m=4 :
答案为(3 mod 1)
(4 mod 2)+(3 mod 1) (4 mod 3)+(3 mod 1) * (4 mod 4) + (3 mod 2) * (4 mod 1) + (3 mod 2) * (4 mod 3) + (3 mod 2) * (4 mod 4) + (3 mod 3) * (4 mod 1) + (3 mod 3) * (4 mod 2) + (3 mod 3) * (4 mod 4) = 1
题解
可以先去做做
余数求和 ,我的
题解 。
一开始看漏了
i ≠ j " role="presentation" style="position: relative;">i ≠ j i ≠ j ,以为这和余数求和一样…
但是既然又和取模的和有关,与余数求和的思想是差不多的。
我们有
a m o d b = a − f l o o r ( a / b ) ∗ b " role="presentation" style="position: relative;">a m o d b = a − f l o o r ( a / b ) ∗ b a m o d b = a − f l o o r ( a / b ) ∗ b
于是我们再来推一波式子:
∑ i = 1 n ∑ j = 1 , j ≠ i m ( ( n m o d i ) ∗ ( m m o d j ) ) " role="presentation">∑ i = 1 n ∑ j = 1 , j ≠ i m ( ( n m o d i ) ∗ ( m m o d j ) ) ∑ i = 1 n ∑ j = 1 , j ≠ i m ( ( n m o d i ) ∗ ( m m o d j ) )
= ∑ ∑ ( ( n m o d i ) ∗ ( m m o d j ) ) − ∑ i = 1 m i n ( n , m ) ( ( n m o d i ) ∗ ( m m o d i ) ) " role="presentation" style="position: relative;">= ∑ ∑ ( ( n m o d i ) ∗ ( m m o d j ) ) − ∑ m i n ( n , m ) i = 1 ( ( n m o d i ) ∗ ( m m o d i ) ) = ∑ ∑ ( ( n m o d i ) ∗ ( m m o d j ) ) − ∑ i = 1 m i n ( n , m ) ( ( n m o d i ) ∗ ( m m o d i ) )
前面那段就是余数求和的方法,不多讲,我们来看后面一段。
不妨设
n ≤ m " role="presentation" style="position: relative;">n ≤ m n ≤ m
那么:
∑ i = 1 n ( ( n m o d i ) ∗ ( m m o d i ) ) " role="presentation" style="position: relative;">∑ n i = 1 ( ( n m o d i ) ∗ ( m m o d i ) ) ∑ i = 1 n ( ( n m o d i ) ∗ ( m m o d i ) )
= ∑ ( ( n − n / i ∗ i ) ∗ ( m − m / i ∗ i ) ) " role="presentation" style="position: relative;">= ∑ ( ( n − n / i ∗ i ) ∗ ( m − m / i ∗ i ) ) = ∑ ( ( n − n / i ∗ i ) ∗ ( m − m / i ∗ i ) )
令
n / i = x , m / i = y " role="presentation" style="position: relative;">n / i = x , m / i = y n / i = x , m / i = y
原式化为:
∑ ( m n − m x i − n y i + x y ∗ i 2 ) " role="presentation" style="position: relative;">∑ ( m n − m x i − n y i + x y ∗ i 2 ) ∑ ( m n − m x i − n y i + x y ∗ i 2 )
∑ ( m n + x y ∗ i 2 − ( m x + n y ) ∗ i ) " role="presentation" style="position: relative;">∑ ( m n + x y ∗ i 2 − ( m x + n y ) ∗ i ) ∑ ( m n + x y ∗ i 2 − ( m x + n y ) ∗ i )
到这里式子已经推的差不多了。
我们发现可以对
x , y " role="presentation" style="position: relative;">x , y x , y 进行除法分块,且前面的mn可以预先处理掉。
于是我们着眼于求出一个块内的
( x y ∗ i 2 − ( m x + n y ) ∗ i ) " role="presentation" style="position: relative;">( x y ∗ i 2 − ( m x + n y ) ∗ i ) ( x y ∗ i 2 − ( m x + n y ) ∗ i )
假定当前块内