洛谷P2522 [HAOI2011]Problem b

    技术2024-12-17  11

    欢迎访问个人博客

    传送门 ↬ \looparrowright

    题目描述

      对于给出的 n n n 个询问,每次求有多少个数对 ( x , y ) (x,y) (x,y),满足 a ≤ x ≤ b a \le x \le b axb c ≤ y ≤ d c \le y \le d cyd,且 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) 的个数。

    输入输出样例

    输入 #1

    2 2 5 1 5 1 1 5 1 5 2

    输出 #1

    14 3

    说明/提示

      对于 100 % 100\% 100% 的数据满足: 1 ≤ n , k ≤ 5 × 1 0 4 1 \le n,k \le 5 \times 10^4 1n,k5×104 1 ≤ a ≤ b ≤ 5 × 1 0 4 1 \le a \le b \le 5 \times 10^4 1ab5×104 1 ≤ c ≤ d ≤ 5 × 1 0 4 1 \le c \le d \le 5 \times 10^4 1cd5×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=1xj=1y[gcd(i,j)=k],其中 x , y ∈ N ∗ x,y\in N^* x,yN。由题意,所求答案为 ∑ 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=abj=cd[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(a1,d)f(b,c1)+f(a1,c1)。   任务转化为,对于给定的 n , m ∈ N ∗ n,m\in N^* n,mN,求 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=1nj=1m[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=1nj=1m[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=1knj=1km[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)=1dgcd(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=1knj=1kmdgcd(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=1min(kn,kn)i=1knj=1kmμ(d)[dgcd(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=1min(kn,kn)μ(d)i=1knj=1km[dgcd(i,j)]。   有条件 d ∣ gcd ⁡ ( i , j ) d\mid\gcd(i,j) dgcd(i,j),故可设 gcd ⁡ ( i , j ) = p d \gcd(i,j)=pd gcd(i,j)=pd;由最大公约数的定义 p d ∣ i pd\mid i pdi p d ∣ j pd\mid j pdj,因此假设 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,p2N。经过上述略证, d ∣ gcd ⁡ ( i , j ) d\mid\gcd(i,j) dgcd(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 dkn 个;在区间 [ 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 dkm 个;因此,满足条件 d ∣ gcd ⁡ ( i , j ) d\mid\gcd(i,j) dgcd(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=1knj=1km[dgcd(i,j)]=dkn×dkm。   将 ∑ 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=1knj=1km[dgcd(i,j)]=dkn×dkm 代入,得 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=1min(kn,kn)dkndkmμ(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=1nj=1m[gcd(i,j)=k]=i=1nj=1m[gcd(ki,kj)=1]=i=1knj=1km[gcd(i,j)=1]=i=1knj=1kmdgcd(i,j)μ(d)=d=1min(kn,kn)i=1knj=1kmμ(d)[dgcd(i,j)]=d=1min(kn,kn)μ(d)i=1knj=1km[dgcd(i,j)]=d=1min(kn,kn)dkndkmμ(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 dkn×dkm 恒为 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=lrdkndkmμ(d)=Ad=lrμ(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(a1,d)f(b,c1)+f(a1,c1)

    代码

    #include<iostream> #include<cstring> #include<algorithm> #define ll long long #define N 50006 using namespace std; bool vis[N]; int prime[N>>1];//质数 int mu[N];//莫比乌斯函数 int sum[N];//mu[i]的前缀和 int cnt; int a,b,c,d,k; //-------------------------------------------- //欧拉筛求得莫比乌斯函数 //求莫比乌斯函数的前缀和 void init(int n) { mu[1]=1; int i,j; for(i=2;i<=n;i++) { if(!vis[i]) { prime[++cnt]=i; mu[i]=-1; } for(j=1;j<=cnt&&i*prime[j]<=n;j++) { vis[i*prime[j]]=1; if(i%prime[j]==0) { mu[i*prime[j]]=0; break; } else mu[i*prime[j]]=-mu[i]; } } for(i=1;i<=n;i++) sum[i]=sum[i-1]+mu[i]; } //-------------------------------------------- //-------------------------------------------------- //对于给定的n,m //计算f(n,m) //将n/k,m/k看作整体 inline ll f(int n,int m) { n/=k; m/=k; if(n>m) swap(n,m); int l,r; ll res=0; for(l=1;l<=n;l=r+1) { r=min(n/(n/l),m/(m/l)); res=res+1ll*(n/l)*(m/l)*(sum[r]-sum[l-1]); } return res; } //-------------------------------------------------- int main() { init(50000); int _; for(cin>>_;_;_--) { scanf("%d%d%d%d%d",&a,&b,&c,&d,&k); //容斥原理 printf("%lld\n",f(b,d)-f(a-1,d)-f(b,c-1)+f(a-1,c-1)); } return 0; }
    Processed: 0.053, SQL: 9