https://www.luogu.com.cn/problem/P2834
求 ∑ i = 1 n ∑ j = 1 m ( n m o d i ) ( m m o d j ) [ i ! = j ] \sum_{i=1}^n \sum_{j=1}^m (n\ mod\ i)(m\ mod\ j)[i!=j] i=1∑nj=1∑m(n mod i)(m mod j)[i!=j]
数据范围 n , m ≤ 1 0 9 n,m\leq 10^9 n,m≤109
强令 n ≤ m n\leq m n≤m ∑ i = 1 n ∑ j = 1 m ( n m o d i ) ( m m o d j ) − ∑ i = 1 n ( n m o d i ) ( m m o d i ) \sum_{i=1}^n \sum_{j=1}^m (n\ mod\ i)(m\ mod\ j)-\sum_{i=1}^n(n\ mod\ i)(m\ mod \ i) i=1∑nj=1∑m(n mod i)(m mod j)−i=1∑n(n mod i)(m mod i)
转换一下 m o d mod mod ∑ i = 1 n ∑ j = 1 m ( n − i ⌊ n i ⌋ ) ( m − i ⌊ m j ⌋ ) − ∑ i = 1 n ( n − i ⌊ n i ⌋ ) ( m − i ⌊ m i ⌋ ) \sum_{i=1}^n \sum_{j=1}^m (n-i\lfloor \frac n i\rfloor)(m-i\lfloor\frac m j\rfloor)-\sum_{i=1}^n(n-i\lfloor \frac n i\rfloor)(m-i\lfloor\frac m i\rfloor) i=1∑nj=1∑m(n−i⌊in⌋)(m−i⌊jm⌋)−i=1∑n(n−i⌊in⌋)(m−i⌊im⌋)
注意第一项中 i , j i,j i,j是不相关的,可以分开计算,再相乘
∑ i = 1 n ( n − i ⌊ n i ⌋ ) × ∑ i = 1 m ( m − i ⌊ m i ⌋ ) − ∑ i = 1 n ( n m − m i ⌊ n i ⌋ − n i ⌊ m i ⌋ + i 2 ⌊ m i ⌋ ⌊ n i ⌋ ) \sum_{i=1}^n (n-i\lfloor \frac n i\rfloor) \times \sum_{i=1}^m(m-i\lfloor\frac m i\rfloor)-\sum_{i=1}^n (nm-mi\lfloor \frac n i\rfloor-ni\lfloor \frac m i\rfloor+i^2\lfloor \frac m i\rfloor\lfloor \frac n i\rfloor) i=1∑n(n−i⌊in⌋)×i=1∑m(m−i⌊im⌋)−i=1∑n(nm−mi⌊in⌋−ni⌊im⌋+i2⌊im⌋⌊in⌋)
然后有 ∑ i = 1 n i 2 = n ( n + 1 ) ( 2 n + 1 ) 6 \sum_{i=1}^n i^2=\frac {n(n+1)(2n+1)}{6} ∑i=1ni2=6n(n+1)(2n+1)
乘除分块就做完了,复杂度 O ( m ) O(\sqrt m) O(m )