BZOJ 3613: [Heoi2014]南园满地堆轻絮(二分)
题面:
https://www.lydsy.com/JudgeOnline/problem.php?id=3613
题解:
考虑前面的数越小答案越优秀,于是我们二分答案,判断时让前面的数达到所能达到的最小值,这样最优秀。。
代码:
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=5000010; int n; ll g[maxn],p,c,d,a,b,l,r,ans; bool pan(ll x){ ll mx=-0x3f3f3f3f; for(int i=1;i<=n;i++){ if(g[i]-x>mx) mx=g[i]-x; else if(g[i]+x<mx) return 0; } return 1; } ll f(ll x){ return (a*x%p*x%p*x%p+b*x%p*x%p+c*x%p+d)%p; } int main(){ scanf("%d%lld%lld%lld%lld%lld%lld",&n,&a,&b,&c,&d,&g[1],&p); for(int i=2;i<=n;i++) g[i]=(f(g[i-1])+f(g[i-2]))%p; l=0,r=1e9+7; while(l<=r){ ll mid=l+r>>1; if(pan(mid)) r=mid-1,ans=mid; else l=mid+1; } printf("%lld",ans); return 0; }