Question
Set 1 ~ N is arranged in a circle in a counter-clockwise order. No k numbers are deleted from 1, and all numbers are deleted.
Calculate the number of three deleted objects.
Ideas
We already know how to release the last deleted number (refer to Baidu encyclopedia)
F (1) = 0, indicating that when the last one is left, this number is 0
F (n) = (f (n-1) + m) % n
Store the last number 1, 2, and 3 until the first one.
Code
/** =================================================== ===== * This is a solution for ACM/ICPC problem ** @ source: uva-1452 Jump * @ author: shuangde * @ blog: blog.csdn.net/shuangde800 * @ email: zengshuangde@gmail.com * =================================================== =====*/# include <iostream> # include <cstdio> # include <algorithm> # include <vector> # include <queue> # include <cmath> # include <cstring> using namespace std; typedef long lon G int64; const int INF = 0x3f3f3f; const double PI = acos (-1.0); const int MAXN = 110; int n, m; int main () {int T; scanf ("% d", & T); while (T --) {scanf ("% d", & n, & m); int ans1 = 0, ans2, ans3; for (int I = 2; I <= n; ++ I) {ans1 = (ans1 + m) % I; if (I = 2) {// when the last two digits are left, the number is 0, 1, and the last two deleted digits are introduced. The current value is ans2 =! Ans1;} else if (I = 3) {// number 0, 1, 2, number of last-to-last deletions: ans2 = (ans2 + m) % I; bool vis [3]; memset (vis, 0, sizeof (vis )); vis [ans1] = vis [ans2] = true; for (int j = 0; j <3; ++ j) if (! Vis [j]) {ans3 = j; break ;}} else {ans2 = (ans2 + m) % I; ans3 = (ans3 + m) % I ;}} ans1 = (ans1 + 1) % n; ans2 = (ans2 + 1) % n; ans3 = (ans3 + 1) % n; printf ("% d \ n", ans3? Ans3: n, ans2? Ans2: n, ans1? Ans1: n);} return 0 ;}