显示代码纯文本
#include <iostream>
#include <fstream>
using namespace std;
long long n,a,b,c,d,e,f,g,h,m;
long long i,j,k,l;
long long w[1500030][3];
void Sort(int x,int y)
{
int i,j;
long long k,l,p;
i=x;
j=y;
k=w[(x+y)/2][1];
l=w[(x+y)/2][2];
do
{
while (w[i][1]>k) i++;
while ((w[i][1]==k)and(w[i][2]<l)) i++;
while (w[j][1]<k) j--;
while ((w[j][1]==k)and(w[j][2]>l)) j--;
if (i<=j)
{
p=w[i][1];
w[i][1]=w[j][1];
w[j][1]=p;
p=w[i][2];
w[i][2]=w[j][2];
w[j][2]=p;
i++;
j--;
}
}while (i<=j);
if (i<y) Sort(i,y);
if (x<j) Sort(x,j);
}
int main()
{
ifstream fin("farmoff.in");
ofstream fout("farmoff.out");
fin>>n>>a>>b>>c>>d>>e>>f>>g>>h>>m;
for (i=0;i<3*n;i++)
{
w[i+1][1]=(e*((((((((i*i)%h)*i)%h)*i)%h)*i)%h))%h;
w[i+1][1]=(w[i+1][1]+(f*( (((i*i)%h))*i)%h )%h)%h;
w[i+1][1]=(w[i+1][1]+g)%h;
w[i+1][2]=(a*((((((((i*i)%d)*i)%d)*i)%d)*i)%d))%d;
w[i+1][2]=(w[i+1][2]+(b*( (i*i)%d ))%d)%d;
w[i+1][2]=(w[i+1][2]+c)%d;
}
Sort(1,3*n);
//for (i=0;i<3*n;i++)
// cout << w[i+1][2] << " " << w[i+1][1] << endl;
l=0;
for (i=1;i<=n;i++)
l=(l+w[i][2])%m;
fout<<l;
fin.close();
fout.close();
return 0;
}