[USACO09NOV] The Grand Farm-off S

题目描述

Farmer John owns 3\*N (1 <= N <= 500,000) cows surprisingly numbered 0..3\*N-1, each of which has some associated integer weight W\_i (1 <= W\_i <= d). He is entering the Grand Farm-off, a farming competition where he shows off his cows to the greater agricultural community. This competition allows him to enter a group of N cows. He has given each of his cows a utility rating U\_i (1 <= U\_i <= h), which represents the usefulness he thinks that a particular cow will have in the competition, and he wants his selection of cows to have the maximal sum of utility. There might be multiple sets of N cows that attain the maximum utility sum. FJ is afraid the competition may impose a total weight limit on the cows in the competition, so a secondary priority is to bring lighter weight competition cows. Help FJ find a set of N cows with minimum possible total weight among the sets of N cows that maximize the utility, and print the remainder when this total weight is divided by M (10,000,000 <= M <= 1,000,000,000). Note: to make the input phase faster, FJ has derived polynomials which will generate the weights and utility values for each cow. For each cow 0 <= i < 3\*N, $$W_i=(a\times i^5+b\times i^2+c)\mod d$$ $$U_i=(e\times i^5+f\times i^3+g)\mod h$$ $$(0\le a,b,c,d,e,f,g,h\le 10^9)$$ The formulae do sometimes generate duplicate numbers; your algorithm should handle this properly. 农夫约翰有3N(1 < N < 500000)头牛,编号依次为1..#N,每头牛都有一个整数值的体 重。约翰准备参加农场技艺大赛,向广大的农业社区展示他的奶牛. 大赛规则允许约翰带N头牛参赛.约翰给每头牛赋予了一个“有用度”Ui,它表 示了某头牛在比赛中的有用程度.约翰希望他选出的奶牛的有用度之和最大. 有可能选出很多组的N头牛都能达到有用度最大和.约翰害怕选出的N头牛的总重量会给大赛 带来震撼,所以,要考虑优先选择体重轻的奶牛. 帮助约翰选出N头总重量最轻,并且有用度之和最大的奶牛.输出体重模M后的余数. 注意:为了使输入更快,约翰使用了一个多项式来生成每头牛的体重和有用度.对每头牛/, 体重和有用度的计算公式为: $$W_i=(a\times i^5+b\times i^2+c)\mod d$$ $$U_i=(e\times i^5+f\times i^3+g)\mod h$$ $$(0\le a,b,c,d,e,f,g,h\le 10^9)$$

输入输出格式

输入格式


\* Line 1: Ten space-separated integers: N, a, b, c, d, e, f, g, h, and M

输出格式


\* Line 1: A single integer representing the lowest sum of the weights of the N cows with the highest net utility.

输入输出样例

输入样例 #1

2 0 1 5 55555555 0 1 0 55555555 55555555 

输出样例 #1

51 

说明

The functions generate weights of 5, 6, 9, 14, 21, and 30 along with utilities of 0, 1, 8, 27, 64, and 125. The two cows with the highest utility are cow 5 and 6, and their combined weight is 21+30=51.