有n个同学围成一圈,其id依次为1~n(nn号挨着1号)。
现在从1号开始报数,第一回合报到m的人就出局,第二回合从出局的下一个人开始报数,报到m2的同学出局。
以此类推,直到最后一个回合报到mn−1的人出局,剩下最后一个同学。
输出这个同学的编号。
输入格式
共一行,包含两个整数n和m。
输出格式
输出最后剩下的同学的编号。
数据范围
n≤15,m≤5
输入样例:
5 2
输出样例:
5
代码:
#include<iostream>
using namespace std;
int main()
{
int n,m,i,j,a[100]={0},r,k,x;
cin>>n>>m;
x=1;
for(i=1,r=n;i<=n;i++,r--)
{
k=1;
for(j=1;j<=i;j++)
k=k*m%r;
if(k==0)
k=r;
while(1)
{
if(a[x]==0)
{k--;
if(k==0)
{
a[x]=1;
break;
}
}
x++;
if(x>n)
x=1;
}
}
cout<<x<<endl;
return 0;
}