[CQOI2017]小Q的棋盘
贪心走完最长链,然后剩下的两步可以走一个点
[CQOI2017]小Q的表格
发现 b ∗ f ( a , a + b ) = ( a + b ) ∗ f ( a , b ) b*f(a,a+b)=(a+b)*f(a,b) b∗f(a,a+b)=(a+b)∗f(a,b), 于是有 f ( a , a + b ) a + b = f ( a , b ) b \frac{f(a,a+b)}{a+b}=\frac{f(a,b)}{b} a+bf(a,a+b)=bf(a,b)
于是有 f ( a , a % b ) a ∗ ( a % b ) = f ( a , b ) a ∗ b = f ( a % b , a ) ( a % b ) ∗ a \frac{f(a,a\%b)}{a*(a\%b)}=\frac{f(a,b)}{a*b}=\frac{f(a\%b,a)}{(a\%b)*a} a∗(a%b)f(a,a%b)=a∗bf(a,b)=(a%b)∗af(a%b,a)
这与 gcd 辗转相除特别像
于是有 f ( a , b ) a ∗ b = f ( g c d ( a , b ) , g c d ( a , b ) ) g c d ( a , b ) 2 \frac{f(a,b)}{a*b}=\frac{f(gcd(a,b),gcd(a,b))}{gcd(a,b)^2} a∗bf(a,b)=gcd(a,b)2f(gcd(a,b),gcd(a,b))
a n s = ∑ i = 1 k ∑ j = 1 k f ( i , j ) ans=\sum_{i=1}^k\sum_{j=1}^k f(i,j) ans=i=1∑kj=1∑kf(i,j)
= ∑ d = 1 k f ( d , d ) d 2 ∑ i = 1 k ∑ j = 1 k i j [ g c d ( i , j ) = d ] =\sum_{d=1}^k \frac{f(d,d)}{d^2}\sum_{i=1}^k\sum_{j=1}^kij[gcd(i,j)=d] =d=1∑kd2f(d,d)i=1∑kj=1∑kij[gcd(i,j)=d]
= ∑ d = 1 k f ( d , d ) ∑ i = 1 k / d ∑ j = 1 k / d i j [ g c d ( i , j ) = 1 ] =\sum_{d=1}^k f(d,d)\sum_{i=1}^{k/d}\sum_{j=1}^{k/d}ij[gcd(i,j)=1] =d=1∑kf(d,d)i=1∑k/dj=1∑k/dij[gcd(i,j)=1]
考虑到 ∑ i = 1 n i [ g c d ( n , i ) = 1 ] = φ ( n ) ∗ n 2 \sum_{i=1}^n i[gcd(n,i)=1]=\frac{\varphi(n)*n}{2} ∑i=1ni[gcd(n,i)=1]=2φ(n)∗n,所以有
∑ i = 1 k / d ∑ j = 1 k / d i j [ g c d ( i , j ) = 1 ] = ∑ i = 1 k / d i 2 ∗ φ ( i ) \sum_{i=1}^{k/d}\sum_{j=1}^{k/d}ij[gcd(i,j)=1]=\sum_{i=1}^{k/d}i^2*\varphi(i) i=1∑k/dj=1∑k/dij[gcd(i,j)=1]=i=1∑k/di2∗φ(i)
可以预处理,然后需要求 f 的前缀和,需要查询 m ∗ n m*\sqrt n m∗n,修改 m m m次,于是用分块维护前缀和就可以了
[CQOI2017]老C的任务
主席树,树套树,CDQ随便选一个开心就好
[CQOI2017]老C的键盘
考虑dp, f i , j f_{i,j} fi,j 表示 i 的子树,它的排名为 j 的方案数,枚举当前排名
然后从自己子树选几个比它小,从其他子树选几个比它小,如果当前比子树大
f u , l = ∑ i + j = l ∑ k = 1 j f u , i ∗ f v , k ∗ ( i + j − 1 i − 1 ) ∗ ( s i z x + s i z y − l s i z x − i ) f_{u,l}=\sum_{i+j=l} \sum_{k=1}^j f_{u,i}*f_{v,k}*\binom{i+j-1}{i-1}*\binom{siz_x+siz_y-l}{siz_x-i} fu,l=∑i+j=l∑k=1jfu,i∗fv,k∗(i−1i+j−1)∗(sizx−isizx+sizy−l)
发现有一个东西可以用前缀和维护一下,比子树小的情况差不多