LA-3882-And Then There Was One

Source: Internet
Author: User

Meaning: 1 to n are arranged clockwise. Take m for the first time, and then take k in the clockwise direction ...... Until there is only one number left, what is the last number left? (2n10000, 1k10000, 1mn)

--> After reading rujia's book for more than a day, I cannot figure out the last step. In the end, I am still using my own ideas ......

Set d [I] to number n numbers starting from 0 clockwise. Delete 0 for the first time, delete one number for each k, and then delete the remaining number for each k.

In fact, d [I] is the number of digits offset clockwise.

State transition equation:

D [I] = (k-1 + d [I-1]) % (n-1) + 1;

(After 0 is deleted, 1, 2 ,..., n, minus 1 to get 0, 1, 2 ,..., n-1, so the original Delete k --> K-1, clockwise Offset d [I-1] bit, modulo, add 1 back to the original number)


[Cpp]
# Include <cstdio>
 
Using namespace std;
 
Const int maxn = 10000 + 10;
 
Int d [maxn]; // d [I] indicates that the number of I is deleted from 0, and the remaining number is deleted.
 
Int main ()
{
Int n, k, m, I;
D [1] = 0;
While (~ Scanf ("% d", & n, & k, & m ))
{
If (! N &&! K &&! M) return 0;
For (I = 2; I <= n; I ++) d [I] = (k-1 + d [I-1]) % (I-1) + 1;
Int ret = (m-1 + d [n]) % n + 1;
Printf ("% d \ n", ret );
}
Return 0;
}

# Include <cstdio>

Using namespace std;

Const int maxn = 10000 + 10;

Int d [maxn]; // d [I] indicates that the number of I is deleted from 0, and the remaining number is deleted.

Int main ()
{
Int n, k, m, I;
D [1] = 0;
While (~ Scanf ("% d", & n, & k, & m ))
{
If (! N &&! K &&! M) return 0;
For (I = 2; I <= n; I ++) d [I] = (k-1 + d [I-1]) % (I-1) + 1;
Int ret = (m-1 + d [n]) % n + 1;
Printf ("% d \ n", ret );
}
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.