题解:CF2006C Eri and Expanded Sets
CF2006C Eri and Expanded Sets
对于最终的序列,有以下的性质
-
最终序列中相邻两个数的差值一定是奇数,易得
-
序列必为等差数列,证明:
用反证法,假设最终序列不为等差数列,令其中三个相邻的数之间的差为
d_1 与d_2 ,则当d_1 \ne d_2 时,由于d_1+d_2 一定是偶数,则必能产生新的数字。
由于对于两个数
所以对于最终合法的序列(有多种数的情况下),我们要使其公差是
对于一段序列
所以我们可以使用尺取法。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=4e5+5;
int T;
int n,a[N],g[N][25];
void init(){
for(int i=1;i<n;i++)
g[i][0]=abs(a[i+1]-a[i]);
for(int j=1;j<=20;j++)
{
for(int i=1;i+(1<<j)<=n;i++){
int nxt=i+(1<<(j-1));
g[i][j]=__gcd(g[i][j-1],g[nxt][j-1]);
}
}
}
int find(int x,int y){
int k=log2(y-x);
return __gcd(g[x][k],g[(y-(1<<k))][k]);
}
bool check(int x){
if(x==0) return false;
int k=log2(x);
if((1<<k)==x) return true;
return false;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>T;
while(T--)
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
init();
/*
计算只有一个数
*/
ll ans=0,cnt=1;
for(int i=2;i<=n;i++)
{
if(a[i]!=a[i-1]){
ans+=(cnt+1)*cnt/2;
cnt=0;
}
cnt++;
}
ans+=(cnt+1)*cnt/2;
/*
计算 gcd 为 2 的幂
具有传递性可以用尺取法
*/
int now=2;
for(int i=1;i<=n;i++)
{
now=max(now,i+1);
while(check(find(i,now))==false&&now<=n) now++;
ans+=(n-now+1);
}
cout<<ans<<"\n";
}
return 0;
}
/*
*/