• 【luogu 1457】在表格里造序列(莫反)(杜教筛)


    在表格里造序列

    题目链接:luogu 1457

    题目大意

    有一个 n*n 的表格,每个位置有一些数列,其中 i 行 j 列里面的数列是所有满足长度为 m,值域是 1~n,且所有值的 gcd 与 i,j 的 gcd 相同的数组。
    问你表格里总共有多少个数组。

    思路

    害,世界上最好的还得是莫反。 _{_{_{\color{black}害,世界上最好的还得是莫反。}}} 害,世界上最好的还得是莫反。
    当其它所有的题都在用奇奇怪怪的方式恶心你的时候 _{_{_{\color{black}当其它所有的题都在用奇奇怪怪的方式恶心你的时候}}} 当其它所有的题都在用奇奇怪怪的方式恶心你的时候
    只有莫反,纯纯的推式子,纯纯的,多好啊。 _{_{_{\color{black}只有莫反,纯纯的推式子,纯纯的,多好啊。}}} 只有莫反,纯纯的推式子,纯纯的,多好啊。
    ( _{_{_{\color{black}(}}}
    还有捏嘛 c s p 怎么不考数学(算了考了我也不会,还是别考了吧 _{_{_{\color{black}还有捏嘛csp怎么不考数学(算了考了我也不会,还是别考了吧}}} 还有捏嘛csp怎么不考数学(算了考了我也不会,还是别考了吧


    考虑枚举 gcd ⁡ \gcd gcd 的值,再分别枚举由多少个数组以及多少个表格上的位置满足条件。
    ∑ d = 1 n ( ∑ x = 1 n ∑ y = 1 n [ gcd ⁡ ( x , y ) = d ] ) ( ∑ a 1 = 1 n . . . ∑ a m = 1 n [ gcd ⁡ ( a 1 , . . . , a m = d ) ] ) \sum\limits_{d=1}^n(\sum\limits_{x=1}^n\sum\limits_{y=1}^n[\gcd(x,y)=d])(\sum\limits_{a_1=1}^n...\sum\limits_{a_m=1}^n[\gcd(a_1,...,a_m=d)]) d=1n(x=1ny=1n[gcd(x,y)=d])(a1=1n...am=1n[gcd(a1,...,am=d)])
    ∑ d = 1 n ( ∑ x = 1 ⌊ n d ⌋ ∑ y = 1 ⌊ n d ⌋ [ gcd ⁡ ( x , y ) = 1 ] ) ( ∑ a 1 = 1 ⌊ n d ⌋ . . . ∑ a m = 1 ⌊ n d ⌋ [ gcd ⁡ ( a 1 , . . . , a m = 1 ) ] ) \sum\limits_{d=1}^n(\sum\limits_{x=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum\limits_{y=1}^{\left\lfloor\frac{n}{d}\right\rfloor}[\gcd(x,y)=1])(\sum\limits_{a_1=1}^{\left\lfloor\frac{n}{d}\right\rfloor}...\sum\limits_{a_m=1}^{\left\lfloor\frac{n}{d}\right\rfloor}[\gcd(a_1,...,a_m=1)]) d=1n(x=1dny=1dn[gcd(x,y)=1])(a1=1dn...am=1dn[gcd(a1,...,am=1)])
    ∑ d = 1 n ( ∑ x = 1 ⌊ n d ⌋ ∑ y = 1 ⌊ n d ⌋ ∑ p ∣ gcd ⁡ ( x , y ) μ ( p ) ) ( ∑ a 1 = 1 ⌊ n d ⌋ . . . ∑ a m = 1 ⌊ n d ⌋ ∑ p ∣ gcd ⁡ ( a 1 , . . . , a m ) μ ( p ) ) \sum\limits_{d=1}^n(\sum\limits_{x=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum\limits_{y=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum\limits_{p|\gcd(x,y)}\mu(p))(\sum\limits_{a_1=1}^{\left\lfloor\frac{n}{d}\right\rfloor}...\sum\limits_{a_m=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum\limits_{p|\gcd(a_1,...,a_m)}\mu(p)) d=1n(x=1dny=1dnpgcd(x,y)μ(p))(a1=1dn...am=1dnpgcd(a1,...,am)μ(p))
    ∑ d = 1 n ( ∑ p = 1 ⌊ n d ⌋ μ ( p ) ∑ x = 1 ⌊ n d p ⌋ ∑ y = 1 ⌊ n d p ⌋ ) ( ∑ p = 1 ⌊ n d ⌋ μ ( p ) ∑ a 1 = 1 ⌊ n d p ⌋ . . . ∑ a m = 1 ⌊ n d p ⌋ ) \sum\limits_{d=1}^n(\sum\limits_{p=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\mu(p)\sum\limits_{x=1}^{\left\lfloor\frac{n}{dp}\right\rfloor}\sum\limits_{y=1}^{\left\lfloor\frac{n}{dp}\right\rfloor})(\sum\limits_{p=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\mu(p)\sum\limits_{a_1=1}^{\left\lfloor\frac{n}{dp}\right\rfloor}...\sum\limits_{a_m=1}^{\left\lfloor\frac{n}{dp}\right\rfloor}) d=1n(p=1dnμ(p)x=1dpny=1dpn)(p=1dnμ(p)a1=1dpn...am=1dpn)
    ∑ d = 1 n ( ∑ p = 1 ⌊ n d ⌋ μ ( p ) ( ⌊ n d p ⌋ ) 2 ) ( ∑ p = 1 ⌊ n d ⌋ μ ( p ) ( ⌊ n d p ⌋ ) m ) \sum\limits_{d=1}^n(\sum\limits_{p=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\mu(p)(\left\lfloor\frac{n}{dp}\right\rfloor)^2)(\sum\limits_{p=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\mu(p)(\left\lfloor\frac{n}{dp}\right\rfloor)^m) d=1n(p=1dnμ(p)(dpn)2)(p=1dnμ(p)(dpn)m)

    ∑ d = 1 n ∑ p = 1 ⌊ n d ⌋ μ ( p ) ( ⌊ n d p ⌋ ) m = n m \sum\limits_{d=1}^n\sum\limits_{p=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\mu(p)(\left\lfloor\frac{n}{dp}\right\rfloor)^m=n^m d=1np=1dnμ(p)(dpn)m=nm
    ∑ p = 1 n μ ( p ) ( ⌊ n p ⌋ ) m = n m − ∑ d = 2 n ∑ p = 1 ⌊ n d ⌋ μ ( p ) ( ⌊ n d p ⌋ ) m \sum\limits_{p=1}^n\mu(p)(\left\lfloor\frac{n}{p}\right\rfloor)^m=n^m-\sum\limits_{d=2}^n\sum\limits_{p=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\mu(p)(\left\lfloor\frac{n}{dp}\right\rfloor)^m p=1nμ(p)(pn)m=nmd=2np=1dnμ(p)(dpn)m
    ∑ p = 1 n μ ( p ) ( ⌊ n p ⌋ ) m = f n \sum\limits_{p=1}^n\mu(p)(\left\lfloor\frac{n}{p}\right\rfloor)^m=f_n p=1nμ(p)(pn)m=fn

    f n = n m − ∑ d = 2 n f ⌊ n d ⌋ f_n=n^m-\sum\limits_{d=2}^nf_{\left\lfloor\frac{n}{d}\right\rfloor} fn=nmd=2nfdn

    ∑ d = 1 n ( ∑ p = 1 ⌊ n d ⌋ μ ( p ) ( ⌊ n d p ⌋ ) 2 ) ( f ⌊ n d ⌋ ) \sum\limits_{d=1}^n(\sum\limits_{p=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\mu(p)(\left\lfloor\frac{n}{dp}\right\rfloor)^2)(f_{\left\lfloor\frac{n}{d}\right\rfloor}) d=1n(p=1dnμ(p)(dpn)2)(fdn)

    其中 f f f 可以记忆化一下在常规整除分块做到 O ( n 3 4 ) O(n^{\frac{3}{4}}) O(n43),左边的一坨式子,都可以用杜教筛快速 rush μ \mu μ 前缀和也是 O ( n 3 4 ) O(n^{\frac{3}{4}}) O(n43) 从而通过题目捏。

    代码

    #include
    #include
    #define ll long long
    #define mo 998244353
    
    using namespace std;
    
    const int Lim = 1e6;
    const int B = 2e5 + 100;
    ll n, m, lim, mus[Lim + 100];
    ll rem_f[B], rem_mu[B], prime[Lim + 100];
    bool np[Lim + 100];
    
    int id(ll x) {
    	if (x <= lim) return x; return lim + n / x;
    }
    
    ll ksm(ll x, ll y) {
    	ll re = 1;
    	while (y) {
    		if (y & 1) re = re * x % mo;
    		x = x * x % mo; y >>= 1;
    	}
    	return re;
    }
    
    ll get_f(ll now) {
    	if (rem_f[id(now)] != -1) return rem_f[id(now)];
    	ll re = ksm(now, m);
    	for (ll L = 2, R; L <= now; L = R + 1) {
    		R = now / (now / L);
    		(re += mo - (R - L + 1) % mo * get_f(now / L) % mo) %= mo;
    	}
    	return rem_f[id(now)] = re;
    }
    
    ll ask(ll now) {
    	if (now <= Lim) return (mus[now] % mo + mo) % mo;
    	if (rem_mu[id(now)] != -1) return rem_mu[id(now)];
    	ll re = 1;
    	for (int L = 2, R; L <= now; L = R + 1) {
    		R = now / (now / L);
    		(re += mo - (R - L + 1) * ask(now / L) % mo) %= mo;
    	}
    	return rem_mu[id(now)] = re;
    }
    
    ll slove(ll n) {
    	ll ans = 0;
    	for (ll L = 1, R; L <= n; L = R + 1) {
    		R = n / (n / L);
    		ll mus = (ask(R) - ask(L - 1) + mo) % mo;
    		(ans += mus * (n / L) % mo * (n / L) % mo) %= mo;
    	}
    	return ans;
    }
    
    int main() {
    	for (int i = 0; i < B; i++) rem_f[i] = rem_mu[i] = -1;
    	mus[1] = 1;
    	for (int i = 2; i <= Lim; i++) {
    		if (!np[i]) mus[i] = -1, prime[++prime[0]] = i;
    		for (int j = 1; j <= prime[0] && i * prime[j] <= Lim; j++) {
    			np[i * prime[j]] = 1;
    			if (i % prime[j] == 0) break;
    				else mus[i * prime[j]] = -mus[i];
    		}
    		mus[i] += mus[i - 1];
    	}
    	
    	scanf("%lld %lld", &n, &m); lim = sqrt(n);
    	
    	ll ans = 0;
    	for (ll L = 1, R; L <= n; L = R + 1) {
    		R = n / (n / L);
    		(ans += (R - L + 1) % mo * slove(n / L) % mo * get_f(n / L) % mo) %= mo;
    	}
    	printf("%lld", ans);
    	
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
  • 相关阅读:
    主流分布式存储技术对比分析:GFS、HDFS、GlusterFS、Ceph、Swift
    Go 限流器使用
    【Laravel系列7.8】广播系统
    文本检测及识别小组周报
    基于Hadoop的学习行为数据云存储平台的设计与实现
    KMP 算法 + 详细笔记
    一文跑通Yolovx_ByteTrack并实现行人追踪
    OS2.3.2:进程互斥的软件实现方法
    el-table中给每行添加loading效果案例
    机器学习从入门到放弃:硬train一发手写数字识别
  • 原文地址:https://blog.csdn.net/weixin_43346722/article/details/127573423