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;
}