D. New Year Concert

New Year is just around the corner, which means that in School 179, preparations for the concert are in full swing.

There are nn classes in the school, numbered from 11 to nn, the ii-th class has prepared a scene of length aiai minutes.

As the main one responsible for holding the concert, Idnar knows that if a concert has kk scenes of lengths b1b1, b2b2, ……, bkbk minutes, then the audience will get bored if there exist two integers ll and rr such that 1≤l≤r≤k1≤l≤r≤k and gcd(bl,bl+1,…,br−1,br)=r−l+1gcd(bl,bl+1,…,br−1,br)=r−l+1, where gcd(bl,bl+1,…,br−1,br)gcd(bl,bl+1,…,br−1,br) is equal to the greatest common divisor (GCD) of the numbers blbl, bl+1bl+1, ……, br−1br−1, brbr.

To avoid boring the audience, Idnar can ask any number of times (possibly zero) for the tt-th class (1≤t≤k1≤t≤k) to make a new scene dd minutes in length, where dd can be any positive integer. Thus, after this operation, btbt is equal to dd. Note that tt and dd can be different for each operation.

For a sequence of scene lengths b1b1, b2b2, ……, bkbk, let f(b)f(b) be the minimum number of classes Idnar has to ask to change their scene if he wants to avoid boring the audience.

Idnar hasn't decided which scenes will be allowed for the concert, so he wants to know the value of ff for each non-empty prefix of aa. In other words, Idnar wants to know the values of f(a1)f(a1), f(a1f(a1,a2)a2), ……, f(a1f(a1,a2a2,……,an)an).

Input

The first line contains a single integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of classes in the school.

The second line contains nn positive integers a1a1, a2a2, ……, anan (1≤ai≤1091≤ai≤109) — the lengths of the class scenes.

Output

Print a sequence of nn integers in a single line — f(a1)f(a1), f(a1f(a1,a2)a2), ……, f(a1f(a1,a2a2,……,an)an).

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+10;
int a[N],n;
struct node
{
	int l,r;
	int g;
}tr[N*4];

int gcd(int a,int b)
{
	return b?gcd(b,a%b):a;
}

void pushup(int u)
{
	tr[u].g=gcd(tr[u<<1].g,tr[u<<1|1].g);
}

void build(int u,int l,int r)
{
	if(l==r) tr[u]={l,r,a[l]};
	else 
	{
		tr[u]={l,r};
		int mid=l+r>>1;
		build(u<<1,l,mid);
		build(u<<1|1,mid+1,r);
		
		pushup(u);
	}
}

int query(int u,int l,int r)
{
	if(tr[u].r<=r&&tr[u].l>=l) return tr[u].g;
	else
	{
		int mid=tr[u].l+tr[u].r>>1;
		int ans1=0,ans2=0;
		if(mid>=l) ans1=query(u<<1,l,r);
		if(mid<r) ans2=query(u<<1|1,l,r);
		
		return gcd(ans1,ans2);
	}
}

int main() 
{
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	build(1,1,n);
	
	int maxz=-1;
	int sta=1;
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		while(sta<i&&query(1,sta,i)<i-sta+1) sta++;//寻找gcd==i-sta+1 区间变大 gcd变小 i-sta+1变大  gcd大时增大区间 小时减小区间 
		int d=query(1,sta,i);
		if(d==i-sta+1)
		{
			if(sta>maxz)
			{
				ans++;
				maxz=i;//在i的位置改变成大素数 
			}
		}
	//	cout<<sta<<" "<<i<<" "<<d<<endl;
		cout<<ans<<endl;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值