解题思路:分析题目可知进行n轮游戏等价于没有进行游戏,所以我们只需要用快速幂让游戏轮数对n进行取模即可。然后进行余数次位置变换即可
C++代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, m, k, x;
int fast_pow(int a, int b, int p)
{
int res = 1 % p;
while (b)
{
if (b & 1)
{
res = 1LL * res * a % p;
}
a = 1LL * a * a % p;
b >>= 1;
}
return res;
}
signed main()
{
cin >> n >> m >> k >> x;
int t = fast_pow(10, k, n);
while (t -- ) x = (x + m) % n;
cout << x << endl;
return 0;
}