#include <cstdio> #include <vector> #include <algorithm> using namespace std; const int MAXN = 1 << 17; struct SegTree { int n, m; int a[MAXN + MAXN]; static int L(int i) { return i << 1; } static int R(int i) { return L(i) ^ 1; } void init(int m) { this->m = m; n = 1; while (n < m) { n <<= 1; } fill(a + n, a + n + m, 1); fill(a + n + m, a + n + n, 0); for (int i = n - 1; i > 0; --i) { a[i] = a[L(i)] + a[R(i)]; } } void reset(int i) { i += n; while (i > 0) { --a[i]; i >>= 1; } } int find(int p, int pl, int pr, int l, int r, int& k) { if (pl == l && pr == r) { if (a[p] <= k) { k -= a[p]; return -1; } else if (pr - pl == 1) { return pl; } } int pm = (pl + pr) / 2; if (r <= pm) { return find(L(p), pl, pm, l, r, k); } else if (pm <= l) { return find(R(p), pm, pr, l, r, k); } else { int ret = find(L(p), pl, pm, l, pm, k); if (ret == -1) { ret = find(R(p), pm, pr, pm, r, k); } return ret; } } int find(int i, int k) { k %= a[1]; i = find(1, 0, n, i, m, k); if (i == -1) { i = find(1, 0, n, 0, m, k); } return i; } } st; int main() { int n, q, x, a, b, m; vector<pair<int, int> > v; while (scanf("%d%d%d%d%d%d", &n, &q, &x, &a, &b, &m) != EOF) { // fprintf(stderr, "%d %d %d %d %d %d\n", n, q, x, a, b, m); v.resize(q); for (int i = 0; i < q; ++i) { scanf("%d", &v[i].first); v[i].second = i; } sort(v.begin(), v.end()); st.init(n); for (int i = 0, j = 0, k = 0; i < q; ++i) { while (j < v[i].first) { ++j; k = st.find(k, x); st.reset(k); x = (1LL * x * a + b) % m; } v[i].first = k; swap(v[i].first, v[i].second); } sort(v.begin(), v.end()); for (int i = 0; i < q; ++i) { if (i > 0) { putchar(' '); } printf("%d", v[i].second + 1); } puts(""); } return 0; }
【约瑟夫改进问题】
http://blog.csdn.net/hackbuteer1/article/details/6657938
http://blog.csdn.net/u012377333/article/details/46343303
3.POJ1012——再解一遍艰难的约瑟夫
http://bbs.csdn.net/topics/380056863
http://blog.csdn.net/cscj2010/article/details/7586016
5.全排列算法及实现
http://blog.csdn.net/hackbuteer1/article/details/6657435
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-05 09:28:15