题目大意
a1=t0
an=(A*an-1^2+B*an+C)%D(n>1)
求该序列最长不下降子序列长度
暴力
n不是很大显然可以暴力。
n很大呢?
那就不断减循环节长度直至减到一个阈值内,再暴力。
正确性显然,只要阈值不要设太小。
#include<cstdio>
#include<algorithm>
#include<cmath>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
typedef long long ll;
const int maxn=150+10,lim=1000000;
int a[1000000+500],b[maxn],d[1000000+500];
int wz[maxn];
int i,j,k,l,t,m,top,A,B,C,ans;
ll n,ti;
int main(){
freopen("lis.in","r",stdin);freopen("lis.out","w",stdout);
scanf("%lld",&n);
scanf("%d%d%d%d%d",&a[1],&A,&B,&C,&m);
//a[1]%=m;
top=1;
t=a[1]%m;
while (1){
t=(A*t*t+B*t+C)%m;
if (wz[t]){
l=wz[t];
break;
}
a[++top]=t;
wz[t]=top;
}
k=top-l+1;
if (n>lim){
ti=(n-lim)/k+1;
n-=(ll)k*ti;
}
if (n<=lim){
top=1;
t=a[1]%m;
fo(i,2,n){
t=(A*t*t+B*t+C)%m;
a[++top]=t;
//wz[t]=top;
}
/*k=top-l+1;
fo(i,l,top)
fo(j,1,m)
a[i+k*j]=a[i];
top=top+m*k;
top=int(min((ll)top,n));*/
fo(i,1,top) d[i]=200;
fo(i,1,top){
j=upper_bound(d,d+top+1,a[i])-d-1;
j++;
if (j>ans) ans=j;
d[j]=min(d[j],a[i]);
}
}
printf("%lld\n",(ll)ans+ti);
}