Chapter 1 of algorithm competition-Training Guide-1.26 _ La 3882

Source: Internet
Author: User

This question is tragic. Joseph's mathematical solution has never been heard of it.


This is the case when I look up the Internet. The thought is like this, because every time you report a number, you will get rid of a person. If we do not consider the number, get people (people are different .) For example, as long as you have fixed the beginning, the only person must be the last one left behind, and the START will not change.


In fact, we play this game in real life as follows: remove a person, re-arrange the number, then remove a person, re-arrange the number, then remove a person, and then get the last person.

This is actually in line with our common sense, but it is troublesome for us to solve the problem, that is, how do we distinguish these people? Of course, we need to give each person a unique number, then the only problem we need to solve is how to get the number, instead of the number we re-compile each time?

There is a formula: F [I] = (F [I-1] + M) % I; it is a bit hard to understand, but it can still be understood if it is understood, in fact, when you do I must have removed the number of the M-1, it is actually you use F [I-1] To get the re-number plus our M is that is the starting to set the number.

But you can't think too much. If you think too much, you may be able to tear your mind apart. I have to understand it!

I don't quite understand the final result. I will continue to think about it after dinner and edit it later.

Paste the Code:

#include <stdio.h>#include <string.h>#include <iostream>#include <string>using namespace std;const int MAXN = 10000 + 5;int f[MAXN];int main(){int n, k, m;while (scanf("%d%d%d", &n, &k, &m) != EOF){if (n == 0 && k == 0 && m == 0){break;}if (n == 1){printf("1\n");continue;}f[1] = 0;for (int i = 2; i <= n; i++){f[i] = (f[i - 1] + k) % i;}int ans = (m - k + f[n] + 1) % n;if (ans <= 0){ans += n;}printf("%d\n", ans);}//system("pause");return 0;}

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.