CF264B Good Sequences

LII.CF264B Good Sequences

状态很显然。设\(f[i]\)表示位置\(i\)的最长长度。

关键是转移——暴力转移是\(O(n^2)\)的。我们必须找到一个更优秀的转移。

因为一个数的质因子数量是\(O(\log n)\)的,而只有和这个数具有相同质因子的数是可以转移的;

因此我们可以对于每个质数\(p\),设一个\(mx_p\)表示所有有\(p\)作为质因子的\(x\)\(f_i\)的最大值。

关于质因子应该怎么得出嘛……线性筛一下即可。

复杂度\(O(n\log n)\)

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=1e5;
int n,pri[N+10],pre[N+10],mx[N+10],f[N+10],res;
void ural(){
	for(int i=2;i<=N;i++){
		if(!pri[i])pri[++pri[0]]=i,pre[i]=pri[0];
		for(int j=1;j<=pri[0]&&i*pri[j]<=N;j++){
			pri[i*pri[j]]=true,pre[i*pri[j]]=j;
			if(!(i%pri[j]))break;
		}
	}
}
int main(){
	scanf("%d",&n),ural();
	for(int i=1,x,t;i<=n;i++){
		scanf("%d",&x),f[i]=1;
		t=x;
		while(t!=1)f[i]=max(f[i],mx[pre[t]]+1),t/=pri[pre[t]];
		t=x;
		while(t!=1)mx[pre[t]]=f[i],t/=pri[pre[t]];
		res=max(res,f[i]);
	}
	printf("%d\n",res);
	return 0;
}

原文地址:https://www.cnblogs.com/Troverld/p/14597406.html