zoukankan      html  css  js  c++  java
  • CF1538F.Array Stabilization (GCD version)(二分+ST表或线段树)

    传送门

    思路:

    经过(x次)变化后
    (b_{1}=gcd(a_{1},a_{2},……,a_{x}))
    也就是说最多(n)次操作后,数组中的数就会相等。
    答案具有单调性,如果(mid)次操作可以使得数组中的数相等,那么说明大于(mid)次的操作也一定可以。
    二分变化的次数,(check)的时候只需要判断变化后的数是否相等,根据上面的规律可以得知,问题转化成了求区间(gcd)
    没有修改,可以写(ST)表。

    代码:

    ST表代码:

    // Problem: F. Array Stabilization (GCD version)
    // Contest: Codeforces - Codeforces Round #731 (Div. 3)
    // URL: https://codeforces.com/contest/1547/problem/F
    // Memory Limit: 512 MB
    // Time Limit: 4000 ms
    // 
    // Powered by CP Editor (https://cpeditor.org)
    #pragma GCC optimize(2)
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<ll, ll>PLL;
    typedef pair<int, int>PII;
    typedef pair<double, double>PDD;
    #define I_int ll
    inline ll read()
    {
        ll x = 0, f = 1;
        char ch = getchar();
        while(ch < '0' || ch > '9')
        {
            if(ch == '-')f = -1;
            ch = getchar();
        }
        while(ch >= '0' && ch <= '9')
        {
            x = x * 10 + ch - '0';
            ch = getchar();
        }
        return x * f;
    }
    #define read read()
    #define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
    #define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
    #define rep(i,a,b) for(int i=(a);i<=(b);i++)
    #define repp(i,a,b) for(int i=(a);i<(b);i++)
    #define per(i,a,b) for(int i=(a);i>=(b);i--)
    #define perr(i,a,b) for(int i=(a);i>(b);i--)
    ll ksm(ll a, ll b, ll p)
    {
        ll res = 1;
        while(b)
        {
            if(b & 1)res = res * a % p;
            a = a * a % p;
            b >>= 1;
        }
        return res;
    }
    const int inf = 0x3f3f3f3f;
    #define PI acos(-1)
    
    const int maxn=4e5+100;
    
    int n,a[maxn],dp[maxn][18];
    
    void init(){
    	for(int j=0;j<18;j++){
    		for(int i=1;i+(1<<j)-1<=2*n;i++){
    			if(j==0) dp[i][j]=a[i];
    			else{
    				dp[i][j]=__gcd(dp[i][j-1],dp[i+(1<<j-1)][j-1]);
    			}
    		}
    	}
    }
    
    int query(int l,int r){
    	ll len=r-l+1;
        ll k=log(len)/log(2);
        return __gcd(dp[l][k],dp[r-(1<<k)+1][k]);
    }
    
    bool check(int x){
    	int las=query(1,1+x);
    	rep(i,2,n){
    		if(query(i,i+x)!=las) return 0;
    	}
    	return 1;
    }
    
    int main(){
    	int _=read;
    	while(_--){
    		n=read;
    		rep(i,1,n){
    			a[i]=read;
    			a[i+n]=a[i];
    		}
    		init();
    		int l=0,r=n,res;
    		while(l<=r){
    			int mid=(l+r)/2;
    			if(check(mid)) r=mid-1,res=mid;
    			else l=mid+1;
    		}
    		cout<<res<<endl;
    	}
    	return 0;
    }
    
    
     
    
  • 相关阅读:
    oracle的安装与plsql的环境配置
    Working with MSDTC
    soapui-java.lang.Exception Failed to load url
    Oracle 一个owner访问另一个owner的table,不加owner
    Call API relation to TLS 1.2
    Call API HTTP header Authorization: Basic
    VS2008 .csproj cannot be opened.The project type is not supported by this installat
    The changes couldn't be completed.Please reboot your computer and try again.
    Create DB Table View Procedure
    DB Change
  • 原文地址:https://www.cnblogs.com/OvOq/p/14999589.html
Copyright © 2011-2022 走看看