Codeforces Round #731 (Div. 3) F

5 篇文章 0 订阅
3 篇文章 0 订阅

F. Array Stabilization (GCD version)

本题大概题意是:
给你一个数组,每次变换为a[i]=gcd(a[i],a[i+1]),而且是一个环状的,即a[n]=gcd(a[n],a[0]),每次操作遍历一遍数组,
问经过多少次操作是得a[1]==a[2]==a[3]==....a[n].
思路是:
第一次操作后a[1]=gcd(a[1],a[2]),a[2]=gcd(a[2],a[3]),a[3]=gcd(a[3],a[4])...
第二次操作后 a[1]=gcd(a[1],gcd(a[2]),a[3]),a[2]=gcd(a[2],gcd(a[3],gcd(4)))
即 a[1]=gcd(a[1],a[2],a[3]),a[2]=gcd(a[2],a[3],a[4]);
所以每x-1次操作后 a[i]=gcd(a[i],a[i+1],...,a[i+x])
所以只要操作次数多,这个序列一定会变成有全相等,次数越少,越不可能,所以应该具有单调性,可以二分,至于查找的话,有RMQ和线段树两种查找方法。
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <vector>
#define ll long long int
#define ms(a, b) memset(a, b, sizeof(a))
#define lowbit(x) x & -x
#define fi first
#define ull unsigned long long
#define se second
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define endl "\n"
#define bug cout << "----acac----" << endl;
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
using namespace std;
const int maxn = 8e5 + 10;
const int maxm = 2e4 + 50;
const double eps = 1e-8;
const ll inf = 0x3f3f3f3f;
const ll lnf = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1);
const ll mod = 1e9 + 7;
int gcd(int a,int b)
{
    return b == 0 ? a : gcd(b, a % b);
}
int n, a[maxn];
struct node
{
    int l, r, w;
} t[maxn<<2];
void build(int rt,int l,int r)
{
    t[rt].l = l;
    t[rt].r = r;
    if(l==r)
    {
        t[rt].w = a[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(lson, l, mid);
    build(rson, mid + 1, r);
    t[rt].w=gcd(t[lson].w,t[rson].w);
}
int query(int rt,int l,int r)
{
    if(t[rt].l>=l&&t[rt].r<=r)
    {
        return t[rt].w;
    }
    int ans = 0;
    int mid = (t[rt].l + t[rt].r) >> 1;
    if(mid>=l)
        ans = gcd(ans, query(lson, l, r));
    if(mid<r)
        ans = gcd(ans, query(rson, l, r));
    return ans;
}
bool chekc(int x)
{
    map<int, int> p;
    for (int i = 1; i <= n;i++)
    {
        int l = i, r = i + x;
        p[query(1, l, r)]++;
        if(p.size()>=2)
            return false;
    }
    return true;
}
int main()
{
    IOS;
    int T;
    cin >> T;
    while(T--)
    {
        cin >> n;
        for (int i = 1; i <= n;i++)
        {
            cin >> a[i];
            a[i + n] = a[i];
        }
        build(1, 1, 2*n);
        int ans = -1;
        int l = 0, r = n - 1;
        while(r>=l)
        {
            int mid = (l + r) >> 1;
            if(chekc(mid))
            {
                ans = mid;
                r = mid - 1;
            }
            else
            {
                l = mid + 1;
            }
        }
        cout << ans << endl;
    }
    return 0;
}

RQM版本

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <vector>
#define ll long long int
#define ms(a, b) memset(a, b, sizeof(a))
#define lowbit(x) x & -x
#define fi first
#define se second
#define ull unsigned long long
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define endl "\n"
#define bug cout << "----acac----" << endl;
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
using namespace std;
const int maxn = 8e5 + 10;
const int maxm = 5e3 + 50;
const double eps = 1e-8;
const ll inf = 0x3f3f3f3f;
const ll lnf = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1);
const ll mod = 1e9 + 7;
int dp[maxn][40];
int a[maxn];
int gcd(int a,int b)
{
    return b == 0 ? a : gcd(b, a % b);
}
void RMQ(int n)
{
    for (int i = 1; i <= n; i++)
        dp[i][0] = a[i];
    for (int j = 1; (1 << j) <= n; j++)
    {
        for (int i = 1; i + (1 << j) - 1 <= n; i++)
        {
            dp[i][j] = gcd(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
        }
    } 
}
int n;
int query(int l,int r)
{
    int k = (int)(log(r - l + 1) / log(2.0));
    return gcd(dp[l][k], dp[r - (1 << k) + 1][k]);
}
bool chekc(int x)
{
    map<int, int> p;
    for (int i = 1; i <= n;i++)
    {
        int l = i, r = i + x;
        p[query(l, r)]++;
        if(p.size()>=2)
            return false;
    }
    return true;
}
int main()
{
    IOS;
    int T;
    cin >> T;
    while(T--)
    {
        cin >> n;
        for (int i = 1; i <= n;i++)
        {
            cin >> a[i];
            a[i + n] = a[i];
        }
        RMQ(2*n);
        int ans = -1;
        int l = 0, r = n - 1;
        while(r>=l)
        {
            int mid = (l + r) >> 1;
            if(chekc(mid))
            {
                ans = mid;
                r = mid - 1;
            }
            else
            {
                l = mid + 1;
            }
        }
        cout << ans << endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值