题解 传送门
其实只要找差距最大的逆序对就好了
答案就是此逆序对的差 /2
代码 (代码很短)
#include<bits/stdc++.h>
using namespace std;
#define re register
#define ll long long
#define get getchar()
#define in inline
in int read()
{
int t=0,x=1; char ch=get;
while ((ch<'0' || ch>'9') && ch!='-') ch=get;
if(ch=='-') ch=get,x=-1;
while (ch<='9' && ch>='0') t=t*10+ch-'0', ch=get;
return t*x;
}
int n,sa,sb,sc,sd,mod;
in ll F(ll x)
{
return ((((((sa*x%mod)*x%mod)*x%mod)+((sb*x%mod)*x%mod))%mod+(sc*x%mod))%mod+(sd%mod))%mod;//不要在意这些丑陋的细节,生活总是美好的
}
int main()
{
ll a0=0,a1,ans=0,a2,maxx;
n=read(),sa=read(),sb=read(),sc=read(),sd=read(),a1=read(),mod=read();
maxx=a1;
for(re int i=2;i<=n;i++)
{
a2=(F(a0)+F(a1))%mod;
maxx=max(maxx,a2);
ans=max(ans,(maxx-a2+1)>>1);
a0=a1,a1=a2;
}
cout<<ans;
}