# [BZOJ2018]&&[Usaco2009 Nov] 农场技艺大赛

## 描述 Description

$W_{I}=(aI^{5}+bI^{2}+c) mod d \ U_{I}=(eI^{5}+fI^{3}+g) mod h$

## 样例输入 SampleInput

222500 23637582 79462751 57479383 82737269 91983785 89849583 36429858 95372257 17222153

15270656

Silver

BZOJ 2018

## 代码 Code

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int i,j,t;
long long n,m,l,r,k,z,y,x;
const int maxn=500005;
struct cow
{
long long w,u;
bool operator < (const cow& temp) const
{
return (u!=temp.u)?(u>temp.u):(w<temp.w);
}
}co[3*maxn];
long long a,b,c,d,e,f,g,h,ans;
inline void make_data()
{
long long i,j;
long long t[6];
for (i=0;i<n*3;i++)
{
for (t[1]=i%d,j=2;j<=5;j++) t[j]=(i*t[j-1])%d;
co[i].w=((a*t[5])%d+(b*t[2])%d+c%d)%d;
for (t[1]=i%h,j=2;j<=5;j++) t[j]=(i*t[j-1])%h;
co[i].u=((e*t[5])%h+(f*t[3])%h+g%h)%h;
}
}
int main()
{
scanf("%lld%lld%lld%lld%lld%lld%lld%lld%lld%lld",&n,&a,&b,&c,&d,&e,&f,&g,&h,&m);
make_data();
sort(co,co+3*n);
ans=0;
for (i=0;i<n;i++) ans=(ans+co[i].w)%m;
printf("%lld\n",ans);
return 0;
}