atcoder#ARC127F. [ARC127F] ±AB
[ARC127F] ±AB
Score : points
Problem Statement
Given are integers , , , and , where and are guaranteed to be coprime. Additionally, you have an integer . Initially, .
You can do the following four kinds of operations any number of times in any order.
- Replace the value of with .
- Replace the value of with .
- Replace the value of with .
- Replace the value of with .
Here, must hold at any moment during this process.
Under this condition, find how many different values can take.
For each input file, solve test cases.
Constraints
- and are coprime.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Each case is in the following format:
Output
Print the answer for each case.
5
3 5 0 5
1 2 5 10
5 8 4 9
10 99 48 106
500000000 500000001 123456789 900000000
4
11
4
10
800000002
In the first case, can take four values: .