题目大意
n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏。按照顺时针方向给 n 个位置编号,从0 到 n-1。最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 1 号位置,……,依此类推。游戏规则如下:每一轮第 0 号位置上的小伙伴顺时针走到第 m 号位置,第 1 号位置小伙伴走到第 m+1 号位置,……,依此类推,第n − m号位置上的小伙伴走到第 0 号位置,第n-m+1 号位置上的小伙伴走到第 1 号位置,……,第 n-1 号位置上的小伙伴顺时针走到第m-1 号位置。
现在,一共进行了 10^k轮,请问 x 号小伙伴最后走到了第几号位置。
题解
大胆想,从整体角度考虑,所求(=(10^k m+x)mod n)。用快速幂、快速幂运算即可。
#include <cstdio>
#include <iostream>
using namespace std;
#define ll long long
ll Mult(ll a, ll b, ll p)
{
ll ans = 0;
while(b)
{
if(b & 1)
ans = (ans + a) % p;
a = (a + a) % p;
b >>= 1;
}
return ans;
}
ll Power(ll a, ll n, ll p)
{
ll ans = 1;
while(n)
{
if(n & 1)
ans=Mult(ans, a, p);
a = Mult(a, a, p);
n >>= 1;
}
return ans;
}
int main()
{
ll n, m, k, x;
cin>>n>>m>>k>>x;
cout<<(Mult(Power(10, k, n), m, n) + x) % n<<endl;
return 0;
}