ACM集训题

Let me alone.


题目
gcd变化次数, https://ac.nowcoder.com/acm/contest/69791/F
线段树二分, 理解01排序,https://www.luogu.com.cn/problem/P2824
2023ccpc桂林C, 线性基, https://codeforces.com/gym/104768/problem/C
https://ac.nowcoder.com/acm/contest/95323/K
基环树 https://www.luogu.com.cn/problem/P1399
2023ccpc桂林I https://codeforces.com/gym/104768/problem/I
2023ccpc桂林H https://codeforces.com/gym/104768/problem/H
2023icpc桂林J https://codeforces.com/gym/104768/problem/J
牛客 基础算法班

GCD 的变化次数

注意到的性质:gcd(x, y) <= min(x,y) → gcd(x,y) <= x / 2

相当于 GCD 要不不变,要不变小 1/2 O(log(n))

  • 对于一个子序列的 最大公约数(GCD),可以认定这个 最大公约数的 可能性是可以枚举的。

    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
    #include<bits/stdc++.h>
    using namespace std;
    #define int long long
    const int N = 6e5 + 10;
    vector<pair<int, int>> pos[N];
    void build(vector<int>& a){
    int n = a.size() - 1;
    pos[1].push_back({1,a[1]});
    for(int i = 2; i <= n; i++){
    pos[i].push_back({i,a[i]});
    int t = a[i];
    for(auto [x,y] : pos[i - 1]){
    int now = gcd(t,y);
    if(t != now){
    pos[i].push_back({x,now});
    t = now;
    }
    }
    }
    }
    signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin >> n >> m;
    vector<int> a(n + 1);
    for(int i = 1; i <= n; i++) cin >> a[i];
    build(a);
    while(m --){
    int l, r;
    cin >> l >> r;
    int ans = 0;
    for(auto [x,y]: pos[r]){
    if(x >= l){
    ans += 1;
    }else break;
    }
    cout << ans << "\n";
    }
    }
作者

Jiely

发布于

2025-04-15

更新于

2025-04-17

许可协议

# 相关文章
  1.算法之美
  2.alogrithm
  3.老算法笔记
  4.PYTHON 算法学习
评论

:D 一言句子获取中...