[HEOI2014]南园满地堆轻絮
BZOJ
luogu
二分答案贪心check
首先b[1]最小一定优
之后就贪心的最小化b[i]就行
#include<bits/stdc++.h>
using namespace std;
const int _=5e6+5;
int n,sa,sb,sc,sd,p,ans,a[_],b[_];
int F(int x){return (((1ll*sa*x%p*x%p*x%p+1ll*sb*x%p*x%p)%p+1ll*sc*x%p)%p+sd)%p;}
bool check(int k){
for(int i=1;i<=n;i++)b[i]=a[i];
b[1]=max(1,b[1]-k);
for(int i=2;i<=n;i++){
if(b[i]+k<b[i-1])return 0;
if(b[i]<=b[i-1])b[i]=b[i-1];
else b[i]=max(b[i-1],b[i]-k);
}
return 1;
}
int main(){
cin>>n>>sa>>sb>>sc>>sd>>a[1]>>p;
for(int i=2;i<=n;i++)a[i]=(F(a[i-1])+F(a[i-2]))%p;
int l=0,r=p;
while(l<=r){
int mid=(l+r)>>1;
if(check(mid))r=mid-1,ans=mid;
else l=mid+1;
}
cout<<ans<<endl;
return 0;
}