QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#235325#7613. Inverse ProblemdykwAC ✓33604ms132124kbC++203.3kb2023-11-02 17:14:552023-11-02 17:14:57

Judging History

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

  • [2023-11-02 17:14:57]
  • 评测
  • 测评结果:AC
  • 用时:33604ms
  • 内存:132124kb
  • [2023-11-02 17:14:55]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

constexpr int maxn = 125;
constexpr int mod = (int) 1e9 + 7;

bitset<mod> f;
int t;
int r[10];
bool ok[10];
vector<int> rid;
int w[126][126], iw[126][126], inv[126];
vector<pair<int, int>> g[126], h[126];

void dfs(int x, int used, int res, const int &n) {
  f[res] = 1;
  for (int i = x; i <= min(n - 2, 6); ++i) {
    if (used + i > n - 2) {
      break;
    }
    dfs(i, used + i, 1LL * res * w[n - 2][i] % mod, n);
  }
}

void dfs_b(int x, int used, int res, const int &n) {
  for (int i : rid) {
    if (f[1LL * r[i] * inv[n] % mod * inv[n - 1] % mod * res % mod]) {
      g[used].emplace_back(i, res);
    }
  }
  for (int i = x; i <= n - 2; ++i) {
    if (used + i > n - 2) {
      break;
    }
    dfs_b(i, used + i, 1LL * res * iw[n - 2][i] % mod, n);
  }
}

int ans_n[10];
vector<int> tmp;
vector<int> siz[10];

void dfs_chk(int x, int used, int res, const int &n) {
  for (auto [i, v] : g[n - 2 - used]) {
    if (1LL * res * n % mod * (n - 1) % mod == 1LL * r[i] * v % mod) {
      if (ok[i]) {
        continue;
      }
      ok[i] = true;
      ans_n[i] = n;
      siz[i] = tmp;
      h[n - 2 - used].emplace_back(i, v);
    }
  }
  for (int i = x; i <= min(6, n - 2); ++i) {
    if (used + i > n - 2) {
      break;
    }
    tmp.push_back(i);
    dfs_chk(i, used + i, 1LL * res * w[n - 2][i] % mod, n);
    tmp.pop_back();
  }
}

void dfs_rem(int x, int used, int res, const int &n) {
  for (auto [i, v] : h[used]) {
    if (res == v) {
      for (int u : tmp) {
        siz[i].push_back(u);
      }
    }
  }
  for (int i = x; i <= n - 2; ++i) {
    if (used + i > n - 2) {
      break;
    }
    tmp.push_back(i);
    dfs_rem(i, used + i, 1LL * res * iw[n - 2][i] % mod, n);
    tmp.pop_back();
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  
  cin >> t;
  for (int i = 0; i < t; ++i) {
    cin >> r[i];
    if (r[i] == 1) {
      ans_n[i] = 1;
    } else if (r[i] == 2) {
      ans_n[i] = 2;
    } else {
      rid.push_back(i);
    }
  }
  
  inv[1] = 1;
  for (int i = 2; i <= 125; ++i) {
    inv[i] = 1LL * (mod - mod / i) * inv[mod % i] % mod;
  }
  for (int i = 1; i <= 125; ++i) {
    w[i][0] = iw[i][0] = 1;
    for (int j = 1; j <= i; ++j) {
      w[i][j] = 1LL * w[i][j - 1] * (i - j + 1) % mod;
      iw[i][j] = 1LL * iw[i][j - 1] * inv[i - j + 1] % mod;
    }
  }
  
  auto clk = clock();
  for (int n = 3; n <= maxn; ++n) {
    if (rid.empty()) {
      break;
    }
    f.reset();
    for (int i = 0; i <= n; ++i) {
      g[i].clear();
      h[i].clear();
    }
    dfs(1, 0, 1, n);
    dfs_b(7, 0, 1, n);
    dfs_chk(1, 0, 1, n);
    dfs_rem(7, 0, 1, n);
    for (int i = 0; i < t; ++i) {
      if (ans_n[i] != 0 && find(rid.begin(), rid.end(), i) != rid.end()) {
        rid.erase(find(rid.begin(), rid.end(), i));
      }
    }
  }
  cerr << "Elp: " << clock() - clk << '\n';
  
  for (int i = 0; i < t; ++i) {
    cout << ans_n[i] << '\n';
    if (ans_n[i] == 2) {
      cout << "1 2" << '\n';
    } else if (ans_n[i] != 1) {
      cout << "1 2" << '\n';
      int lst = 2;
      for (int k : siz[i]) {
        int now = lst;
        while (k--) {
          ++now;
          cout << lst << ' ' << now << '\n';
        }
        lst = now; 
      }
    }
  }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 81ms
memory: 125612kb

input:

4
2
360
1
509949433

output:

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

result:

ok OK (4 test cases)

Test #2:

score: 0
Accepted
time: 18952ms
memory: 127444kb

input:

9
185396120
468170792
837583517
696626231
338497514
762842660
800028852
928391161
733524004

output:

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

result:

ok OK (9 test cases)

Test #3:

score: 0
Accepted
time: 33604ms
memory: 132124kb

input:

10
338497514
733524004
447182954
415993605
453460670
50499055
648088551
986982752
907925397
315315230

output:

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

result:

ok OK (10 test cases)

Test #4:

score: 0
Accepted
time: 4370ms
memory: 125808kb

input:

10
1
2
3
4
5
6
7
8
9
10

output:

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

result:

ok OK (10 test cases)

Test #5:

score: 0
Accepted
time: 2027ms
memory: 125868kb

input:

10
269199917
392009324
753889928
751355133
472639410
132096559
331333826
40414701
72847302
475706026

output:

55
1 2
2 3
2 4
2 5
2 6
2 7
2 8
2 9
2 10
2 11
2 12
2 13
2 14
2 15
2 16
2 17
2 18
2 19
2 20
2 21
2 22
2 23
2 24
2 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
2 39
2 40
2 41
2 42
2 43
2 44
2 45
2 46
2 47
2 48
2 49
2 50
2 51
2 52
2 53
2 54
2 55
84
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
...

result:

ok OK (10 test cases)

Extra Test:

score: 0
Extra Test Passed