A through 1617: a circular motion game

Circling game

Relatively easy to think of ideas:

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

Remember to open long long fast power


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;
}

Guess you like

Origin www.cnblogs.com/Benjamin-cpp/p/11122944.html