题目链接
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 5000010;
#define int long long
int n,sa,sb,sc,sd,a1,mod;
int a[N]={0},b[N];
int f(int x)
{
return sa*x%mod*x%mod*x%mod+sb*x%mod*x%mod+sc*x%mod+sd;
}
bool check(int x)
{
b[1]=max(0ll,a[1]-x);
for(int i=2;i<=n;i++)
{
b[i]=max(b[i-1],a[i]-x);
if(abs(a[i]-b[i])>x)return false;
}
return true;
}
signed main()
{
cin>>n>>sa>>sb>>sc>>sd>>a[1]>>mod;
int l=0,r=a[1];
for(int i=2;i<=n;i++)a[i]=(f(a[i-1]%mod)%mod+f(a[i-2]%mod)%mod)%mod,r=max(a[i],r);
while(l<r)
{
int mid=l+r>>1;
if(check(mid))r=mid;
else l=mid+1;
}
cout<<l<<endl;
}