最长不下降子序列

39 篇文章 0 订阅
23 篇文章 0 订阅

题目大意

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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值