CF1780F Three Chairs
Posted dygxczn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF1780F Three Chairs相关的知识,希望对你有一定的参考价值。
题目描述
One day Kira found n n n friends from Morioh and decided to gather them around a table to have a peaceful conversation. The height of friend i i i is equal to a i a_i ai . It so happened that the height of each of the friends is unique.
Unfortunately, there were only 3 3 3 chairs in Kira’s house, and obviously, it will not be possible to seat all friends! So, Kira has to invite only 3 3 3 of his friends.
But everything is not so simple! If the heights of the lowest and the tallest of the invited friends are not coprime, then the friends will play tricks on each other, which will greatly anger Kira.
Kira became interested, how many ways are there to choose 3 3 3 of his friends so that they don’t play tricks? Two ways are considered different if there is a friend invited in one way, but not in the other.
Formally, if Kira invites friends i i i , j j j , and k k k , then the following should be true: gcd ( min ( a i , a j , a k ) , max ( a i , a j , a k ) ) = 1 \\gcd(\\min(a_i, a_j, a_k), \\max(a_i, a_j, a_k)) = 1 gcd(min(ai,aj,ak),max(ai,aj,ak))=1 , where gcd ( x , y ) \\gcd(x, y) gcd(x,y) denotes the greatest common divisor (GCD) of the numbers x x x and y y y .
Kira is not very strong in computer science, so he asks you to count the number of ways to invide friends.
题意简述
给一个长度为 n n n 的数组 a a a,数字两两不同。求有多少个三元组 ( a i , a j , a k ) (a_i,a_j,a_k) (ai,aj,ak),满足 i < j < k i<j<k i<j<k 且 gcd ( a i , a k ) = 1 \\gcd(a_i,a_k)=1 gcd(ai,ak)=1。
题解
首先给 a a a 数组从小到大排序,容易想到枚举 i , k i,k i,k,若 gcd ( a i , a k ) = 1 \\gcd(a_i,a_k)=1 gcd(ai,ak)=1,则对答案有 ( k − i − 1 ) (k-i-1) (k−i−1) 的贡献。
由此可得 a n s = ∑ k = 1 n ∑ i = 1 k − 1 [ gcd ( a i , a k ) = 1 ] ( k − i − 1 ) ans=\\sum\\limits_k=1^n\\sum\\limits_i=1^k-1\\left[\\gcd(a_i,a_k)=1\\right](k-i-1) ans=k=1∑ni=1∑k−1[gcd(ai,ak)=1](k−i−1)
根据莫比乌斯函数的性质 ∑ d ∣ n μ ( d ) = [ n = 1 ] \\sum\\limits_d\\mid n\\mu(d)=[n=1] d∣n∑μ(d)=[n=1],有 ∑ k = 1 n ∑ i = 1 k − 1 ∑ d ∣ gcd ( a i , a k ) μ ( d ) ( k − i − 1 ) \\sum\\limits_k=1^n\\sum\\limits_i=1^k-1\\sum\\limits_d\\mid\\gcd(a_i,a_k)\\mu(d)(k-i-1) k=1∑ni=1∑k−1d∣gcd(ai,ak)∑μ(d)(k−i−1)
因为 d ∣ gcd ( a i , a k ) d\\mid\\gcd(a_i,a_k) d∣gcd(ai,ak),所以 d ∣ a i ∧ d ∣ a k d\\mid a_i\\land d\\mid a_k d∣ai∧d∣ak,二者是等价的。
先枚举 d d d,有 ∑ d = 1 a n μ ( d ) ∑ d ∣ a k ∑ d ∣ a i ∧ i < k ( k − i − 1 ) \\sum\\limits_d=1^a_n\\mu(d)\\sum\\limits_d\\mid a_k\\sum\\limits_d\\mid a_i\\land i<k(k-i-1) d=1∑anμ(d)d∣ak∑d∣ai∧i<k∑(k−i−1)
为了方便,继续化简式子
∑ d = 1 a n μ ( d ) ∑ d ∣ a k ( n u m ⋅ ( k − 1 ) − ∑ d ∣ a i ∧ i < k i ) \\sum\\limits_d=1^a_n\\mu(d)\\sum\\limits_d\\mid a_k\\left(num\\cdot(k-1)-\\sum\\limits_d\\mid a_i\\land i<ki\\right) d=1∑anμ(d)d∣ak∑ num⋅(k−1)−d∣ai∧i<k∑i
n u m num num 表示满足 d ∣ a i ∧ i < k d\\mid a_i\\land i<k d∣ai∧i<k 的数量。
观察这个式子, μ \\mu μ 可以 以上是关于CF1780F Three Chairs的主要内容,如果未能解决你的问题,请参考以下文章