LUOGU P1965 转圈游戏 (Noip 2013)

传送门

解题思路

比较简单的模拟题,转圈一定有一个循环节,而且循环节长度一定小于m,因为循环节是一个%m的剩余系,然后一遍模拟记录下来循环节,快速幂即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>

using namespace std;
const int MAXN = 1000005;
typedef long long LL;

inline int rd(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)) {f=ch=='-'?0:1;ch=getchar();}
    while(isdigit(ch))  {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
    return f?x:-x;
}

int n,m,cnt,pos[MAXN],x,k;
int ans;

int fast_pow(int x,int y){
    int ret=1;
    for(;y;y>>=1){
        if(y&1) ret=(LL)ret*x%(cnt+1);
        x=(LL)x*x%(cnt+1);
    }
    return ret;
}

int main(){
    n=rd(),m=rd(),k=rd(),x=rd();pos[0]=x;
    int now=x;now=(now+m)%n;
    while(now!=x){
        pos[++cnt]=now;
        now=(now+m)%n;
    }
    ans=pos[fast_pow(10,k)];
    cout<<ans<<endl;
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/sdfzsyq/p/9681047.html