QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 128 MB Total points: 10

#6079. Crystal [A]

Statistics

Bajtazar works at a company that manufactures crystals. Each atom of a crystal is of one of three types, conventionally denoted as $A_{0}, A_{1}, A_{2}$. A crystal of order $n$ consists of $3n(n - 1) + 1$ atoms arranged in a hexagon with side length $n-1$, composed of $6(n - 1)^{2}$ equilateral triangles with side length 1. The figure below shows an example of a crystal of order 2:

Crystals exhibit electrical properties. Every three atoms forming a triangle of side length 1 that have $pairwise\ distinct\ types$ generate an electric field with a unit charge—positive if the atoms are in the order $A_{0}, A_{1}, A_{2}$ when traversing the triangle counter-clockwise, or negative if they are in that order when traversing the triangle clockwise. The charge of the entire crystal is equal to the sum of the charges of all such triples of atoms.

Although Bajtazar has a device for producing arbitrarily large crystals, it operates in a peculiar way. The device is programmed by providing six numbers: $n$, $m$, $s$, $a$, $b$, $k$. It creates a crystal of order $n$ by placing atoms row by row, starting from the top row. Within each row, atoms are placed from left to right. The type of each successive atom $A_{r}$ is chosen pseudo-randomly by applying the following procedure:

$$ s := ((a \cdot s + b) \operatorname{div} k) \bmod (3 \cdot m) $$

$$ r := s \operatorname{div} m $$

The operation $x \operatorname{div} y$ denotes the integer part of the division of $x$ by $y$, and $x \bmod y$ is the remainder of the division of $x$ by $y$. The two instructions above are executed one after the other.

Bajtazar has asked you to write a program that determines the charge of the crystal for the given input parameters of the device.

Input

The first line of input contains six integers $n$, $m$, $s$, $a$, $b$, $k$ describing the input parameters of the crystal production device ($2 \le n \le 10^{9}$, $1 \le m \le 10^{6}$, $0 \le s, a, b < 3\cdot m$, $1 \le k < 3 \cdot m$).

Output

The only line of output should contain an integer representing the charge of the crystal produced by the device for the given parameters.

Examples

Input 1

2 7 0 5 10 1

Output 1

1

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.