QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#276606#7613. Inverse ProblemAllTheWayNorthAC ✓27653ms107208kbC++144.1kb2023-12-05 23:42:212023-12-05 23:42:26

Judging History

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

  • [2023-12-05 23:42:26]
  • 评测
  • 测评结果:AC
  • 用时:27653ms
  • 内存:107208kb
  • [2023-12-05 23:42:21]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int M = 150;
int power(int a, int b) {
    long long res = a, ans = 1;
    for (; b; b >>= 1, res = res * res % mod) if (b & 1) ans = ans * res % mod;
    return ans;
}

int qry[15], ans[15];
vector < pair <int, int> > eans[15];

struct node {
    int n;
    vector <int> L[M], R[M];
    int f[M];
    void build(int N) {
        memset(f, 0, sizeof f);
        for (int i = 0; i < M; i++) L[i].clear(), R[i].clear();
        n = N;
        f[0] = 1;
        for (int i = 1; i <= n - 2; i++) f[i] = 1ll * f[i - 1] * (n - i - 1) % mod;
        L[0].push_back(1);
        R[0].push_back(1);
        int l = 1, r = n - 2;
        while (l <= r) {
            int suml = 0, sumr = 0;
            for (int i = 0; i <= n - 2; i++) suml += (n - 2 - i + l) / l * L[i].size();
            for (int i = 0; i <= n - 2; i++) sumr += (n - 2 - i + r) / r * R[i].size();
            if (suml < sumr) {
                int inv = power(f[l], mod - 2);
                for (int i = 0; i + l <= n - 2; i++) {
                    for (auto j : L[i]) L[i + l].push_back(1ll * j * inv % mod);
                }
                l++;
            }
            else {
                for (int i = 0; i + r <= n - 2; i++) {
                    for (auto j : R[i]) R[i + r].push_back(1ll * j * f[r] % mod), sumr++;
                }
                r--;
            }
        }
        for (int i = 0; i <= n - 2; i++) {
            sort(L[i].begin(), L[i].end());
            L[i].resize(unique(L[i].begin(), L[i].end()) - L[i].begin());
        }
        for (int i = 0; i <= n - 2; i++) {
            sort(R[i].begin(), R[i].end());
            R[i].resize(unique(R[i].begin(), R[i].end()) - R[i].begin());
        }
    }
    vector < pair <int, int> > query(int v) {
        v = 1ll * v * power(1ll * n * (n - 1) % mod, mod - 2) % mod;
        for (int i = 0; i <= n - 2; i++) {
            for (auto j : L[i]) {
                int cur = 1ll * v * j % mod;
                auto ite = lower_bound(R[n - 2 - i].begin(), R[n - 2 - i].end(), cur);
                if (ite == R[n - 2 - i].end() || *ite != cur) continue;
                vector < pair <int, int> > ans{{1, 2}};
                int p = 2, tmp = j, rst = i;
                for (int t = 1; t <= rst; t++) {
                    while (rst >= t) {
                        int nxt = 1ll * tmp * f[t] % mod;
                        auto ite = lower_bound(L[rst - t].begin(), L[rst - t].end(), nxt);
                        if (ite == L[rst - t].end() || *ite != nxt) break;
                        rst -= t, tmp = nxt;
                        for (int x = 1; x <= t; x++) ans.push_back({p, p + x});
                        p += t;
                    }
                }
                assert(tmp == 1 && rst == 0);
                tmp = cur, rst = n - 2 - i;
                for (int t = 1; t <= rst; t++) {
                    while (rst >= t) {
                        int nxt = 1ll * tmp * power(f[t], mod - 2) % mod;
                        auto ite = lower_bound(R[rst - t].begin(), R[rst - t].end(), nxt);
                        if (ite == R[rst - t].end() || *ite != nxt) break;
                        rst -= t, tmp = nxt;
                        for (int x = 1; x <= t; x++) ans.push_back({p, p + x});
                        p += t;
                    }
                }
                assert(tmp == 1 && rst == 0);
                fflush(stdout);
                return ans;
            }
        }
        return {};
    }
} a;

int main() {
    int T;
    scanf("%d", &T);
    int rst = T;
    for (int i = 1; i <= T; i++) {
        scanf("%d", qry + i);
        if (qry[i] == 1) ans[i] = 1, rst--;
    }
    for (int i = 2; rst; i++) {
        a.build(i);
        for (int i = 1; i <= T; i++) if (ans[i] == 0) {
            eans[i] = a.query(qry[i]);
            if (eans[i].size()) ans[i] = eans[i].size() + 1, rst--;
        }
    }
    for (int i = 1; i <= T; i++) {
        printf("%d\n", ans[i]);
        for (auto j : eans[i]) printf("%d %d\n", j.first, j.second);
    }
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3704kb

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: 19614ms
memory: 103416kb

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
7 9
9 10
9 11
11 12
11 13
13 14
13 15
13 16
16 17
16 18
16 19
19 20
19 21
19 22
22 23
22 24
22 25
22 26
26 27
26 28
26 29
26 30
30 31
30 32
30 33
30 34
30 35
35 36
35 37
35 38
35 39
35 40
35 41
35 42
42 4...

result:

ok OK (9 test cases)

Test #3:

score: 0
Accepted
time: 27653ms
memory: 107208kb

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
12 14
14 15
14 16
14 17
17 18
17 19
17 20
17 21
17 22
22 23
22 24
22 25
22 26
22 27
22 28
28 29
28 30
28 31
28 32
28 33
28 34
28 35
35 36
35 37
35 38
35 39
35 40
35 41
35 42
42 43
42 44
42 45
42 46
42 47
42 48
42 49
49 50
49 51
49 52
49 53
4...

result:

ok OK (10 test cases)

Test #4:

score: 0
Accepted
time: 3377ms
memory: 21628kb

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: 924ms
memory: 10288kb

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