QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#73359#7. 主旋律slongle100 ✓154ms48508kbC++143.3kb2023-01-24 04:01:252023-01-24 04:01:27

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-01-24 04:01:27]
  • 评测
  • 测评结果:100
  • 用时:154ms
  • 内存:48508kb
  • [2023-01-24 04:01:25]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
// typedef __int128 ll;
typedef double ld;
const int mod = 1e9 + 7;
const int MAXN = 18;

ll pmod(ll v, ll p = mod) {
    v %= p;
    if (v < 0) {
        v += p;
    }
    return v;
}

ll power(ll a, ll b, ll p = mod) {
    ll c = 1;
    for (; b; b >>= 1) {
        if (b & 1) {
            c = pmod(c * a, p);
        }
        a = pmod(a * a, p);
    }
    return c;
}

ll inv(ll a) { return power(a, mod - 2); }

int e1[MAXN][1 << MAXN]; // e1[i][s] = i -> s
int e4[MAXN][1 << MAXN]; // e4[i][s] = s -> i
int e2[1 << MAXN];       // e2[s] = s -> s
int e3[1 << MAXN];       // e3[t] = s\t -> t
int G[MAXN][MAXN];
int popc[1 << MAXN];
ll f[1 << MAXN], g[1 << MAXN];
ll bas2[MAXN * MAXN * 2];
int root2[1 << MAXN];

void solve(int cas) {
    int n, m;
    cin >> n >> m;
    popc[0] = 0;
    for (int i = 1; i < (1 << n); i++) {
        popc[i] = popc[i ^ (i & (-i))] + 1;
    }
    for (int i = 0; i < n; i++) {
        root2[1 << i] = i;
    }
    memset(G, 0, sizeof(G));
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        G[u][v] = 1;
    }
    memset(e1, 0, sizeof(e1));
    for (int i = 0; i < n; i++) {
        int s = 0;
        for (int j = 0; j < n; j++) {
            if (G[i][j]) {
                s |= (1 << j);
            }
        }
        for (int t = 0; t < (1 << n); t++) {
            e1[i][t] = popc[t & s];
        }
    }
    memset(e4, 0, sizeof(e4));
    for (int i = 0; i < n; i++) {
        e4[i][0] = 0;
        for (int s = 1; s < (1 << n); s++) {
            e4[i][s] = e4[i][s ^ (s & (-s))] + G[root2[s & (-s)]][i];
        }
    }
    memset(e2, 0, sizeof(e2));
    for (int i = 0; i < (1 << n); i++) {
        for (int j = 0; j < n; j++) {
            if (i & (1 << j)) {
                e2[i] += e1[j][i];
            }
        }
    }
    bas2[0] = 1;
    for (int i = 1; i <= m * 2; i++) {
        bas2[i] = pmod(bas2[i - 1] * 2);
    }
    vector<int> states;
    states.reserve(1 << n);
    for (int s = 1; s < (1 << n); s++) {
        if (popc[s] == 1) {
            f[s] = g[s] = 1;
            continue;
        }
        states.clear();
        for (int t = s; t > 0; t = (t - 1) & s) {
            states.push_back(t);
        }
        int len = states.size();
        e3[0] = 0;
        for (int i = len - 1; i >= 0; i--) {
            int t = states[i];
            int lb = t & (-t);
            e3[t] = e3[t ^ lb] - e1[root2[lb]][t ^ lb] + e4[root2[lb]][s ^ t];
        }
        g[s] = 0;
        int ux = s & (-s);
        for (int t = (s - 1) & s; t > 0; t = (t - 1) & s) {
            if (t & ux) {
                g[s] -= pmod(g[s ^ t] * f[t]);
            }
        }
        g[s] = pmod(g[s]);
        f[s] = 0;
        for (int t = s; t > 0; t = (t - 1) & s) {
            f[s] += pmod(bas2[e2[s ^ t] + e3[t]] * g[t]);
        }
        f[s] = pmod(bas2[e2[s]] - f[s]);
        g[s] = pmod(g[s] + f[s]);
    }
    cout << f[(1 << n) - 1] << endl;
}

