SDNU 1300.转圈游戏(快速幂)

Description

 n个MM(编号从0到n-1)围在一圈“丢手绢”。按照顺时针方向给n个位置编号,从0到n-1。最初,第0号MM在第0号位置,第1号MM在第1号位置,……,依此类推。 

游戏规则如下:每一轮第0号位置上的MM顺时针走到第m号位置,第1号位置MM走到第m+1号位置,……,依此类推,第n−m号位置上的MM走到第0号位置,第n-m+1号位置上的MM走到第1号位置,……,第n-1号位置上的MM顺时针走到第m-1号位置。  现在,一共进行了10^k轮,请问x号MM最后走到了第几号位置。  

Input

输入共1行,包含4个整数n、m、k、x,每两个整数之间用一个空格隔开。 

Output

输出共1行,包含1个整数,表示10^k轮后x号MM所在的位置编号。  

Sample Input

10 3 4 5

Sample Output

5

Hint

对于30%的数据,0<?<7; 对于80%的数据,0<?<10^7; 

对于100%的数据,1<?<1,000,000,0<?<?,0≤x <n< span="">,0<?<10^9。 

#include <cstdio>
#include <iostream>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
using namespace std;

#define ll long long
#define eps 1e-9

const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;

ll qsm(ll a, ll b, ll c)
{
    ll sum = 1;
    a = a % c;
    while(b)
    {
        if(b & 1)
            sum = (sum * a) % c;
        b /= 2;
        a = (a * a) % c;
    }
    return sum;
}

int main()
{
    ll n, m, k, x;
    scanf("%lld%lld%lld%lld", &n, &m, &k, &x);
    ll buffer = qsm(10, k, n);
    ll s = ((x % n) + ((m % n) * buffer) % n) % n;
    printf("%lld\n", s);
    return 0;
}

 

转载于:https://www.cnblogs.com/RootVount/p/11340435.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值