码迷,mamicode.com
首页 > 其他好文 > 详细

一本通 1617:转圈游戏

时间:2019-07-02 21:24:48      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:cpp   show   names   ++   tps   def   href   class   string   

转圈游戏

比较容易想到的思路:

进行10^k轮游戏后的结果与进行(10^k)%n 的结果是一致的,所以只需要快速幂求(10^k)%n,然后再求出(10^k)%n轮后的结果即可。

快速幂时记得开long long


Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
//Mystery_Sky
//
#define M 1000100
#define ll long long
ll n, m, k, x, ans;
ll quickPow(ll x, ll k)
{
    ll ret = 1;
    while(k) {
        if(k & 1) ret = (ret * x) % n;
        k >>= 1;
        x = (x * x) % n;
    }
    return ret % n;
} 

int main() {
    ll t;
    scanf("%lld%lld%lld%lld", &n, &m, &k, &x);
    t = quickPow(10, k);
    for(int i = 1; i <= t; i++) {
        x = (x + m) % n;
    }
    printf("%lld\n", x);
    return 0;
}

一本通 1617:转圈游戏

标签:cpp   show   names   ++   tps   def   href   class   string   

原文地址:https://www.cnblogs.com/Benjamin-cpp/p/11122944.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!