aboutsummaryrefslogtreecommitdiff
path: root/07_mathematics/josephus_queries_2164.cpp
diff options
context:
space:
mode:
Diffstat (limited to '07_mathematics/josephus_queries_2164.cpp')
-rw-r--r--07_mathematics/josephus_queries_2164.cpp17
1 files changed, 17 insertions, 0 deletions
diff --git a/07_mathematics/josephus_queries_2164.cpp b/07_mathematics/josephus_queries_2164.cpp
new file mode 100644
index 0000000..b17f3c9
--- /dev/null
+++ b/07_mathematics/josephus_queries_2164.cpp
@@ -0,0 +1,17 @@
1#include <iostream>
2
3int f(int n, int k, int m) {
4 int h = n/2 + m*(n%2);
5 if (n == 1) return 1;
6 if (k <= h) return 2*k-m;
7 return 2*f(n-h, k-h, m^(n%2))+m-1;
8}
9
10int main() {
11 int q, n, k;
12 std::cin >> q;
13 for (int i = 0; i < q; i++) {
14 std::cin >> n >> k;
15 std::cout << f(n, k, 0) << "\n";
16 }
17}

Generated with cgit - Back to sebastiano.tronto.net