This page is still under construction.

Parts of this page are still being built. What you see may change.

Rabbit Lunch

Time limit8sMemory limit512 MB

Summary
Given counts of M carrot kinds and N kiwi kinds generated by recurrences, find the maximum number of distinct (carrot kind, kiwi kind) pairs a rabbit can take, one carrot and one kiwi each.
Level

Medium7 of 10

Topics
Greedy, Sorting, Math, Implementation
Solved
No attempts yet

Problem

A rabbit eats one carrot and one kiwi for lunch. Rabbits are very distinctive, so there must not be two different rabbits that eat the same kind of carrot and the same kind of kiwi.

There are MM kinds of carrots. There are mim_i carrots of the ii-th kind. There are NN kinds of kiwis. There are nin_i kiwis of the ii-th kind. Find the maximum number of rabbits that can eat lunch.

Generate mim_i and nin_i using the following recurrences.

  • m0=m0m_0 = m0
  • mi+1=(mi∗58+md)m_{i+1} = (m_i * 58 + md ) mod (N+1)(N + 1)
  • n0=n0n_0 = n0
  • ni+1=(ni∗58+nd)n_{i+1} = (n_i * 58 + nd ) mod (M+1)(M + 1)

Input

The input is given in the following format:

MM NN m0m0 mdmd n0n0 ndnd

Output

Print one integer on a single line: the maximum number of rabbits that can eat lunch.

Constraints

  • MM will be between 1 and 2,500,000, inclusive.
  • NN will be between 1 and 2,500,000, inclusive.
  • m0m0 and mdmd will be between 0 and NN, inclusive.
  • n0n0 and ndnd will be between 0 and MM, inclusive.

Examples2

  1. Example 1

    Input
    2 3 1 3 1 0
    
    Expected output
    2
    
  2. Example 2

    Input
    5 8 1 2 3 4
    
    Expected output
    19