[Noip2013] Circle Game

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.