Wannafly Challenge 15 C "a team" (Josephus kind of problem)

Portal

 

References

  [1]:

  [2]: Forget

  [3]:My CSDN

the meaning of problems

  Personal circle n, the number of packets 2, leave a message, find the number of times x dequeue;

• understanding of Bowen [1]

  The first round dequeued certain odd number, if x is odd, then x must be in the first round of the team, ans = (x + 1) / 2;

  If x is not an odd number, then the process is performed as follows:

1 while(x%2 == 0)
2 {
3     x /= 2;
4     if(n&1)
5         x++;
6     ans += n/2;
7     n -= n/2;
8 }

  The following will explain the action of the code;

  ① If x is even, then the team in the first round, not even before the x and the team, a total of x / 2 numbered;

  ② If n is even, then the first round of CPC dequeued dequeue the n / 2 number, leaving the n / 2 number, x is the remaining numbers of x / 2 numbered;

  The team in the second round, the remaining number of still a dequeue a first number corresponding to the remaining n / 2 ①② numbered repeat the above process;

  If n is odd, the first round of CPC dequeue dequeued n / 2 + 1 is numbered, leaving the n / 2 number;

  However, this remaining n / 2 in the second round the numbered teams, a first team ID is no longer the first number, the second number but, different from the above-mentioned process ②;

  How can this be handled?

  Very good run in the first round of the team, so the team last number n into the second round of the team's first team position, then the second round of the team's numbers, the first is still out on is the first number;

  Is the remaining number of variations on the basis of prior +1, followed by repeating the above procedure ①②;

•Code

. 1 #include <bits / STDC ++ H.>
 2  the using  namespace STD;
 . 3  #define LL Long Long
 . 4  
. 5  LL n-, X;
 . 6  int Q;
 . 7  
. 8  LL the Solve ()
 . 9  {
 10      LL ANS = 0 ;
 . 11      LL = CUR n-;
 12 is      the while (x% 2 == 0 )
 13 is      {
 14          x / = 2 ; /// and before consensus x x / 2 numbered 
15          IF (& CUR . 1 ) ///If the current remaining odd-numbered, the last number as the next dequeue 
16              X ++ ;
 . 17          ANS + = CUR / 2 ;
 18 is          CUR - = CUR / 2 ;
 . 19      }
 20 is      ANS + = (X + . 1 ) / 2 ;
 21 is      return ANS;
 22 is  }
 23 is  int main ()
 24  {
 25      Scanf ( " % D% LLD " , & n-, & Q);
 26 is      the while (q - )
 27      {
 28          Scanf ( " % LLD ",&x);
29         printf("%lld\n",Solve());
30     }
31     return 0;
32 }
View Code

• understanding of Bowen [2] of

  

  Dapper code, tql!

  If x is not odd, then x is definitely not a team in this round, but before the x-even number is no longer in this round of the team;

  That is x / 2 which are not numbered in a dequeue, then increase corresponding to x / 2 numbered in the tail of the n number, x becomes the tail number;

  The above process is then performed;

•Code

. 1 #include <bits / STDC ++ H.>
 2  the using  namespace STD;
 . 3  #define LL Long Long
 . 4  
. 5  LL n-, X;
 . 6  int Q;
 . 7  
. 8  LL the Solve ()
 . 9  {
 10      the while (X% 2 == 0 )
 . 11          x + = n-x / 2 ; /// the x / 2 numbered added to the tail, the tail becomes number x, and x can be determined whether the team in the first round 
12 is      return x / 2 + . 1 ;
 13 is  }
 14  int main ()
 15  {
 16     scanf("%lld%d",&n,&q);
17     while(q--)
18     {
19         scanf("%lld",&x);
20         printf("%lld\n",Solve());
21     }
22     return 0;
23 }
View Code

 

Guess you like

Origin www.cnblogs.com/violet-acmer/p/11111440.html