QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#54274#856. Cactusckiseki#AC ✓1442ms55296kbC++2.7kb2022-10-07 18:34:212022-10-07 18:34:22

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-10-07 18:34:22]
  • 评测
  • 测评结果:AC
  • 用时:1442ms
  • 内存:55296kb
  • [2022-10-07 18:34:21]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int mod = 1000000007;

int modmul(int64_t a, int64_t b) {
    return static_cast<int>(a * b % mod);
}
int modadd(int a, int b) {
    return a + b >= mod ? a + b - mod : a + b;
}
int modsub(int a, int b) {
    return a - b < 0 ? a - b + mod : a - b;
}

int modpow(int e, int p) {
    int r = 1;
    while (p) {
        if (p & 1) r = modmul(r, e);
        e = modmul(e, e);
        p >>= 1;
    }
    return r;
}

signed main() {
    cin.tie(nullptr) -> sync_with_stdio(false);
    int T;
    cin >> T;
    while (T--) {
        int N, M, K;
        cin >> N >> M >> K;

        vector<int> inv(M + 1), fac(M + 1), ifac(M + 1);
        inv[1] = 1;
        for (int i = 2; i <= M; i++)
            inv[i] = modmul(inv[mod % i], (mod - mod / i));
        fac[0] = ifac[0] = 1;
        for (int i = 1; i <= M; i++) {
            fac[i] = modmul(fac[i-1], i);
            ifac[i] = modmul(ifac[i-1], inv[i]);
        }

        vector<vector<int>> g(N);
        for (int i = 0; i < M; i++) {
            int u, v;
            cin >> u >> v;
            --u, --v;
            g[u].emplace_back(v);
            g[v].emplace_back(u);
        }

        int cycle_cnt = 0;
        vector<int> vis(N);
        vector<int> dep(N);
        vector<int> cycle;
        const auto dfs = [&](auto self, int i, int f) -> void {
            vis[i] = true;
            for (int j: g[i]) {
                if (not vis[j]) {
                    dep[j] = dep[i] + 1;
                    self(self, j, i);
                } else if (j != f && dep[j] < dep[i]) {
                    cycle.emplace_back(dep[i] - dep[j] + 1);
                    cycle_cnt += cycle.back();
                }
            }
        };

        dfs(dfs, 0, -1);
        const int bridge = M - cycle_cnt;

        const int invk = modpow(K, mod - 2);
        const auto C = [&](int n, int k) {
            if (k < 0 || n < k) return 0;
            return modmul(fac[n], modmul(ifac[k], ifac[n-k]));
        };
        int ans = 1;
        for (int sz: cycle) {
            // cerr << "sz = " << sz << endl;
            int sum = 0;
            int prod = 1;
            for (int i = 0; i <= sz; i++) {
                if (i % 2 == 0)
                    sum = modadd(sum, modmul(prod, C(sz, i)));
                else
                    sum = modsub(sum, modmul(prod, C(sz, i)));
                if (i <= sz - 2) {
                    prod = modmul(prod, invk);
                }
            }
            ans = modmul(ans, sum);
        }
        // cerr << "bridge = " << bridge << '\n';
        for (int i = 0; i < bridge; i++) {
            int sum = modsub(1, invk);
            ans = modmul(ans, sum);
        }
        ans = modmul(ans, modpow(K, N));
        cout << ans << '\n';
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3552kb

input:

2
2 1 100
1 2
6 7 3
1 2
2 3
3 1
4 5
5 6
6 4
1 4

output:

9900
24

result:

ok 2 number(s): "9900 24"

Test #2:

score: 0
Accepted
time: 124ms
memory: 3680kb

input:

50000
9 10 4
4 7
5 2
1 5
7 3
9 6
8 3
3 2
9 1
4 8
6 2
10 11 4
4 1
1 3
5 1
10 9
8 4
1 6
7 9
7 10
8 1
1 9
10 2
10 10 4
7 5
6 9
5 1
9 7
10 9
4 9
5 10
2 3
3 7
3 8
9 10 4
3 9
3 7
5 4
6 2
1 9
6 5
4 2
9 8
5 1
7 8
9 9 4
9 4
4 1
6 3
8 7
2 9
6 7
1 8
6 9
5 2
10 11 4
7 8
6 2
9 10
7 2
2 4
4 7
3 7
3 1
10 6
6 9
5 1...

output:

15120
34992
61236
15876
19764
34992
19692
34992
52488
19440
19764
34992
19692
13608
13608
52488
19692
13608
40824
34992
17496
40824
19656
52488
19764
13176
34992
59040
19692
34992
52488
13176
19656
34992
19680
599760
52488
34992
61236
19440
58320
11664
34992
40824
20412
34992
20412
34992
61236
34992...

result:

ok 50000 numbers

Test #3:

score: 0
Accepted
time: 1442ms
memory: 55296kb

input:

10
300000 344504 711589813
136663 59111
262959 256239
220957 296457
132870 53422
167951 237433
252790 102337
18228 30482
162993 268323
127652 185288
133496 174755
122093 241171
165750 24398
4524 236165
261647 83998
127329 221453
263837 257156
263948 122651
142880 167089
203580 26970
4992 84305
11692...

output:

46959312
961402883
2
219976660
721840853
507095342
747233052
107251856
932546015
975072100

result:

ok 10 numbers