Title Description
n the number of rows in a circle. Delete m for the first time, delete each k number once, and ask for the last number to be deleted.
Problem analysis
First, in order to take the mold, n personal number is 0,1,2......n-1, when only 1 people, it must be numbered 0 (only it), so dp[1]=0,
When there are two people, the last remaining (dp[1]+k)%2, is the reverse thinking process, so dp[i]= (dp[i-1]+k)%i;
There is also because the first time is the deletion of M, then the difference will be added to the margin, so ans= (dp[n]+k-m)%n; Due to the k,m difference
The value may be very small negative numbers, so that ans is negative, so when ans<0, Ans+=n, finally will ans+1, will be easy to take the mold
Minus 1 to make up.
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace Std;const int maxn=100000+100;int Dp[maxn];int main () { int n,m,k; while (~SCANF ("%d%d%d", &n,&k,&m) &&n) { dp[1]=0; for (int i=2;i<=n;i++) dp[i]= (dp[i-1]+k)%i; int ans= (dp[n]+ (m-k))%n; if (ans<0) ans+=n; printf ("%d\n", ans+1); } return 0;}
Example 26 variants of the Joseph problem (and then there was One,japan 2007,la 3882)