对于给出的 n n n 个询问,每次求有多少个数对 ( x , y ) (x,y) (x,y),满足 a ≤ x ≤ b a \le x \le b a≤x≤b, c ≤ y ≤ d c \le y \le d c≤y≤d,且 gcd ( x , y ) = k \gcd(x,y) = k gcd(x,y)=k, gcd ( x , y ) \gcd(x,y) gcd(x,y) 函数为 x x x 和 y y y 的最大公约数。
第一行一个整数 n n n,接下来 n n n 行每行五个整数,分别表示 a , b , c , d , k a,b,c,d,k a,b,c,d,k。
共 n n n 行,每行一个整数表示满足要求的数对 ( x , y ) (x,y) (x,y) 的个数。
2 2 5 1 5 1 1 5 1 5 2
14 3
对于 100 % 100\% 100% 的数据满足: 1 ≤ n , k ≤ 5 × 1 0 4 1 \le n,k \le 5 \times 10^4 1≤n,k≤5×104, 1 ≤ a ≤ b ≤ 5 × 1 0 4 1 \le a \le b \le 5 \times 10^4 1≤a≤b≤5×104, 1 ≤ c ≤ d ≤ 5 × 1 0 4 1 \le c \le d \le 5 \times 10^4 1≤c≤d≤5×104。
令二元函数 f ( x , y ) = ∑ i = 1 x ∑ j = 1 y [ gcd ( i , j ) = k ] f(x,y)=\sum\limits_{i=1}^x\sum\limits_{j=1}^y [\gcd(i,j)=k] f(x,y)=i=1∑xj=1∑y[gcd(i,j)=k],其中 x , y ∈ N ∗ x,y\in N^* x,y∈N∗。由题意,所求答案为 ∑ i = a b ∑ j = c d [ gcd ( i , j ) = k ] \sum\limits_{i=a}^b\sum\limits_{j=c}^d[\gcd(i,j)=k] i=a∑bj=c∑d[gcd(i,j)=k];依据容斥原理, a n s = f ( b , d ) − f ( a − 1 , d ) − f ( b , c − 1 ) + f ( a − 1 , c − 1 ) ans=f(b,d)-f(a-1,d)-f(b,c-1)+f(a-1,c-1) ans=f(b,d)−f(a−1,d)−f(b,c−1)+f(a−1,c−1)。 任务转化为,对于给定的 n , m ∈ N ∗ n,m\in N^* n,m∈N∗,求 f ( n , m ) = ∑ i = 1 n ∑ j = 1 m [ gcd ( i , j ) = k ] f(n,m)=\sum\limits_{i=1}^n\sum\limits_{j=1}^m [\gcd(i,j)=k] f(n,m)=i=1∑nj=1∑m[gcd(i,j)=k] 的值。 首先, k k k 为 i , j i,j i,j 的最大公约数,故 i k \frac{i}{k} ki 与 j k \frac{j}{k} kj 互质,即 gcd ( i k , j k ) = 1 \gcd(\frac{i}{k},\frac{j}{k})=1 gcd(ki,kj)=1。所以 f ( n , m ) = ∑ i = 1 n ∑ j = 1 m [ gcd ( i k , j k ) = 1 ] f(n,m)=\sum\limits_{i=1}^n\sum\limits_{j=1}^m \left[\gcd\left(\frac{i}{k},\frac{j}{k}\right)=1\right] f(n,m)=i=1∑nj=1∑m[gcd(ki,kj)=1];从此式看出, i k \frac{i}{k} ki 的上限为 ⌊ n k ⌋ \left\lfloor\frac{n}{k}\right\rfloor ⌊kn⌋, j k \frac{j}{k} kj 的上限为 ⌊ m k ⌋ \left\lfloor\frac{m}{k}\right\rfloor ⌊km⌋;因此等价于, f ( n , m ) = ∑ i = 1 ⌊ n k ⌋ ∑ j = 1 ⌊ m k ⌋ [ gcd ( i , j ) = 1 ] f(n,m)=\sum\limits_{i=1}^{\left\lfloor\frac{n}{k}\right\rfloor}\sum\limits_{j=1}^{\left\lfloor\frac{m}{k}\right\rfloor}[\gcd(i,j)=1] f(n,m)=i=1∑⌊kn⌋j=1∑⌊km⌋[gcd(i,j)=1]。 接下来利用莫比乌斯反演的性质, gcd ( i , j ) = 1 ⇔ ∑ d ∣ gcd ( i , j ) μ ( d ) \gcd(i,j)=1\Leftrightarrow\sum\limits_{d\mid\gcd(i,j)}\mu(d) gcd(i,j)=1⇔d∣gcd(i,j)∑μ(d)。故 f ( n , m ) = ∑ i = 1 ⌊ n k ⌋ ∑ j = 1 ⌊ m k ⌋ ∑ d ∣ gcd ( i , j ) μ ( d ) f(n,m)=\sum\limits_{i=1}^{\left\lfloor\frac{n}{k}\right\rfloor}\sum\limits_{j=1}^{\left\lfloor\frac{m}{k}\right\rfloor}\sum\limits_{d\mid\gcd(i,j)}\mu(d) f(n,m)=i=1∑⌊kn⌋j=1∑⌊km⌋d∣gcd(i,j)∑μ(d)。 接下来变换求和次序,改为枚举 d d d。 f ( n , m ) = ∑ d = 1 min ( ⌊ n k ⌋ , ⌊ n k ⌋ ) ∑ i = 1 ⌊ n k ⌋ ∑ j = 1 ⌊ m k ⌋ μ ( d ) [ d ∣ gcd ( i , j ) ] f(n,m)=\sum\limits_{d=1}^{\min\left(\left\lfloor\frac{n}{k}\right\rfloor,\left\lfloor\frac{n}{k}\right\rfloor\right)}\sum\limits_{i=1}^{\left\lfloor\frac{n}{k}\right\rfloor}\sum\limits_{j=1}^{\left\lfloor\frac{m}{k}\right\rfloor}\mu(d)[d\mid\gcd(i,j)] f(n,m)=d=1∑min(⌊kn⌋,⌊kn⌋)i=1∑⌊kn⌋j=1∑⌊km⌋μ(d)[d∣gcd(i,j)];由于 μ ( d ) \mu(d) μ(d) 为只与 d d d 相关的函数,则 f ( n , m ) = ∑ d = 1 min ( ⌊ n k ⌋ , ⌊ n k ⌋ ) μ ( d ) ∑ i = 1 ⌊ n k ⌋ ∑ j = 1 ⌊ m k ⌋ [ d ∣ gcd ( i , j ) ] f(n,m)=\sum\limits_{d=1}^{\min\left(\left\lfloor\frac{n}{k}\right\rfloor,\left\lfloor\frac{n}{k}\right\rfloor\right)}\mu(d)\sum\limits_{i=1}^{\left\lfloor\frac{n}{k}\right\rfloor}\sum\limits_{j=1}^{\left\lfloor\frac{m}{k}\right\rfloor}[d\mid\gcd(i,j)] f(n,m)=d=1∑min(⌊kn⌋,⌊kn⌋)μ(d)i=1∑⌊kn⌋j=1∑⌊km⌋[d∣gcd(i,j)]。 有条件 d ∣ gcd ( i , j ) d\mid\gcd(i,j) d∣gcd(i,j),故可设 gcd ( i , j ) = p d \gcd(i,j)=pd gcd(i,j)=pd;由最大公约数的定义 p d ∣ i pd\mid i pd∣i 且 p d ∣ j pd\mid j pd∣j,因此假设 i = p 1 p d i=p_1pd i=p1pd, j = p 2 p d j=p_2pd j=p2pd;其中, p , p 1 , p 2 ∈ N ∗ p,p_1,p_2\in N^* p,p1,p2∈N∗。经过上述略证, d ∣ gcd ( i , j ) d\mid\gcd(i,j) d∣gcd(i,j) 的充要条件是: i , j i,j i,j 都为 d d d 的倍数。在区间 [ 1 , ⌊ n k ⌋ ] \left[1,\left\lfloor\frac{n}{k}\right\rfloor\right] [1,⌊kn⌋] 中, d d d 的倍数有 ⌊ ⌊ n k ⌋ d ⌋ \left\lfloor\frac{\left\lfloor\frac{n}{k}\right\rfloor}{d}\right\rfloor ⌊d⌊kn⌋⌋ 个;在区间 [ 1 , ⌊ m k ⌋ ] \left[1,\left\lfloor\frac{m}{k}\right\rfloor\right] [1,⌊km⌋] 中, d d d 的倍数有 ⌊ ⌊ m k ⌋ d ⌋ \left\lfloor\frac{\left\lfloor\frac{m}{k}\right\rfloor}{d}\right\rfloor ⌊d⌊km⌋⌋ 个;因此,满足条件 d ∣ gcd ( i , j ) d\mid\gcd(i,j) d∣gcd(i,j) 的数对 ( i , j ) (i,j) (i,j) 的个数有 ∑ i = 1 ⌊ n k ⌋ ∑ j = 1 ⌊ m k ⌋ [ d ∣ gcd ( i , j ) ] = ⌊ ⌊ n k ⌋ d ⌋ × ⌊ ⌊ m k ⌋ d ⌋ \sum\limits_{i=1}^{\left\lfloor\frac{n}{k}\right\rfloor}\sum\limits_{j=1}^{\left\lfloor\frac{m}{k}\right\rfloor}[d\mid\gcd(i,j)]=\left\lfloor\frac{\left\lfloor\frac{n}{k}\right\rfloor}{d}\right\rfloor\times\left\lfloor\frac{\left\lfloor\frac{m}{k}\right\rfloor}{d}\right\rfloor i=1∑⌊kn⌋j=1∑⌊km⌋[d∣gcd(i,j)]=⌊d⌊kn⌋⌋×⌊d⌊km⌋⌋。 将 ∑ i = 1 ⌊ n k ⌋ ∑ j = 1 ⌊ m k ⌋ [ d ∣ gcd ( i , j ) ] = ⌊ ⌊ n k ⌋ d ⌋ × ⌊ ⌊ m k ⌋ d ⌋ \sum\limits_{i=1}^{\left\lfloor\frac{n}{k}\right\rfloor}\sum\limits_{j=1}^{\left\lfloor\frac{m}{k}\right\rfloor}[d\mid\gcd(i,j)]=\left\lfloor\frac{\left\lfloor\frac{n}{k}\right\rfloor}{d}\right\rfloor\times\left\lfloor\frac{\left\lfloor\frac{m}{k}\right\rfloor}{d}\right\rfloor i=1∑⌊kn⌋j=1∑⌊km⌋[d∣gcd(i,j)]=⌊d⌊kn⌋⌋×⌊d⌊km⌋⌋ 代入,得 f ( n , m ) = ∑ d = 1 min ( ⌊ n k ⌋ , ⌊ n k ⌋ ) ⌊ ⌊ n k ⌋ d ⌋ ⌊ ⌊ m k ⌋ d ⌋ μ ( d ) f(n,m)=\sum\limits_{d=1}^{\min\left(\left\lfloor\frac{n}{k}\right\rfloor,\left\lfloor\frac{n}{k}\right\rfloor\right)}\left\lfloor\frac{\left\lfloor\frac{n}{k}\right\rfloor}{d}\right\rfloor\left\lfloor\frac{\left\lfloor\frac{m}{k}\right\rfloor}{d}\right\rfloor\mu(d) f(n,m)=d=1∑min(⌊kn⌋,⌊kn⌋)⌊d⌊kn⌋⌋⌊d⌊km⌋⌋μ(d)。该式可用数论分块解决。 结合以上 f ( n , m ) = ∑ i = 1 n ∑ j = 1 m [ gcd ( i , j ) = k ] = ∑ i = 1 n ∑ j = 1 m [ gcd ( i k , j k ) = 1 ] = ∑ i = 1 ⌊ n k ⌋ ∑ j = 1 ⌊ m k ⌋ [ gcd ( i , j ) = 1 ] = ∑ i = 1 ⌊ n k ⌋ ∑ j = 1 ⌊ m k ⌋ ∑ d ∣ gcd ( i , j ) μ ( d ) = ∑ d = 1 min ( ⌊ n k ⌋ , ⌊ n k ⌋ ) ∑ i = 1 ⌊ n k ⌋ ∑ j = 1 ⌊ m k ⌋ μ ( d ) [ d ∣ gcd ( i , j ) ] = ∑ d = 1 min ( ⌊ n k ⌋ , ⌊ n k ⌋ ) μ ( d ) ∑ i = 1 ⌊ n k ⌋ ∑ j = 1 ⌊ m k ⌋ [ d ∣ gcd ( i , j ) ] = ∑ d = 1 min ( ⌊ n k ⌋ , ⌊ n k ⌋ ) ⌊ ⌊ n k ⌋ d ⌋ ⌊ ⌊ m k ⌋ d ⌋ μ ( d ) \begin{aligned}f(n,m)&=\sum\limits_{i=1}^n\sum\limits_{j=1}^m [\gcd(i,j)=k]\\&=\sum\limits_{i=1}^n\sum\limits_{j=1}^m \left[\gcd\left(\frac{i}{k},\frac{j}{k}\right)=1\right]\\&=\sum\limits_{i=1}^{\left\lfloor\frac{n}{k}\right\rfloor}\sum\limits_{j=1}^{\left\lfloor\frac{m}{k}\right\rfloor}[\gcd(i,j)=1]\\&=\sum\limits_{i=1}^{\left\lfloor\frac{n}{k}\right\rfloor}\sum\limits_{j=1}^{\left\lfloor\frac{m}{k}\right\rfloor}\sum\limits_{d\mid\gcd(i,j)}\mu(d)\\&=\sum\limits_{d=1}^{\min\left(\left\lfloor\frac{n}{k}\right\rfloor,\left\lfloor\frac{n}{k}\right\rfloor\right)}\sum\limits_{i=1}^{\left\lfloor\frac{n}{k}\right\rfloor}\sum\limits_{j=1}^{\left\lfloor\frac{m}{k}\right\rfloor}\mu(d)[d\mid\gcd(i,j)]\\&=\sum\limits_{d=1}^{\min\left(\left\lfloor\frac{n}{k}\right\rfloor,\left\lfloor\frac{n}{k}\right\rfloor\right)}\mu(d)\sum\limits_{i=1}^{\left\lfloor\frac{n}{k}\right\rfloor}\sum\limits_{j=1}^{\left\lfloor\frac{m}{k}\right\rfloor}[d\mid\gcd(i,j)]\\&=\sum\limits_{d=1}^{\min\left(\left\lfloor\frac{n}{k}\right\rfloor,\left\lfloor\frac{n}{k}\right\rfloor\right)}\left\lfloor\frac{\left\lfloor\frac{n}{k}\right\rfloor}{d}\right\rfloor\left\lfloor\frac{\left\lfloor\frac{m}{k}\right\rfloor}{d}\right\rfloor\mu(d)\end{aligned} f(n,m)=i=1∑nj=1∑m[gcd(i,j)=k]=i=1∑nj=1∑m[gcd(ki,kj)=1]=i=1∑⌊kn⌋j=1∑⌊km⌋[gcd(i,j)=1]=i=1∑⌊kn⌋j=1∑⌊km⌋d∣gcd(i,j)∑μ(d)=d=1∑min(⌊kn⌋,⌊kn⌋)i=1∑⌊kn⌋j=1∑⌊km⌋μ(d)[d∣gcd(i,j)]=d=1∑min(⌊kn⌋,⌊kn⌋)μ(d)i=1∑⌊kn⌋j=1∑⌊km⌋[d∣gcd(i,j)]=d=1∑min(⌊kn⌋,⌊kn⌋)⌊d⌊kn⌋⌋⌊d⌊km⌋⌋μ(d) 综上所述,对于给定的 n , m n,m n,m 最终用数论分块求解 f ( n , m ) f(n,m) f(n,m) 的值 。假设在区间 [ l , r ] [l,r] [l,r] 内, ⌊ ⌊ n k ⌋ d ⌋ × ⌊ ⌊ m k ⌋ d ⌋ \left\lfloor\frac{\left\lfloor\frac{n}{k}\right\rfloor}{d}\right\rfloor\times\left\lfloor\frac{\left\lfloor\frac{m}{k}\right\rfloor}{d}\right\rfloor ⌊d⌊kn⌋⌋×⌊d⌊km⌋⌋ 恒为 A A A,则 ∑ d = l r ⌊ ⌊ n k ⌋ d ⌋ ⌊ ⌊ m k ⌋ d ⌋ μ ( d ) = A ∑ d = l r μ ( d ) \sum\limits_{d=l}^r\left\lfloor\frac{\left\lfloor\frac{n}{k}\right\rfloor}{d}\right\rfloor\left\lfloor\frac{\left\lfloor\frac{m}{k}\right\rfloor}{d}\right\rfloor\mu(d)=A\sum\limits_{d=l}^r\mu(d) d=l∑r⌊d⌊kn⌋⌋⌊d⌊km⌋⌋μ(d)=Ad=l∑rμ(d);显然,我们要预处理莫比乌斯函数的前缀和,才能在 O ( 1 ) O(1) O(1) 内求出一个分块的值。 题目的最终答案为 f ( b , d ) − f ( a − 1 , d ) − f ( b , c − 1 ) + f ( a − 1 , c − 1 ) f(b,d)-f(a-1,d)-f(b,c-1)+f(a-1,c-1) f(b,d)−f(a−1,d)−f(b,c−1)+f(a−1,c−1)。