int main() {
    ios::sync_with_stdio(false);
    // freopen("hidden.in", "r", stdin);
    // freopen("hidden.out", "w", stdout);
    int T = 1, cas = 0;
    // cin >> T;
    while (T--) {
        solve(++cas);
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 10
Accepted
time: 0ms
memory: 47864kb

input:

5 15
4 3
4 2
2 5
2 1
1 2
5 1
3 2
4 1
1 4
5 4
3 4
5 3
2 3
1 5
3 1

output:

9390

result:

ok single line: '9390'

Test #2:

score: 10
Accepted
time: 1ms
memory: 47420kb

input:

5 18
4 3
4 2
2 5
2 1
1 2
5 1
3 2
4 1
1 4
5 4
3 4
5 3
2 3
1 5
3 1
1 3
5 2
2 4

output:

100460

result:

ok single line: '100460'

Test #3:

score: 10
Accepted
time: 2ms
memory: 47988kb

input:

8 35
5 1
8 7
7 8
7 6
6 1
2 5
6 5
8 2
7 2
7 5
3 1
6 3
2 3
5 2
8 5
8 3
6 8
2 1
1 6
2 6
7 3
2 4
3 5
3 2
3 7
7 1
8 4
3 4
3 6
6 4
2 7
4 6
6 7
7 4
8 1

output:

299463717

result:

ok single line: '299463717'

Test #4:

score: 10
Accepted
time: 5ms
memory: 47772kb

input:

8 40
5 1
8 7
7 8
7 6
6 1
2 5
6 5
8 2
7 2
7 5
3 1
6 3
2 3
5 2
8 5
8 3
6 8
2 1
1 6
2 6
7 3
2 4
3 5
3 2
3 7
7 1
8 4
3 4
3 6
6 4
2 7
4 6
6 7
7 4
8 1
1 2
4 8
5 8
4 3
5 7

output:

21156439

result:

ok single line: '21156439'

Test #5:

score: 10
Accepted
time: 0ms
memory: 47144kb

input:

8 45
5 1
8 7
7 8
7 6
6 1
2 5
6 5
8 2
7 2
7 5
3 1
6 3
2 3
5 2
8 5
8 3
6 8
2 1
1 6
2 6
7 3
2 4
3 5
3 2
3 7
7 1
8 4
3 4
3 6
6 4
2 7
4 6
6 7
7 4
8 1
1 2
4 8
5 8
4 3
5 7
2 8
1 5
3 8
1 3
4 1

output:

426670664

result:

ok single line: '426670664'

Test #6:

score: 10
Accepted
time: 10ms
memory: 46888kb

input:

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

output:

931896041

result:

ok single line: '931896041'

Test #7:

score: 10
Accepted
time: 2ms
memory: 46528kb

input:

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

output:

303656759

result:

ok single line: '303656759'

Test #8:

score: 10
Accepted
time: 146ms
memory: 46884kb

input:

15 130
7 10
9 12
4 6
1 10
14 9
4 8
8 9
4 3
15 9
3 9
1 8
2 15
8 4
13 7
3 5
14 13
6 2
14 6
8 3
4 2
8 13
9 2
6 13
12 11
6 4
11 8
15 5
3 8
10 8
15 7
15 6
12 15
8 12
13 9
12 9
8 15
11 6
6 7
10 4
2 8
11 12
7 9
7 12
14 1
5 8
10 9
3 7
7 13
11 9
11 10
1 5
1 3
2 1
2 7
10 1
10 15
7 14
5 6
6 1
15 10
5 15
15 8
5...

output:

717458968

result:

ok single line: '717458968'

Test #9:

score: 10
Accepted
time: 154ms
memory: 48508kb

input:

15 140
7 10
9 12
4 6
1 10
14 9
4 8
8 9
4 3
15 9
3 9
1 8
2 15
8 4
13 7
3 5
14 13
6 2
14 6
8 3
4 2
8 13
9 2
6 13
12 11
6 4
11 8
15 5
3 8
10 8
15 7
15 6
12 15
8 12
13 9
12 9
8 15
11 6
6 7
10 4
2 8
11 12
7 9
7 12
14 1
5 8
10 9
3 7
7 13
11 9
11 10
1 5
1 3
2 1
2 7
10 1
10 15
7 14
5 6
6 1
15 10
5 15
15 8
5...

output:

459157220

result:

ok single line: '459157220'

Test #10:

score: 10
Accepted
time: 145ms
memory: 47956kb

input:

15 150
7 10
9 12
4 6
1 10
14 9
4 8
8 9
4 3
15 9
3 9
1 8
2 15
8 4
13 7
3 5
14 13
6 2
14 6
8 3
4 2
8 13
9 2
6 13
12 11
6 4
11 8
15 5
3 8
10 8
15 7
15 6
12 15
8 12
13 9
12 9
8 15
11 6
6 7
10 4
2 8
11 12
7 9
7 12
14 1
5 8
10 9
3 7
7 13
11 9
11 10
1 5
1 3
2 1
2 7
10 1
10 15
7 14
5 6
6 1
15 10
5 15
15 8
5...

output:

663282473

result:

ok single line: '663282473'