analyze:
It takes a long time to construct the data, you can use Qin Jiushao to optimize it.
Dichotomous answer + greedy, that is: each b[i] is as small as possible and meets the meaning of the question. During the enumeration process, determine whether there is a b[i-1]>a[i]+x
If it exists, look to the right
If it doesn't exist, look left
Attached code:
#include <cstdio> #include <algorithm> #include <cmath> #include <cstring> #include <cstdlib> #include <iostream> #include <queue> //#include <ctime> using namespace std; #define N 5000005 #define ll long long int a [N], f [N], b [N], n, Sa, Sb, Sc, Sd, mod; int F(int x) { if(f[x]) return f[x]; return f[x]=((1ll*Sa*a[x]%mod+Sb)*a[x]*a[x]%mod+(1ll*Sc*a[x]%mod+Sd)%mod)%mod; } int check(int x) { b[0]=-1<<30; for(int i=1;i<=n;i++) { if(a[i]>b[i-1]+x) { b[i]=a[i]-x; }else { if(a[i]+x<b[i-1])return 0; b[i]=b[i-1]; } } return 1; } intmain() { a[0]=0; scanf("%d%d%d%d%d%d%d",&n,&Sa,&Sb,&Sc,&Sd,&a[1],&mod); for(int i=2;i<=n;i++)a[i]=(F(i-1)+F(i-2))%mod; int l=0,r=1<<30; while(l<r) { int m=(l+r)>>1; if(check(m)) r=m; else l=m+1; } printf("%d\n",l); return 0; }
Stuck a constant (you need to tap a constant on loj, bzoj and Luogu don’t need it, they run fast with O2)