QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#406208#7613. Inverse Problemucup-team3215TL 23ms3724kbC++205.4kb2024-05-06 22:30:292024-05-06 22:30:31

Judging History

你现在查看的是最新测评结果

  • [2024-05-06 22:30:31]
  • 评测
  • 测评结果:TL
  • 用时:23ms
  • 内存:3724kb
  • [2024-05-06 22:30:29]
  • 提交

answer

#include <array>
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

constexpr int mod = 1e9 + 7;

auto& mul(auto&& a, auto b) { return a = a * (uint64_t) b % mod; }
int inv(int a) { for (int r = 1, b = mod - 2; ; b /= 2, mul(a, a)) if (!b) return r; else if (b % 2) mul(r, a); }

using namespace std;

struct HS {
  static constexpr int mlf = 4;
  int cap;
  vector<array<int, 2>> knx;
  vector<int> head;

  HS(): cap{4 * mlf - 1}, head(cap + 1, -1) {
  }

  void emplace(int k) {
    knx.push_back({k, head[k & cap]});
    head[k & cap] = knx.size() - 1;
    maybe_rehash();
  }

  int find(int k) {
    #pragma GCC unroll 2
    for (int i = head[k & cap]; ~i; i = knx[i][1]) if (knx[i][0] == k) return i;
    return -1;
  }

  void clear() {
    knx.clear();
    head.assign(cap + 1, -1);
  }

  void maybe_rehash() {
    if (knx.size() * mlf <= cap) return;
    head.assign((cap += cap + 1) + 1, -1);
    for (int i = 0; i < knx.size(); ++i) {
      knx[i][1] = head[knx[i][0] & cap];
      head[knx[i][0] & cap] = i;
    }
  }
};

vector<HS> high;
vector<int> inva, fa;

vector<char> done;
vector<vector<array<int, 2>>> found;
vector<int> qs;
vector<vector<int>> ans;
vector<int> idxs;
int to_restore;

void gen_high(const vector<int>& prof, int n0, int n, int c, int p) {
  high[n0 - n].emplace(p);
  for (int cc = prof[n]; ++cc <= min(n, c); ) gen_high(prof, n0, n - cc, cc, mul(+p, inva[cc]));
}

void restore_high(const vector<int>& prof, int n0, int n, int c) {
  static int cur[999], sz = 0;
  while (found[n0 - n].size() && found[n0 - n].back()[0] == idxs[n0 - n]) {
    auto [x, i] = found[n0 - n].back();
    ans[i].insert(ans[i].end(), cur, cur + sz);
    found[n0 - n].pop_back();
    if (!--to_restore) return;
  }
  ++idxs[n0 - n];
  for (int cc = prof[n]; ++cc <= min(n, c); ) cur[sz++] = cc, restore_high(prof, n0, n - cc, cc), --sz;
}

void solve(const vector<int>& prof, int n0, int n, int c, int p) {
  static int cur[999], sz = 0;
  for (int i = 0; i < qs.size(); ++i) if (!done[i] && ~high[n0 - n].find(mul(+p, qs[i]))) {
    done[i] = 1;
    ans[i].insert(ans[i].end(), cur, cur + sz);
    found[n0 - n].push_back({high[n0 - n].find(mul(+p, qs[i])), i});
    ++to_restore;
  }
  for (; n + c <= n0 && c <= prof[n + c]; ++c) cur[sz++] = c, solve(prof, n0, n + c, c, mul(+p, fa[c])), --sz;
}

uint64_t eval_high(vector<int> profile) {
  int N = profile.size();
  vector<uint64_t> cnt(N * N);
  cnt[N * N - 1] = 1;
  uint64_t ans = 0;
  for (int n = N; --n; )
  for (int c = N - n; --c > 0 && c > profile[c + n]; ) if (cnt[n * N + c]) {
    for (int cc = min(n, c); cc > profile[n]; cc--) {
      cnt[(n - cc) * N + cc] += cnt[n * N + c];
    }
    ans += cnt[n * N + c];
  }
  return ans;
}

