BZOJ 1998: [Hnoi2010]Fsk物品调度

求出每一个 \(pos\) 之后就是置换的事了,置换里没有空位就得引入空位,否则就直接交换
现在就是给每个位置找一对 \(x\)\(y\)
题目要求 \(y\) 最小之后 \(x\) 最小
观察式子发现 \(y\) 确定了当前位置在哪个置换中,\(x\) 确定了在置换的哪个位置上
那么对置换和位置都用一个并查集维护即可

#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define pli pair<ll, int>
#define lp p << 1
#define rp p << 1 | 1
#define mid ((l + r) / 2)
#define lowbit(i) ((i) & (-i))
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=((b)-1);i>=(a);i--)
#define Edg int ccnt=1,head[N],to[E],ne[E];void addd(int u,int v){to[++ccnt]=v;ne[ccnt]=head[u];head[u]=ccnt;}void add(int u,int v){addd(u,v);addd(v,u);}
#define Edgc int ccnt=1,head[N],to[E],ne[E],c[E];void addd(int u,int v,int w){to[++ccnt]=v;ne[ccnt]=head[u];c[ccnt]=w;head[u]=ccnt;}void add(int u,int v,int w){addd(u,v,w);addd(v,u,w);}
#define es(u,i,v) for(int i=head[u],v=to[i];i;i=ne[i],v=to[i])
const int MOD = 1e9 + 7;
void M(int &x) {if (x >= MOD)x -= MOD; if (x < 0)x += MOD;}
int qp(int a, int b = MOD - 2) {int ans = 1; for (; b; a = 1LL * a * a % MOD, b >>= 1)if (b & 1)ans = 1LL * ans * a % MOD; return ans % MOD;}
template<class T>T gcd(T a, T b) { while (b) { a %= b; std::swap(a, b); } return a; }
template<class T>bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template<class T>bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
char buf[1 << 21], *p1 = buf, *p2 = buf;
inline char getc() {
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
}
inline int _() {
  int x = 0, f = 1; char ch = getc();
  while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getc(); }
  while (ch >= '0' && ch <= '9') { x = x * 10ll + ch - 48; ch = getc(); }
  return x * f;
}

const int N = 1e5 + 7;
int n, s, q, p, m, d, c[N], ne[N], cnt, belong[N];
bool vis[N];
struct DSU {
  int fa[N];
  void init() { rep (i, 0, n) fa[i] = i; }
  int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
} circle, point;

void done(int x) {
  int fx = point.find(x), fy = point.find((fx + d) % n);
  if (fx != fy) {
    point.fa[fx] = fy;
    return;
  }
  fx = circle.find(belong[x]), fy = circle.find((fx + 1) % cnt);
  circle.fa[fx] = fy;
}

void solve() {
  n = _(), s = _(), q = _(), p = _(), m = _(), d = _() % n;
  circle.init(); point.init();
  rep (i, 1, n) c[i] = (1LL * c[i - 1] * q + p) % m;
  cnt = 0;
  memset(belong, -1, sizeof(belong));
  rep (i, 0, n) if (belong[i] == -1) {
    for (int j = i; belong[j] < 0; j = (j + d) % n)
      belong[j] = cnt;
    cnt++;
  }
  ne[0] = s;
  done(s);
  rep (i, 1, n) {
    c[i] %= n;
    int y = circle.find(belong[c[i]]);
    int x = point.find((c[i] + (y - belong[c[i]] + cnt) % cnt) % n);
    ne[i] = x; done(x);
  }
  int ans = 0;
  memset(vis, 0, sizeof(vis));
  rep (i, 0, n) {
    if (!vis[i]) {
      int cur = i;
      int flag = 0, num = 0;
      while (!vis[cur]) {
        num++;
        vis[cur] = 1;
        if (cur == s) flag = 1;
        cur = ne[cur];
      }
      if (flag == 1) ans += num - 1;
      else if (num > 1) ans += num + 1;
    }
  }
  printf("%d\n", ans);
}

int main() {
#ifdef LOCAL
  freopen("ans.out", "w", stdout);
#endif
  int T = _();
  while (T--) solve();
#ifdef LOCAL
  printf("%.10f\n", (db)clock() / CLOCKS_PER_SEC);
#endif
  return 0;
}
posted @ 2020-03-07 11:43  Mrzdtz220  阅读(74)  评论(0编辑  收藏  举报