bzoj3613: [Heoi2014]南园满地堆轻絮 二分
这道题二分一下就可以了
虽然数据看起来过不了,但还是能过的
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
inline int read(){
int x=0,f=1,ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-'){f=-1;}ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,sa,sb,sc,sd,mod;
inline int calc(int x){
long long res=0;
res+=(long long)sa*x%mod*x%mod*x%mod;
res+=(long long)sb*x%mod*x%mod;
res+=(long long)sc*x%mod;
res+=sd;
return res%mod;
}
int a[5000005];
inline bool ok(int x){
int id=a[1]-x,i,l,r;
for(i=2;i<=n;i++){
l=a[i]-x;r=a[i]+x;
if(id>r) return false;
id=max(id,l);
}
return true;
}
int main(){
n=read(),sa=read(),sb=read(),sc=read(),sd=read(),a[1]=read(),mod=read();
a[1]%=mod;sa%=mod,sb%=mod,sc%=mod,sd%=mod;int i;
for(i=2;i<=n;i++) a[i]=(calc(a[i-1])+calc(a[i-2]))%mod;
int l=0,r=mod-1,mid;
while(l<r){
mid=(l+r)>>1;
if(ok(mid)) r=mid;
else l=mid+1;
}
cout<<l<<endl;
return 0;
}

浙公网安备 33010602011771号