78 lines
1.8 KiB
C++
78 lines
1.8 KiB
C++
#include <iostream>
|
|
|
|
class CycledLinkedList {
|
|
public:
|
|
struct Node {
|
|
int val = 0;
|
|
Node* next = nullptr;
|
|
Node* prev = nullptr;
|
|
};
|
|
|
|
Node* head = nullptr; // They are POINTERS, not OBJECTS.
|
|
|
|
void push_back(int val) {
|
|
if (head == nullptr) {
|
|
head = new Node({.val = val});
|
|
head->next = head;
|
|
head->prev = head;
|
|
return;
|
|
}
|
|
Node* node = new Node({.val = val});
|
|
head->prev->next = node;
|
|
node->prev = head->prev;
|
|
head->prev = node;
|
|
node->next = head;
|
|
}
|
|
|
|
void remove(Node* node) {
|
|
node->prev->next = node->next;
|
|
node->next->prev = node->prev;
|
|
if (node == head) {
|
|
head = node->next;
|
|
}
|
|
delete node;
|
|
}
|
|
|
|
void init_cycle(int n) {
|
|
for (int i = 0; i < n; i++) {
|
|
push_back(i);
|
|
}
|
|
}
|
|
|
|
friend std::ostream& operator<<(std::ostream& os, const CycledLinkedList& list) {
|
|
for (Node* node = list.head->next; node != list.head; node = node->next) {
|
|
os << node->val << "<->";
|
|
}
|
|
os << list.head->val << std::endl;
|
|
return os;
|
|
}
|
|
};
|
|
|
|
int n, m, target_order;
|
|
CycledLinkedList players;
|
|
|
|
int target_idx() {
|
|
for (int i = 0; i < target_order - 1; i++) {
|
|
for (int j = 0; j < m - 1; j++) {
|
|
players.head = players.head->next;
|
|
}
|
|
players.remove(players.head);
|
|
}
|
|
for (int j = 0; j < m - 1; j++) {
|
|
players.head = players.head->next;
|
|
}
|
|
return players.head->val;
|
|
}
|
|
|
|
int main() {
|
|
std::ios::sync_with_stdio(false);
|
|
std::cin.tie(nullptr);
|
|
|
|
std::cin >> n >> m >> target_order;
|
|
players.init_cycle(n);
|
|
// std::clog << players << std::endl;
|
|
std::cout << target_idx() + 1 << std::endl;
|
|
|
|
return 0;
|
|
}
|