OJ-Problems-Source/.ACM-Templates/Josephus_problem.cpp

13 lines
265 B
C++
Raw Normal View History

2016-08-11 11:01:56 +08:00
///约瑟夫问题,n个人,查m个数
int JosephusProblem_Solution4(int n, int m)
{
if(n < 1 || m < 1)
return -1;
vector<int> f(n+1,0);
for(unsigned i = 2; i <= n; i++)
f[i] = (f[i-1] + m) % i;
return f[n];
}