Research Joseph kind of problem

  Today exam T2 hung up (everyone else A I only hang), and then write a review about Joseph kind of problem.

  Joseph problem: n individual (No. 1 ~ n), from 1 Countin, to report exit m, and the rest of the people continue to start from 1 count off. Column are sequentially output number

  

  Reference illustrated.

  Analog is $ O (nm) $, and then I died. n, m to the 1e8.

  Do so many problems found in general, if your solution overtime, mostly because of processing a lot of information useless.

  Then take a look at those useless analog processing the information: he may know every one who died. But the title does not need to know these, one died before the total camel on it.

  Then we can think, requirements: in each round is determined not know the winner of the case who died.

  If this issue is resolved then it should be in line with the time limit.

  Switching ideas: When we consider, it is not considered as dead, 2 then there is no 2, but rather to consider the number put into 2 dead 2 re-assigned to a person. This is the key to solving problems.

  Then seize this calculation, first of all I must be 0 in the final round of the winner, because only one person, then I'll focus on the number of people is how change on the line.

  Then you can push back, and every time the subject of a number of people to go back layer by layer.

  Specifically: i remaining individual, the individual n + 1-i: then it must be from 0 m to find individuals to k, then k = (m% i) -1 No. die.

  That is: 0,1,2, ......, k-1, k, ......, ni

  Then we put k kill, renumbered.

  k+1,……,n-i,0,1,2,……k-1

  0,1,……n-i-k,k,k+1,……n-i-1

  Relabelling rule is id '= (id-k)% i

  Then we can label changes again to a recurrence.

  f[i]=(f[i-1]+m)%i

#include<cstdio>
using namespace std;
int f[100000020],n,m;
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=2;i<=n;i++)f[i]=(f[i-1]+m)%i;
    printf("%d\n",f[n]+1);
}
Long

 

Guess you like

Origin www.cnblogs.com/starsing/p/11401713.html