F.cpp 525 B

12345678910111213141516171819202122
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4. int n, m;
  5. while (cin >> n >> m) {
  6. list<int> L;
  7. for (int i = 1; i <= n; i++) L.push_back(i);
  8. list<int>::iterator it = L.begin();
  9. while (L.size() > 1) {
  10. for (int k = 1; k < m; k++) {
  11. ++it;
  12. if (it == L.end()) it = L.begin();
  13. }
  14. it = L.erase(it);
  15. if (it == L.end()) it = L.begin();
  16. }
  17. cout << *L.begin() << endl;
  18. }
  19. return 0;
  20. }