1. 程式人生 > >BZOJ4815: [Cqoi2017]小Q的表格

BZOJ4815: [Cqoi2017]小Q的表格

傳送門

重點 1 1
f ( a ,

a + b ) a ( a +
b )
= f ( a ,
b )
a b
= f ( a , b a ) a ( b a ) = f ( a , b   m o d   a ) a ( b   m o d   a ) = f ( d , d ) d 2 \frac{f(a,a+b)}{a(a+b)}=\frac{f(a,b)}{ab}=\frac{f(a,b-a)}{a(b-a)}=\frac{f(a,b~mod~a)}{a(b~mod~a)}=\frac{f(d,d)}{d^2}
其中 d = g c d ( a , b + b ) d=gcd(a,b+b)
那麼
a n s = i = 1 k j = 1 k f ( i , j ) = i = 1 k j = 1 k d = 1 k [ g c d ( i , j ) = = d ] i j f ( d , d ) d 2 ans=\sum_{i=1}^{k}\sum_{j=1}^{k}f(i,j)=\sum_{i=1}^{k}\sum_{j=1}^{k}\sum_{d=1}^{k}[gcd(i,j)==d]\frac{ijf(d,d)}{d^2}
最終可以得到
a n s = d = 1 k f ( d , d ) i = 1 k d i = 1 k d [ i j ] i j ans=\sum_{d=1}^{k}f(d,d)\sum_{i=1}^{\lfloor\frac{k}{d}\rfloor}\sum_{i=1}^{\lfloor\frac{k}{d}\rfloor}[i\perp j]ij
重點 2 2
考慮求
i = 1 x j = 1 x [ i j ] i j = 2 i = 1 x i j = 1 i [ i j ] j 1 \sum_{i=1}^{x}\sum_{j=1}^{x}[i\perp j]ij=2\sum_{i=1}^{x}i\sum_{j=1}^{i}[i\perp j]j-1
除了 i = 1 i=1 以外,小於等於 i i 的與 i i 互質的數成對存在,那麼這些數字的和就是 φ ( i ) i 2 \frac{\varphi(i)i}{2}
所以上面變成
i = 1 x φ ( i ) i 2 \sum_{i=1}^{x}\varphi(i)i^2

a n s = d = 1 k f ( d , d ) i = 1 k d φ ( i ) i 2 ans=\sum_{d=1}^{k}f(d,d)\sum_{i=1}^{\lfloor\frac{k}{d}\rfloor}\varphi(i)i^2
篩出 i = 1 x φ ( i ) i 2 \sum_{i=1}^{x}\varphi(i)i^2
維護 f ( d , d ) f(d,d) 的字首和即可
分塊即可暴力樹狀陣列就過了

# include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int mod(1e9 + 7);
const int maxn(4e6 + 5);

inline int Pow(ll x, int y) {
	register ll ret = 1;
	for (; y; y >>= 1, x = x * x % mod)
		if (y & 1) ret = ret * x % mod;
	return ret;
}

inline void Inc(int &x, int y) {
	x = x + y >= mod ? x + y - mod : x + y;
}

int n, m, f[maxn], pr[maxn / 10], tot, sum<