把黑边视为无边,那么答案之和每个点的度数有关
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n,a,b,c,p,d,tot,cnt,deg[5005];
signed main() {
cin>>n>>a>>b>>c>>p>>d;
for(int i=1;i<=n;i++) {
for(int j=i+1;j<=n;j++) {
++tot;
if((a*(i+j)%p*(i+j)%p+b*(i-j)%p*(i-j)%p+c)%p>d) ++deg[i],++deg[j];
}
}
for(int i=1;i<=n;i++) cnt+=deg[i]*(n-deg[i]-1);
cout<<n*(n-1)*(n-2)/6-cnt/2<<endl;
}