Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Problem F:
RabbitLunch

うさぎは昼食ににんじんとキウイを1 個ずつ食べる. うさぎはとても個性的なので, 食べるにんじんの種類もキウイの種類も同じであるような, 異なる2 匹のうさぎが存在してはならない.

にんじんは $M$ 種類ある. $i$ 種類目のにんじんは $m_i$ 個ある. キウイは $N$ 種類ある. $i$ 種類目のキウイは $n_i$ 個ある. 最大何匹のうさぎが昼食をとれるか求めよ.

$m_i$ と $n_i$ は次の漸化式を用いて生成せよ.

$m_0 = m0$

$m_{i+1} = (m_i * 58 + md )$ mod $(N + 1)$

$n_0 = n0$

$n_{i+1} = (n_i * 58 + nd )$ mod $(M + 1)$

Constraints

$M$ will be between 1 and 2,500,000, inclusive.

$N$ will be between 1 and 2,500,000, inclusive.

$m0$ and $md$ will be between 0 and $N$, inclusive.

$n0$ and $nd$ will be between 0 and $M$, inclusive.

Input

入力は以下の形式で与えられる:

$M$ $N$ $m0$ $md$ $n0$ $nd$

Output

昼食をとれるうさぎの匹数の最大値を表す整数を 1 行に出力せよ.

Sample Input 1

2 3 1 3 1 0

Sample Output 1

2

Sample Input 2

5 8 1 2 3 4

Sample Output 2

19

Solution in Ruby (judge verdict: Runtime Error)

class SegmentTree
	def initialize(arr, l = 0, r = arr.size - 1)
		@l = l
		@r = r
		if l == r
			# @val = arr[l]
		else
			c = (l + r) / 2
			@left_child = self.class.new(arr, l, c)
			@right_child = self.class.new(arr, c+1, r)
		end
		@min = arr[l]
		@max = arr[r]
	end

	def query(i, accum_lazy = 0)
		accum_lazy += (@lazy || 0)
		return @min - accum_lazy if @l == @r
		if i <= @left_child.r
			return @left_child.query(i, accum_lazy)
		else
			return @right_child.query(i, accum_lazy)
		end
	end

	def max_index(val)
		force if @lazy
		if @l == @r
			# if @val == val
			if @min == val
				return @r
			else
				return nil
			end
		end

		if val >= @right_child.min
			return @right_child.max_index(val)
		else
			return @left_child.max_index(val)
		end
	end

	def min_index(val)
		force if @lazy
		if @l == @r
			# if @val == val
			if @min == val
				return @l
			else
				return nil
			end
		end

		if val <= @left_child.max 
			return @left_child.min_index(val)
		else
			return @right_child.min_index(val)
		end
	end

	def force
		if @l == @r
			# @val -= @lazy
			# @val = 0 if @val < 0
			# @min = @max = @val
		else
			@left_child.update(@lazy)
			@right_child.update(@lazy)
		end
		@lazy = nil
	end

	def update(dec = 1, l = @l, r = @r)
		if @l == l && @r == r
			@min -= dec
			@min = 0 if @min < 0
			@max -= dec
			@max = 0 if @max < 0
			if @l != @r
			@lazy ||= 0
			@lazy += dec
			end
		elsif r <= @left_child.r 
			force if @lazy
			@left_child.update(dec, l, r)
			@min = @left_child.min
		elsif @right_child.l <= l
			force if @lazy
			@right_child.update(dec, l, r)
			@max = @right_child.max
		else
			force if @lazy
			@left_child.update(dec, l, @left_child.r)
			@min = @left_child.min
			@right_child.update(dec, @right_child.l, r)
			@max = @right_child.max
		end
	end

	attr_reader :val, :min, :max, :l, :r
end

m, n, m0, md, n0, nd = gets.split.map(&:to_i)

degs_l = [m0]
(m - 1).times do
	degs_l << (degs_l[-1] * 58 + md) % (n + 1)
end

degs_l.sort!
# degs_r.sort!

# degs_r = [n0]
# (n - 1).times do
# 	degs_r << (degs_r[-1] * 58 + nd) % (m + 1)
# end

tree = SegmentTree.new(degs_l)
k = 0
x = n0
n.times do
	if x == 0
		x = (x * 58 + nd) % (m + 1)
		next
	end

	y = tree.query(m - x)
	if y == 0
		i = tree.max_index(0)
		if i < n - 1
			tree.update(1, i+1, m-1)
			k += m - i - 1
	 	end
	else
		i = tree.min_index(y)
		j = tree.max_index(y)
		if j == n - 1
			tree.update(1, i, i+x-1)
		else
			tree.update(1, i, i - 1 + x - (m - 1 - j))
			tree.update(1, j+1, m-1)
		end
		k += x
	end
	x = (x * 58 + nd) % (m + 1)
end
p k