p254.ans1
======================
3

=================
p254.in1
======================
5 2

=================
p254.cpp
======================
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int N, Q;
vector<int> nxt, prv;

void read() { cin >> N >> Q; }

void solve() {
    nxt.resize(N + 1);
    prv.resize(N + 1);

    for(int i = 1; i <= N; i++) {
        nxt[i] = (i == N) ? 1 : i + 1;
        prv[i] = (i == 1) ? N : i - 1;
    }

    int current = 1;
    bool clockwise = true;
    int last_erased = -1;
    int remaining = N;

    while(remaining > 0) {
        int pos = current;
        for(int i = 1; i < Q; i++) {
            if(clockwise) {
                pos = nxt[pos];
            } else {
                pos = prv[pos];
            }
        }

        last_erased = pos;

        int next_pos = nxt[pos];
        int prev_pos = prv[pos];
        nxt[prev_pos] = next_pos;
        prv[next_pos] = prev_pos;

        remaining--;

        if(remaining == 0) {
            break;
        }

        current = next_pos;

        if(current % 2 == 1) {
            clockwise = true;
        } else {
            clockwise = false;
        }
    }

    cout << last_erased << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}

=================
statement.txt
======================
254. Strange Random
time limit per test: 0.25 sec.
memory limit per test: 9216 KB
input: 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.

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

Output
Output the last erased integer.

Sample test(s)

Input
Test #1
5 2

Test #2
5 3

Output
Test #1
3

Test #2
2

Note
See picture:

Author:	Sergey Simonchik
Resource:	Petrozavodsk Summer Training Sessions 2004
Date:	August 25, 2004








=================
