## IOI '15 P4 - Horses

View as PDF

Points: 15 (partial)
Time limit: 1.0s
Memory limit: 256M

Problem type
Allowed languages
C, C++

Mansur loves to breed horses, just like his ancient ancestors did. He now has the largest herd in Kazakhstan. But this was not always the case. years ago, Mansur was just a dzhigit (Kazakh for a young man) and he only had a single horse. He dreamed to make a lot of money and to finally become a bai (Kazakh for a very rich person).

Let us number the years from to in chronological order (i.e., year is the most recent one). The weather of each year influenced the growth of the herd. For each year , Mansur remembers a positive integer growth coefficient . If you started year with horses, you ended the year with horses in your herd.

Horses could only be sold at the end of a year. For each year , Mansur remembers a positive integer : the price for which he could sell a horse at the end of year . After each year, it was possible to sell arbitrarily many horses, each at the same price .

Mansur wonders what is the largest amount of money he could have now if he had chosen the best moments to sell his horses during the years. You have the honor of being a guest on Mansur's toi (Kazakh for holiday), and he asked you to answer this question.

Mansur's memory improves throughout the evening, and so he makes a sequence of updates. Each update will change either one of the values or one of the values . After each update he again asks you the largest amount of money he could have earned by selling his horses. Mansur's updates are cumulative: each of your answers should take into account all of the previous updates. Note that a single or may be updated multiple times.

The actual answers to Mansur's questions can be huge. In order to avoid working with large numbers, you are only required to report the answers modulo .

#### Example

Suppose that there are years, with the following information:

012
X213
Y341

For these initial values, Mansur can earn the most if he sells both his horses at the end of year 1. The entire process will look as follows:

• Initially, Mansur has 1 horse.
• After year 0 he will have horses.
• After year 1 he will have horses.
• He can now sell those two horses. The total profit will be .

Then, suppose that there is update: changing to .

After the update we will have:

012
X213
Y321

In this case, one of the optimal solutions is to sell one horse after year 0 and then three horses after year 2. The entire process will look as follows:

• Initially, Mansur has 1 horse.
• After year 0 he will have horses.
• He can now sell one of those horses for , and have one horse left.
• After year 1 he will have horse.
• After year 2 he will have horses.
• He can now sell those three horses for . The total amount of money is .

You are given , , , and the list of updates. Before the first update, and after every update, compute the maximal amount of money that Mansur could get for his horses, modulo . You need to implement the functions init, updateX, and updateY.

int init(int N, int X[], int Y[]);

• The grader will call this function first and exactly once.
• N: the number of years.
• X: an array of length . For , gives the growth coefficient for year .
• Y: an array of length . For , gives the price of a horse after year .
• Note that both X and Y specify the initial values given by Mansur (before any updates).
• After init terminates, the arrays X and Y remain valid, and you may modify their contents if you wish.
• The function should return the maximal amount of money Mansur could get for these initial values of and , modulo .
int updateX(int pos, int val);

• pos: an integer from the range .
• val: the new value for .
• The function should return the maximal amount of money Mansur could get after this update, modulo .
int updateY(int pos, int val);

• pos: an integer from the range .
• val: the new value for .
• The function should return the maximal amount of money Mansur could get after this update, modulo .

You may assume that all the initial, as well as updated values of and are between and inclusive.

After calling init, the grader will call updateX and updateY an integer number of times. The total number of calls to updateX and updateY will be .

3 20 and for init and updateX respectively