POJ 3517
约瑟夫环问题
序列1:0,1,2...m-2,m...n-2,n-1(n-1个)
序列2:m,m+1,m+2...n-1,0,1..m-2(n-1个)
序列3:0,1,2...n-2,n-1(n-1个)
序列4:k,k+1,k+2...k-2(n-2个)
先由f[1] = 0;
f[i] = (f[i-1]+k)%i递推到n-1,然后f[n] = (f[n-1] + m)%n+1即可
View Code
1 #include<iostream> 2 #include<cstdio> 3 #define MAXN 10004 4 using namespace std; 5 int f[MAXN]; 6 7 8 int main() 9 { 10 int n,m,k; 11 while (scanf("%d%d%d",&n,&k,&m) != EOF) { 12 if (n == 0 && k == 0 && m == 0)break; 13 f[1] = 0; 14 for (int i(2); i<n; ++i) { 15 f[i] = (f[i-1] + k)%i; 16 } 17 f[n] = (f[n-1] + m)%n; 18 cout<<f[n]+1<<endl; 19 } 20 return 0; 21 }