使用数学知识,F(n,m)=(F(n-1,m)+m)%n;
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int n,k;
while(scanf("%d%d",&n,&k)==2)
{
int p=0;
for(int i=2;i<=n;i++)
p=(p+k)%i;
cout<<p+1<<endl;
}
return 0;
}
函数图像可以AC,具体移步https://blog.csdn.net/f_zyj/article/details/77980246
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m,ans,x;
int main()
{
cin>>n>>m;
ans=0;
for (ll i=2;i<=n;i+=x+1)
{
x=(i-ans-m)/(m-1);
if (x<0) x=0;
if (i+x+1>n) x=n-i;
ans=(ans+m*(x+1))%(i+x);
}
cout<<ans+1;
return 0;
}