题目链接
一、题意
一个长度为n的数列a,他想问你该数列中满足条件(区间内存在某个数是区间内所有数的公因数)的最长区间有多少个。
数据范围:
二、题解
发现把 这个区间分割为了若干个不相交的区间。
对于分割后的区间,每个数都是区间最小值的倍数。
时间复杂度:
三、代码
#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)x.size()
#define cl(x) x.clear()
#define all(x) x.begin() , x.end()
#define rep(i , x , n) for(int i = x ; i <= n ; i ++)
#define per(i , n , x) for(int i = n ; i >= x ; i --)
#define mem0(x) memset(x , 0 , sizeof(x))
#define mem_1(x) memset(x , -1 , sizeof(x))
#define mem_inf(x) memset(x , 0x3f , sizeof(x))
#define debug(x) cerr << #x << " = " << x << '\n'
#define ddebug(x , y) cerr << #x << " = " << x << " " << #y << " = " << y << '\n'
#define ios std::ios::sync_with_stdio(false) , cin.tie(0)
using namespace std ;
typedef long long ll ;
typedef long double ld ;
typedef pair<int , int> pii ;
typedef pair<ll , ll> pll ;
const int mod = 998244353 ;
const int maxn = 2e6 + 10 ;
const int inf = 0x3f3f3f3f ;
const double eps = 1e-6 ;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()) ;
int n , a[maxn] ;
int max1 = 0 , cnt = 0 ;
int main()
{
ios ;
cin >> n ;
rep(i , 1 , n) cin >> a[i] ;
rep(i , 1 , n)
{
int j = i ;
int min1 = a[i] ;
while(j + 1 <= n)
{
if(a[j + 1] > min1 && a[j + 1] % min1 != 0) break ;
if(a[j + 1] < min1 && min1 % a[j + 1] != 0) break ;
min1 = min(min1 , a[j + 1]) ;
j ++ ;
}
if(j - i + 1 > max1) max1 = j - i + 1 , cnt = 1 ;
else if(j - i + 1 == max1) cnt ++ ;
i = j ;
}
cout << cnt << '\n' ;
return 0 ;
}