No tags yet

No tag edit access

The problem statement has recently been changed. View the changes.

×
time limit per test: 0.25 sec.

memory limit per test: 9216 KB

memory limit per test: 9216 KB

input: standard

output: standard

output: standard

Integer numbers from 1 to N are written in the increasing order, clockwise along the circle. You are moving from integer to integer sequentally. You start moving clockwise from 1.

Moving operation consists of the following steps:

1) You must count Q-th integer in your current moving direction and erase that integer.

2) Then you must move to the next integer clockwise.

3) If that integer is odd then your moving direction becomes clockwise (or nothing happens if you are already moving clockwise).

4) If that integer is even then your moving direction becomes counter-clockwise (or nothing happens if you are already moving counter-clockwise).

If there are no integers left you stop the moving process. Your goal is to find the last erased integer.

Let us consider the following example where N=5 and Q=3. The numbers will be deleted in the following order - 3, 1, 4, 5, 2. The last erased number is 2.

Moving operation consists of the following steps:

1) You must count Q-th integer in your current moving direction and erase that integer.

2) Then you must move to the next integer clockwise.

3) If that integer is odd then your moving direction becomes clockwise (or nothing happens if you are already moving clockwise).

4) If that integer is even then your moving direction becomes counter-clockwise (or nothing happens if you are already moving counter-clockwise).

If there are no integers left you stop the moving process. Your goal is to find the last erased integer.

Let us consider the following example where N=5 and Q=3. The numbers will be deleted in the following order - 3, 1, 4, 5, 2. The last erased number is 2.

The first line of input contains N and Q (1<=N<=2000000; 1<=Q<=10).

Output the last erased integer.

Input

Test #1

5 2

Test #2

5 3

5 2

Test #2

5 3

Output

Test #1

3

Test #2

2

3

Test #2

2

See picture:

Author: | Sergey Simonchik |

Resource: | Petrozavodsk Summer Training Sessions 2004 |

Date: | August 25, 2004 |

Codeforces (c) Copyright 2010-2021 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Jun/25/2021 15:14:15 (i1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|