QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#405472#7613. Inverse Problemucup-team3215AC ✓42999ms1026376kbC++203.0kb2024-05-06 00:01:352024-05-06 00:01:37

Judging History

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

  • [2024-05-06 00:01:37]
  • 评测
  • 测评结果:AC
  • 用时:42999ms
  • 内存:1026376kb
  • [2024-05-06 00:01:35]
  • 提交

answer

#include <algorithm>
#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;

template <typename K, typename V, int mlf = 4>
struct HT {
  int capacity;
  vector<pair<K, int>> knx;
  vector<V> values;
  vector<int> head;

  HT(): capacity{4 * mlf}, head(capacity, -1) {
  }

  void emplace(K k, V v) {
//    if (~find(k)) return;
    knx.push_back({k, head[k & capacity - 1]});
    values.push_back(v);
    head[k & capacity - 1] = knx.size() - 1;
    maybe_rehash();
  }

  int find(K k) {
    for (int i = head[k & capacity - 1]; ~i; i = knx[i].second) if (knx[i].first == k) return i;
    return -1;
  }

  void clear() {
    knx.clear();
    values.clear();
    head.assign(capacity, -1);
  }

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

vector<HT<int, vector<int>>> even(1);
vector<int> inv_(1);

void gen_even(int n0, int n, int c, int p, auto&& cur) {
  even[n0 - n].emplace(p, cur);
  for (; c > 0; c -= 2) if (c <= n) {
    int np = p;
    for (int i = 0; i < c; ++i) mul(np, inv_[n0 - i]);
    cur.push_back(c);
    gen_even(n0, n - c, c, np, cur);
    cur.pop_back();
  }
}

vector<int> qs;
vector<vector<int>> ans;

void solve(int n0, int n, int c, int p, auto&& cur) {
  if (n % 2 == 0)
  for (int i = 0; i < qs.size(); ++i) if (ans[i].empty() && ~even[n].find(mul(+p, qs[i]))) {
    ans[i] = cur;
    auto& t = even[n].values[even[n].find(mul(+p, qs[i]))];
    ans[i].insert(ans[i].end(), t.begin(), t.end());
  }
  for (; c > 0; c -= 2) if (c <= n) {
    int np = p;
    for (int i = 0; i < c; ++i) mul(np, n0 - i);
    cur.push_back(c);
    solve(n0, n - c, c, np, cur);
    cur.pop_back();
  }
}

int main() {
  int tc; cin >> tc;
  qs.resize(tc);
  ans.resize(tc);
  for (int i = 0; i < tc; ++i) {
    cin >> qs[i];
    if (qs[i] == 1) ans[i] = {-1};
    else if (qs[i] == 2) ans[i] = {-1};
    qs[i] = inv(qs[i]);
  }
  for (int n = 1, done = 0; !done++; ++n) {
    inv_.push_back(inv(n));
    even.push_back({});
    for (auto& x: even) x.clear();
    gen_even(n, n, n + n % 2, 1, vector<int>{});
    solve(n, n, n - n % 2 + 1, mul(n + 1, n + 2), vector<int>{});
    for (int i = 0; i < tc; ++i) done &= !ans[i].empty();
  }
  for (int i = 0; i < tc; ++i) {
    if (qs[i] == 1) cout << "1\n";
    else {
      if (ans[i][0] == -1) ans[i] = {};
      cout << accumulate(ans[i].begin(), ans[i].end(), 0) + 2 << '\n' << "1 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: 0ms
memory: 3548kb

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: 0
Accepted
time: 38534ms
memory: 1026376kb

input:

9
185396120
468170792
837583517
696626231
338497514
762842660
800028852
928391161
733524004

output:

14
1 2
1 3
2 4
3 5
4 6
5 7
6 8
7 9
8 10
9 11
9 12
10 13
10 14
122
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1 22
1 23
1 24
1 25
1 26
1 27
1 28
1 29
1 30
1 31
2 32
2 33
2 34
2 35
2 36
2 37
2 38
2 39
2 40
2 41
2 42
2 43
2 44
3 45
3 46
3 47
3 48
3 49
3 ...

result:

ok OK (9 test cases)

Test #3:

score: 0
Accepted
time: 42999ms
memory: 1018392kb

input:

10
338497514
733524004
447182954
415993605
453460670
50499055
648088551
986982752
907925397
315315230

output:

124
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1 22
1 23
1 24
1 25
1 26
1 27
2 28
2 29
2 30
2 31
2 32
2 33
2 34
2 35
2 36
2 37
2 38
2 39
2 40
2 41
2 42
2 43
2 44
2 45
2 46
2 47
2 48
2 49
2 50
3 51
3 52
3 53
3 54
3 55
3 56
3 57
3 58
3 59
3 60
3 61
3 62...

result:

ok OK (10 test cases)

Test #4:

score: 0
Accepted
time: 6494ms
memory: 227004kb

input:

10
1
2
3
4
5
6
7
8
9
10

output:

1
2
1 2
102
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1 22
1 23
1 24
1 25
2 26
2 27
2 28
2 29
2 30
2 31
2 32
2 33
2 34
2 35
2 36
2 37
2 38
3 39
3 40
3 41
3 42
3 43
3 44
3 45
3 46
3 47
4 48
5 49
6 50
7 51
7 52
7 53
7 54
7 55
7 56
7 57
7 58
7 59
7 60
7...

result:

ok OK (10 test cases)

Test #5:

score: 0
Accepted
time: 1504ms
memory: 67052kb

input:

10
269199917
392009324
753889928
751355133
472639410
132096559
331333826
40414701
72847302
475706026

output:

55
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1 22
1 23
1 24
1 25
1 26
1 27
1 28
1 29
1 30
1 31
1 32
1 33
1 34
1 35
1 36
1 37
1 38
1 39
1 40
1 41
1 42
1 43
1 44
1 45
1 46
1 47
1 48
1 49
1 50
1 51
1 52
1 53
1 54
1 55
84
1 2
1 3
1 4
1 5
2 6
2 7
2 8
3 9
...

result:

ok OK (10 test cases)

Extra Test:

score: 0
Extra Test Passed