diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2026-07-06 19:08:08 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2026-07-06 19:08:08 +0200 |
| commit | 96254947699986c59f0dc63d69fd4b76bd3ed43e (patch) | |
| tree | 6c4dca945d7f7427c48be234d827fe4d33be02c5 /07_mathematics/josephus_queries_2164.cpp | |
| download | cses-96254947699986c59f0dc63d69fd4b76bd3ed43e.tar.gz cses-96254947699986c59f0dc63d69fd4b76bd3ed43e.zip | |
Initial commit
Diffstat (limited to '07_mathematics/josephus_queries_2164.cpp')
| -rw-r--r-- | 07_mathematics/josephus_queries_2164.cpp | 17 |
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 | |||
| 3 | int 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 | |||
| 10 | int 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 | } | ||
