UVa 440-eeny Meeny Moo

Source: Internet
Author: User

Title: Joseph Ring, there is a ring first delete the first element, and then every m number of deleted, ask the last left is the number 2nd element,

The minimum number of M to be gone.

Analysis: Number theory, simulation. The element number is 0~n-1, and the last numbered recurrence relationship is left: f (n,m) = (f (n-1,m) +m)%n.

Therefore, the problem is converted into n-1 element, the first element of the Joseph Ring, in order to enumerate m to find the first one can be established.

Description: ╮(╯▽╰)╭.

#include <iostream> #include <cstdlib> #include <cstdio>using namespace std;int deal (int n, int m) {int S = 0;for (int i = 2; i < n; + + i) s = (s+m)%i;return s;} int main () {int n;while (CIN >> n && N) {for (int i = 1;; + + i) {if (!deal (n, i)) {cout << i << E ndl;break;    }}} return 0;}

UVa 440-eeny Meeny Moo

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.