Description
N friends (numbers from 0 to n-1) sat around and played games. N positions are numbered clockwise from 0 to n-1. Initially, the partner on the 1949th was at the position on the 1949th and the partner on the 1949th were at the position on the 1949th ,......, And so on.
The rules of the game are as follows: every round of the second ,......, According to this push, the partner on the nth −m went to the nth position, and the partner on the nth-M + 1th went to the second position ,......, The partner at location n-1 goes clockwise to M-1.
Now, we have performed a total of 10 ^ K rounds. Could you tell me the number of the X partner finally reached its position.
Format input format
Input a total of 1 line, contains four integers n, m, K, X, each two integers are separated by a space.
Output Format
The output contains 1 line, which contains 1 integer, indicating the position number of the partner after the 10 ^ K wheel X.
Example 1 input 1 [copy]
?10 3 4 5
Sample output 1 [copy]
5
Restrictions
1 s for each test point.
Prompt
For 30% of the data, 0 <k <7;
For 80% of the data, 0 <k <10 ^ 7;
For 100% of data, 1 <n <1,000,000, 0 <m <n, 1 <= x <= N, 0 <k <10 ^ 9.
According to the question, we can get ans = (M * 10 ^ K) mod n + x Fast Power second (don't forget to open ll or 30 minutes)
# Include <cstdio>
Using namespace STD;
Long long n, m, X, K;
Long long POW (long a, long B)
{
Long long ret = 1;
While (B)
{
If (B & 1) ret = RET * A % N;
A = A * A % N;
B> = 1;
}
Return RET % N;
}
Int main ()
{
Scanf ("% LLD", & N, & M, & K, & X );
Long long ans = POW (10, k) % N;
Ans = ans * m % N;
Ans = (ANS + x) % N;
Printf ("% LLD", ANS );
}
[Noip2013] Circle Game