Codeforces Round #731 (Div. 3) F. Array Stabilization (GCD version)

Posted TURNINING

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #731 (Div. 3) F. Array Stabilization (GCD version)相关的知识,希望对你有一定的参考价值。

传送门

题意:给你含有n个元素的数组,每一次操作把ai变为gcd(ai, ai+1)。问最少需要多少次操作可以令数组的所有元素相等。

思路:首先我们可以反应过来最后相等的元素就是数组所有元素的gcd,当我们进行k次操作时,ai = gcd(ai, ai+1, ai+2,…,ai+k)。同时我们发现答案是线性的,可以二分,问题是怎么快速得到k操作后每个元素的值。线段树或者ST表就行了,关于ST表有一篇不错的文章。传送门

哦对了这种成环的数组可以化环为链,长度为2*n,比较好维护一些。

线段树版本

#include<bits/stdc++.h>
using namespace std;
 
#define lsn (u << 1)
#define rsn (u << 1 | 1)
 
typedef long long ll;
typedef unsigned long long ull;
 
typedef pair<int, int> PI;
typedef pair<double, double> PD;
 
const int MAXN = 4e5 + 10;
const int MAX_LEN = 100000 + 10;
const int MAX_LOG_V = 22;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-7;
const ull B = 100000007;
 
 
int n, g;
int arr[MAXN];
 
int gcd(int a, int b) {
    if(b == 0) return a;
    return gcd(b, a % b);
};
 
struct Node {
    int l, r, g;
}tr[4*MAXN];
 
void pushup(int u) {
    tr[u].g = gcd(tr[lsn].g, tr[rsn].g);
}
 
void build(int u, int l, int r) {
    tr[u] = {l, r, 0};
    if(l == r) { tr[u].g = arr[l]; return ; }
    int mid = l + r >> 1;
    build(u << 1, l, mid);
    build(u << 1 | 1, mid + 1, r);
    pushup(u);
}
 
int query(int u, int l, int r) {
    if(tr[u].l >= l && tr[u].r <= r) return tr[u].g;
    else {
        int mid = tr[u].l + tr[u].r >> 1;
        if(r <= mid) return query(lsn, l, r);
        else if(l > mid) return query(rsn, l, r);
        int t1 = query(lsn, l, r);
        int t2 = query(rsn, l, r);
        return gcd(t1, t2);
    }
}
 
bool check(int t) {
    for(int i = 0; i < n; i++) {
        int l = i, r = t + i;
        if(query(1, l, r) != g) return false; 
    }
    return true;
}
 
void solve() {
    scanf("%d", &n);
    for(int i = 0; i < n; i++) { scanf("%d", &arr[i]); arr[i+n] = arr[i]; }
    build(1, 0, 2 * n - 1);
    g = arr[0];
    for(int i = 1; i < n; i++) g = gcd(arr[i], g);
    int l = 0, r = n;
    while(l < r) {
        int mid = (l + r) / 2;
        if(check(mid)) r = mid;
        else l = mid + 1;
    }
    printf("%d\\n", r);
} 
 
 
int main() {
    //ios::sync_with_stdio(false);
    int t = 1; scanf("%d", &t);
    while (t--) {
        solve();
    }
    return 0;
}

ST表版本
显然ST表比线段树好写

#include<bits/stdc++.h>
using namespace std;
 
#define lsn (u << 1)
#define rsn (u << 1 | 1)
 
typedef long long ll;
typedef unsigned long long ull;
 
typedef pair<int, int> PI;
typedef pair<double, double> PD;
 
const int MAXN = 4e5 + 10;
const int MAX_LEN = 100000 + 10;
const int MAX_LOG_V = 22;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-7;
const ull B = 100000007;
 
 
int n, g;
int Log[MAXN], f[MAXN][21]; 
 
int query(int l, int r) {
    int s = Log[r - l + 1];
    return __gcd(f[l][s], f[r-(1 << s)+1][s]);
}
 
bool check(int t) {
    for(int i = 1; i <= n; i++) {
        if(query(i, i + t) != g) return false;
    }
    return true;
}
 
void solve() {
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) {
        scanf("%d", &f[i][0]);
        f[i+n][0] = f[i][0];
    }
    g = f[1][0];
    for(int i = 1; i <= n; i++) g = __gcd(g, f[i][0]);
    for(int i = 1; i <= 20; i++) {
        for(int j = 1; j + (1 << i) - 1 <= 2*n; j++) {
            f[j][i] = __gcd(f[j][i - 1], f[j + (1 << (i - 1))][i - 1]);
        }
    }
    int l = 0, r = n;
    while(l < r) {
        int mid = l + r >> 1;
        if(check(mid)) r = mid;
        else l = mid + 1;
    }
    printf("%d\\n", r);
} 
 
 
int main() {
    //ios::sync_with_stdio(false);
    for(int i =  2; i < MAXN; i++) {
        Log[i] = Log[i/2] + 1;
    }
    int t = 1; scanf("%d", &t);
    while (t--) {
        solve();
    }
    return 0;
}

以上是关于Codeforces Round #731 (Div. 3) F. Array Stabilization (GCD version)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #731 (Div. 3)

Codeforces Round #731 (Div. 3) E题解

Codeforces Round #731 (Div. 3) B. Alphabetical Strings

Codeforces Round #731 (Div. 3) E. Air Conditioners

Codeforces Round #731 (Div. 3) F. Array Stabilization (GCD version)

Codeforces Round #731 (Div. 3) F. Array Stabilization (GCD version)