Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. using namespace std;
  5.  
  6. typedef unsigned long long ll;
  7.  
  8. ll cnt;
  9. ll res;
  10.  
  11. ll joseph(ll n, ll k) {
  12. if (n == 1) return 0;
  13. if (k == 1) return n - 1;
  14. if (k > n) return (joseph(n - 1, k) + k) % n;
  15. cnt = n / k;
  16. res = joseph(n - cnt, k);
  17. if (res < n % k) {
  18. res += n;
  19. res -= n % k;
  20. } else {
  21. res -= n % k;
  22. res += res / (k - 1);
  23. }
  24. return res;
  25. }
  26.  
  27.  
  28. int main() {
  29. int n, k;
  30. cin >> n >> k;
  31. std::cout << joseph(n, k) + 1 << std::endl;
  32. return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement