首页 > 其他分享 >CF1780F Codeforces Round 846 (Div. 2) F. Three Chairs

CF1780F Codeforces Round 846 (Div. 2) F. Three Chairs

时间:2023-03-07 20:13:17浏览次数:59  
标签:std 846 ch CF1780F res sum Three int include

https://codeforces.com/contest/1780/problem/F

计算

\[\sum_{i, j, k} [gcd(min(a_i, a_j, a_k), max(a_i, a_j, a_k)) = 1] \]

对 \(a_i\) 排序,则需计算

\[\sum_{i < k < j} [gcd(a_i, a_j) = 1] \]

将艾弗森括号展开成莫比乌斯函数形式,并枚举 \(k\)

\[\sum_k \sum_{i < k} \sum_{j > k} \sum_{d | a_i, d | a_j} \mu(d) \]

对于 \(k\),后面的可以 \(O(1)\) 转移,即每次把 \(k - 1\) 对后面的贡献加上,\(k\) 对前面的贡献减去

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
#include <cstring>
#include <functional>

#define i64 long long

const int mod = 998244353, inf = 0x3f3f3f3f;

inline int read() {
    bool sym = 0; int res = 0; char ch = getchar();
    while (!isdigit(ch)) sym |= (ch == '-'), ch = getchar();
    while (isdigit(ch)) res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar();
    return sym ? -res : res;
}

int main() {
    int n; std::cin >> n;
    std::vector<int> a(n + 1);
    for (int i = 1; i <= n; i++) std::cin >> a[i];
    std::sort(a.begin() + 1, a.end());
    const int m = a[n];
    std::vector<int> mu(m + 1);
    mu[1] = 1;
    for (int i = 1; i <= m; i++) {
        for (int j = 2 * i; j <= m; j += i) {
            mu[j] -= mu[i];
        }
    }
    std::vector<std::vector<int>> div(m + 1);
    for (int i = 1; i <= m; i++) {
        for (int j = i; j <= m; j += i) {
            div[j].push_back(i);
        }
    }
    i64 ans = 0, cur = 0;
    std::vector<int> cl(m + 1), cr(m + 1);
    for (int i = 2; i <= n; i++) for (auto d : div[a[i]]) cr[d]++;
    for (int i = 2; i <= n; i++) {
        for (auto d : div[a[i - 1]]) cur += mu[d] * cr[d], cl[d]++;
        for (auto d : div[a[i]]) cur -= mu[d] * cl[d], cr[d]--;
        ans += cur;
    }
    std::cout << ans << "\n";
    return 0;
}

标签:std,846,ch,CF1780F,res,sum,Three,int,include
From: https://www.cnblogs.com/zrzring/p/CF1780F.html

相关文章

  • Study for Go ! Chapter three - Function
    StudyforGo!Chapterthree-FunctionInitialization函数是结构化编程的最小模块单元函数是代码复用和测试的基本单元关键字func无需前置声明不......
  • Three.js使用webWorker进行八叉树构建
    更新:经过一番尝试发现了这种方式的局限模型太大构建的八叉树结构也非常大一个10万个点的模型构建的八叉树在控制台内存中居然有150M而主线程在接受大量数据的时候又产生......
  • Three.js使用WebWorker进行八叉树碰撞检测
    经过一番探索后还是采用了整个碰撞检测都交给worker来做​​原因​​如果是小的模型还是不需要这么做的js线程足够处理构建时的开销步骤将需要被检测的物体集合转换成可......
  • 题解 CF1406D【Three Sequences】
    看错题了,我很生气。problemYouaregivenasequenceof$n$integers$a_1,a_2,\ldots,a_n$.Youhavetoconstructtwosequencesofintegers$b$and$c......
  • Three.js实现高程数据加载
    通过加载高程数据(dem),显示地形高低起伏,达到良好的立体展示效果;Three.js能够通过设置一系列坐标点的高度,构建成面的形式,显示高程数据。实现方式:使用Three.js的PlaneGeometry进......
  • InstancedMesh threejs 批量重复使用相同的物体和材质
    代码<!DOCTYPEhtml><htmllang="en"> <head> <title>three.jswebgl-instancing-raycast</title> <metacharset="utf-8"/> <meta name="viewport" ......
  • 踩坑(二) --- threejs 踩坑之点击模型,获取点击位置,对应的世界坐标
      这是原型中的效果,需要在模型上添加自定义标签,那么步骤大致就是:点击模型,获取到对应位置的世界坐标,存入到数据库。主页展示的时候,先查出这些点,然后通过css2dObject......
  • thirty-two(模型点击展示)react-three-fiber
    模型点击蒙版展示点击展示目的(用户需要看见模型中更加多的内容信息)使用技术ThreeJs、React-three-fiber、React-three-drei、React、css整体思路:  1、在展示模型中......
  • threejs shader特效,分区辉光
    分区辉光有两种实现方式:1.单个图层两次渲染,先用带bloom的composer渲染一次,再正常渲染一次:https://github.com/mrdoob/three.js/blob/master/examples/webgl_postprocessin......
  • CF846F - Random Query
    题意:对于一个序列,每次随机选择两个数\(l,r\),如果\(l\gtr\)就交换,求\(l,r\)中本质不同的数个数的期望。我们发现,在所有的\(n^2\)个选择方案中,其实就是\(l<r\)的......