显示代码纯文本
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
struct Node
{
long long w,u;
}sum[1500010];
int N,A,B,C,D,E,F,G,H,M;
long long ans;
bool operator <(Node a,Node b)
{
if(a.u>b.u||a.u==b.u&&a.w<b.w)
return true;
else
return false;
}
long long pw(int a,int b,int c)
{
long long re=1;
while(b)
{
re=(re*a)%c;
b--;
}
return re;
}
int main()
{
freopen("farmoff.in","r",stdin);
freopen("farmoff.out","w",stdout);
scanf("%d%d%d%d%d%d%d%d%d%d",&N,&A,&B,&C,&D,&E,&F,&G,&H,&M);
for(int i=1;i<=3*N;i++)
{
sum[i].w=(A%D*pw(i-1,5,D)%D+B%D*pw(i-1,2,D)%D+C%D)%D;
sum[i].u=(E%H*pw(i-1,5,H)%H+F%H*pw(i-1,3,H)%H+G%H)%H;
}
sort(sum+1,sum+1+3*N);
for(int i=1;i<=N;i++)
{
ans+=sum[i].w;
ans%=M;
}
printf("%d\n",ans);
return 0;
}