Ultraviolet A 1452-Jump (Joseph ring deformation)

Source: Internet
Author: User

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 ;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.