forked from t3nsor/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 2
/
anarc08h.cpp
70 lines (70 loc) · 1.77 KB
/
anarc08h.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// 2014-04-30
#include <iostream>
#include <vector>
using namespace std;
struct BIT {
int n;
vector<int> bit;
BIT(int n):n(n), bit(n+1) {}
int read(int idx) {
idx--;
int res = 0;
while (idx > 0) {
res += bit[idx];
idx -= idx & -idx;
}
return res;
}
int read2(int idx1, int idx2) {
return read(idx2) - read(idx1);
}
void update(int idx, int val) {
while (idx <= n) {
bit[idx] += val;
idx += idx & -idx;
}
}
int lower_bound(int target) {
if (target <= 0) return 1;
int pwr = 1; while (2*pwr <= n) pwr *= 2;
int idx = 0; int tot = 0;
for (; pwr; pwr >>= 1) {
if (idx+pwr > n) continue;
if (tot + bit[idx+pwr] < target) {
tot += bit[idx += pwr];
}
}
return idx+2;
}
int upper_bound(int target) {
if (target < 0) return 1;
int pwr = 1; while (2*pwr <= n) pwr *= 2;
int idx = 0; int tot = 0;
for (; pwr; pwr >>= 1) {
if (idx+pwr > n) continue;
if (tot + bit[idx+pwr] <= target) {
tot += bit[idx += pwr];
}
}
return idx+2;
}
};
int main() {
for (;;) {
int N, D; cin >> N >> D; if (N == 0) return 0;
BIT bit(N);
for (int i = 1; i <= N; i++) {
bit.update(i, 1);
}
int target = 1;
for (int i = N; i >= 1; i--) {
target = (target + D-1) % i;
if (target == 0) target = i;
int pos = bit.lower_bound(target) - 1;
bit.update(pos, -1);
if (i == 1) {
cout << N << ' ' << D << ' ' << pos << endl;
}
}
}
}