LA 3882 and then there is one

Source: Internet
Author: User

Problem-Solving ideas: analysis to a long time, too lazy analysis, affixed to the analysis of a Daniel, the code is my own writing.


n the number of rows in a circle, the first time to delete m, after each k number deleted once, the last one to be deleted.

If the problem is to simulate the whole process with a linked list or an array, the time complexity will be up to O (NK), and the n<=10000,k<=10000 will be directly tle.

So is there any other way? The answer is yes.

We ignore the M first, analyze the number of each k deleted once, that is the classic Joseph problem.

Then, each number (1~n) is numbered sequentially 0~n-1

The number of the first deleted number is x, then x= K%n-1 (note is number, really deleted number is number + 1)

Then the remaining number of n-1 can form a new Joseph ring.

What is the number now? Obviously: (Make x+1=y, that is, y= k%n)

Y, y+1, y+2 ... n-1, 0, 1 ... y-2

Putting y in the first order is the next time you start counting from it.

Re-start number of k numbers.

You said re-, uh. Then you can renumber it like this:







Now it becomes the n-1 number (numbered from 0~n-2) of the Joseph Question!

Assuming that Z is the number left by the last n-1 number, then Z ' is the number left by the n person, then the z ' = (z+y)% n is clearly

How do you know the n-1 of a solution? Just give it a hand, you know n-2.

So, there are:

ans [1]=0;

Ans [n] = (ans[n-1]+k)%n;

(one might ask: Is it not z ' = (z+y)% n?) Now how did you become K? Because y= k%n, modulo operation)

Then, the answer is +1 (number and number)

So what about the first time this question is M?

Also very simple, we move k every time, there are n number, then the answer is Ans[n]

But the first move is M, so the trailing movement has a constant gap (K-M)

So the answer is: (ans[n]– (k–m))% n (note may be less than 0 and final answer +1)

1#include <cstdio>2 intMain ()3 {4     intN, K, M, a[10005];5      while(~SCANF ("%d%d%d", &n, &k, &m) && (n | | m | |k))6     {7a[1] =0;8          for(inti =2; I <= N; i++) A[i] = (a[i-1]+K)%i;9         intA = (M-k +1+ a[n])%N;Ten         if(A <=0) A + = n;//note may be less than 0 Oneprintf"%d\n", a); A     } -     return 0; -}
View Code


LA 3882 and then there is one

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: 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.