codeforce 954 div3 G2题

思路:
质因子分解可以顺着分解,也可以逆着分解
即找到每一个数字的倍数,再找到每一个数字的因数

const int N = 5e5+10;
vector<int> ff[N];
vector<int> f[N];
vector<int> g[N];

void solve(){
    int n;
    cin>>n;
    vector<int> nums(n+1,0);
    ll ans = 0;
    for(int i = 1;i<=n;i++){
        cin>>nums[i];
    }
    for(int i = 1;i<=n;i++){
        int w = gcd(i,nums[i]);
        int x = i/w,y = nums[i]/w;
        //cout<<x<<" "<<y<<" eeeeee"<<endl;
        if(nums[i]%i == 0)  ans--;
        f[x].emplace_back(y);
        g[y].emplace_back(x);
    }
    vector<int>cnt(n+1,0);
    for(int i = 1;i<=n;i++){
        int m = f[i].size();
        if(m == 0)  continue;
        sort(f[i].begin(),f[i].end());
        for(int j = i;j<=n;j+=i){
            for(auto x : g[j]){
                cnt[x]++;
            }
        }
        for(int j = 0;j<m;j++){
            int k = 1;
            while(j<m-1 && f[i][j] == f[i][j+1]){
                j++;
                k++;
            }
            int y = f[i][j];
            ll sum = 0;
            for(auto son : ff[y]){
                sum += cnt[son];
            }
            sum *= k;
            ans += sum;
        }


        for(int j = i;j<=n;j+=i){
            for(auto x : g[j]){
                cnt[x]--;
            }
        }
    }
    for(int i = 1;i<=n;i++){
        f[i].clear();
        g[i].clear();
    }
    cout<<ans/2<<endl;
}

相关推荐

  1. codeforce 954 div3 G2

    2024-07-13 12:16:03       20 阅读
  2. Codeforces Round 924 (Div. 2)

    2024-07-13 12:16:03       50 阅读
  3. Codeforces Round 958 (Div. 2)

    2024-07-13 12:16:03       24 阅读
  4. Codeforces Round 950 (Div. 3)

    2024-07-13 12:16:03       23 阅读
  5. Codeforces Round 957 (Div. 3)

    2024-07-13 12:16:03       22 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-13 12:16:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-13 12:16:03       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-13 12:16:03       58 阅读
  4. Python语言-面向对象

    2024-07-13 12:16:03       69 阅读

热门阅读

  1. elk部署springboot

    2024-07-13 12:16:03       25 阅读
  2. 音频筑基:入门50问

    2024-07-13 12:16:03       27 阅读
  3. 永恒之蓝:一场网络风暴的启示

    2024-07-13 12:16:03       29 阅读
  4. TensorFlow系列:第三讲:MobileNetV2使用介绍

    2024-07-13 12:16:03       23 阅读
  5. MySQL上亿数据查询优化:实践与技巧

    2024-07-13 12:16:03       20 阅读
  6. Jetson-AGX-Orin gstreamer+rtmp+http-flv 推拉流

    2024-07-13 12:16:03       20 阅读
  7. Mybatis-Plus最优化持久层开发

    2024-07-13 12:16:03       18 阅读
  8. 389. 找不同

    2024-07-13 12:16:03       20 阅读
  9. Python水平怎么样才能就业?

    2024-07-13 12:16:03       49 阅读
  10. Git: fatal: cannot lock ref‘HEAD‘: Unable to create

    2024-07-13 12:16:03       25 阅读
  11. 大数据学习之 scala基础(补充)

    2024-07-13 12:16:03       24 阅读
  12. Perl中的切分艺术:深入探索split函数的神秘力量

    2024-07-13 12:16:03       22 阅读
  13. 【面试题】Golang 之Channel底层原理 (第三篇)

    2024-07-13 12:16:03       22 阅读
  14. 数据结构

    2024-07-13 12:16:03       26 阅读