luogu#P5032. 经验

经验

Background

In-contest Q&A.

The simplified version has been updated, and the time limit has been changed to 500 ms.

Save up enough experience to enchant~~

Steve often runs into problems in Minecraft: He wants to repair nn enchanted books, and the level of each book is aia_i. He never really understands the anvil repair and experience mechanisms, so he searched for some information on the wiki:

—The figure shows the relationship between experience and level.

After seeing this figure, he thought: the higher my level is, the more experience I need. So if my level is just enough for anvil repairing, wouldn’t I spend the least experience? He continued searching and attached the anvil mechanism below, asking you to help him solve the problem.

Problem Description

Prior Work Penalty

Whether it is renaming, repairing, or combining, the experience cost increases due to the item’s previous operations on an anvil. This extra cost is called the “prior work penalty”. For an item that has never been put on an anvil, the prior work penalty is 00.

After each anvil operation on an item (excluding renaming), its prior work penalty is multiplied by 22 and then increased by 11. Therefore, after NN operations, the prior work penalty is 2N12^N-1. After 66 operations, the prior work penalty is 6363 levels, and in survival mode no further repairing or enchanting can be done. After 3131 operations, the penalty level is 21474836472147483647, and no operations can be done in any mode.

When combining two items, the player suffers the prior work penalties of both items at the same time. The prior work penalty of the combined item is calculated based on the higher one of the two items. For example, combining two items with prior work penalties of 33 and 1515 will add an extra penalty cost of 1818 levels, and the resulting item’s penalty is 3131 (15×2+115 \times 2+1).

The prior work penalty even applies to items that do not wear out, such as enchanted books. Therefore, combining 44 Fortune I enchanted books will produce a Fortune III enchanted book with a prior work penalty of 33.

Total operation count Penalty
00
11
22 33
33 77
44 1515
55 3131

Repairing items using the crafting grid removes all prior work penalties, but it also removes all enchantments.

Combining Items

In the anvil interface, the item in the first slot/on the left is called the target item; the item in the second slot/on the right is called the sacrifice item—it will disappear after combining. If the sacrifice item has enchantments, the anvil will also try to merge the sacrifice item’s enchantments into the target item. Regardless of whether the enchantments on the target item actually change, the anvil will charge the player a level cost based on the enchantments on both the target item and the sacrifice item.

For each enchantment on the sacrifice item, if the target item also has the same enchantment:

  • If the sacrifice item’s enchantment level is higher, the target item’s enchantment level will increase to the sacrifice item’s level.

  • If the sacrifice item’s enchantment level is the same, the target item’s enchantment level will increase by 11 level, unless it is already at the maximum level.

  • If the sacrifice item’s enchantment level is lower, the target item’s enchantment level does not change.

The total cost of combining items is the sum of the following costs:

  1. The sum of the target item’s and the sacrifice item’s prior work penalties.

  2. If renaming is also performed, an additional renaming cost is added.

  3. If the target item’s durability is not full, it costs 22 levels for repairing.

  4. If the sacrifice item has enchantments, an enchanting cost is added.

  5. If the sacrifice item is an enchanted book, there is no repairing cost. The anvil will attempt to merge the book’s enchantments into the target item. Renaming the target item can also be performed at the same time. In this case, the enchanting cost is usually less than the cost of combining two similar items.

—Adapted from mcwiki with minor edits.

Simplified Version

You are given enchanted books. Only books of the same level can be combined. The cost of combining is the sum of the two books’ accumulated costs. The accumulated cost of the resulting book becomes 22 times the maximum accumulated cost before combining, plus 11. Find the highest level and the minimum cost (the highest level is the primary objective). Steve is using cheats, so the highest level is unlimited.

Now you are given nn enchanted books, and each book has its level aia_i. Ask how to obtain the maximum level xx of an enchanted book. On this basis, compute the minimum level cost yy to combine it (we assume the initial prior work penalty of each book is 11).

Steve is lazy. He does not want to read the above text; he only wants you to write a program to compute xx and yy. But to prevent it from being spread, he only asks you to output kk, the multiplicative inverse of xx modulo yy. If it does not exist, output 1-1.

Input Format

The first line contains an integer nn.

In the next nn lines, each line contains an integer aia_i, representing the initial level of each enchanted book. The data is not guaranteed to be sorted.

Output Format

Output one integer kk, representing the multiplicative inverse of xx modulo yy. If it does not exist, output 1-1.

PS: The multiplicative inverse kk can also be understood as: kk is the smallest positive integer such that kx1(mody)kx \equiv 1 \pmod y.

5
1 1 2 3 4
-1
4
1 1 1 1
7

Hint

Sample Explanation

The first sample:

Combine two level 11 books: cost 22 experience, accumulated cost becomes 33;
Then combine two level 22 books: cost 3+1=43+1=4 experience, accumulated cost becomes 77;
Then combine two level 33 books: cost 7+1=87+1=8 experience, accumulated cost becomes 1515;
Finally combine two level 44 books: cost 15+1=1615+1=16 experience, accumulated cost becomes 3131.

Total experience cost: 2+4+8+16=302+4+8+16=30, maximum level: 55.

For the first sample: x=5,y=30x=5,y=30.

For the second sample: x=3,y=10x=3,y=10.

Constraints

The data is guaranteed to be random, and x,y,kx,y,k are within the range of long int.

Friendly Reminder

The input size of this problem is large, so please use a fast input method. Here is an example of fast input (requires including the header <cctype>):

#include<cctype>
inline void read(int &x){
     char ch=getchar();x=0;
     while(!isdigit(ch))   ch=getchar();
     while(isdigit(ch))   x=x*10+ch-'0',ch=getchar();
}

Translated by ChatGPT 5