uint64_t eval_low(vector<int> profile) {
  int N = profile.size();
  vector<uint64_t> cnt(N * N);
  cnt[0] = 1;
  uint64_t ans = 0;
  for (int n = 0; n < N; ++n)
  for (int c = 0; c <= n; ++c) if (cnt[n * N + c]) {
    for (int cc = max(c, 1); n + cc < N && cc <= profile[n + cc]; ++cc) {
      cnt[(n + cc) * N + cc] += cnt[n * N + c];
    }
    ans += cnt[n * N + c];
  }
  return ans;
}

uint64_t eval(vector<int> profile) {
  return eval_high(profile) + eval_low(profile) * 2;
}

vector<int> makeprof(int n, vector<int> from) {
  vector<int> prof(n + 1, 0);
  int j = n;
  for (int i = 0; i < from.size() - 1; ++i) {
    while (j >= from[i]) prof[j--] = i;
  }
  while (j >= 0) prof[j--] = n;
  return prof;
}

vector<int> getopt(int n) {
  static vector<vector<int>> precalc = [] {
    int n = 124;
    vector<vector<int>> res(n);
    vector<int> tmpl(10, n);
    for (int i = tmpl.size() - 1; --i; ) tmpl[i] -= (i - 1) * 10;
    while (n-- > 1) {
      int64_t bst = eval(makeprof(n, tmpl));
      for (int ch = 1; ch--; )
      for (int i = tmpl.size() - 1; --i; )
      for (auto d: {-1, 1})
      for (int chch = 1; chch--; ) if (tmpl[i] + d >= 0 && tmpl[i] + d <= n + 1) {
        tmpl[i] += d;
        int64_t cur = eval(makeprof(n, tmpl));
        if (cur > bst) tmpl[i] -= d;
        else if (cur < bst) ch = chch = 1, bst = cur, sort(tmpl.rbegin() + 1, tmpl.rend());
      }
      res[n] = tmpl;
    }
    return res;
  }();
  return makeprof(n, precalc[n]);
}

int main() {
  int tc; cin >> tc;
  qs.resize(tc);
  ans.resize(tc);
  for (int i = 0; i < tc; ++i) {
    cin >> qs[i];
    done.push_back(qs[i] <= 2);
    qs[i] = inv(qs[i]);
  }
  for (int n = 1; !all_of(done.begin(), done.end(), identity{}); ++n) {
    inva.assign(n + 1, 1);
    fa.assign(n + 1, 1);
    for (int i = 1; i <= n; ++i) inva[i] = mul(inv(n - i + 1), inva[i - 1]),
                                 fa[i] = mul(n - i + 1, fa[i - 1]);
    high.resize(n + 1);
    found.resize(n + 1);
    idxs.assign(n + 1, 0);
    for (auto& x: high) x.clear();
    gen_high(getopt(n), n, n, n, 1);
    solve(getopt(n), n, 0, 1, mul(n + 1, n + 2));
    if (to_restore) {
      for (auto& x: found) sort(x.rbegin(), x.rend());
      restore_high(getopt(n), n, n, n);
    }
  }
  for (int i = 0; i < tc; ++i) {
    if (qs[i] == 1) cout << "1\n";
    else {
      cout << accumulate(ans[i].begin(), ans[i].end(), 0) + 2 << "\n1 2\n";
      for (int j = 0, k = 2; j < ans[i].size(); ++j)
      while (ans[i][j]--) cout << j + 1 << ' ' << ++k << '\n';
    }
  }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 23ms
memory: 3724kb

input:

4
2
360
1
509949433

output:

2
1 2
5
1 2
1 3
2 4
2 5
1
10
1 2
1 3
2 4
3 5
4 6
5 7
6 8
7 9
8 10

result:

ok OK (4 test cases)

Test #2:

score: -100
Time Limit Exceeded

input:

9
185396120
468170792
837583517
696626231
338497514
762842660
800028852
928391161
733524004

output:


result: