QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#400509#8325. 重塑时光skittles14120 652ms12588kbC++176.9kb2024-04-27 13:03:582024-04-27 13:03:59

Judging History

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

  • [2024-04-27 13:03:59]
  • 评测
  • 测评结果:0
  • 用时:652ms
  • 内存:12588kb
  • [2024-04-27 13:03:58]
  • 提交

answer

#include "bits/extc++.h"

using namespace std;

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
    cerr << t;
    ((cerr << " | " << u), ...);
    cerr << endl;
}

#ifdef DEBUG
#define dbg(...)                                           \
    cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
         << ": ";                                          \
    dbgh(__VA_ARGS__)
#else
#define cerr   \
    if (false) \
    cerr
#define dbg(...)
#endif

#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))

template <typename T>
ostream& operator<<(ostream& out, const vector<T>& arr) {
    out << "[";
    for (int i = 0; i < sz(arr); i++) {
        if (i) {
            out << ", ";
        }
        out << arr[i];
    }
    return out << "]";
}

bool on(int mask, int bit) {
    return (mask >> bit) & 1;
}

constexpr int MOD = 1e9 + 7;

struct mint {
    static int norm(long x) {
        x %= MOD;
        if (x < 0) {
            x += MOD;
        }
        return x;
    }

    static mint pow(mint base, long exp) {
        mint ans = 1;

        while (exp) {
            if (exp & 1) {
                ans *= base;
            }
            base *= base;
            exp >>= 1;
        }

        return ans;
    }

    static mint inv(mint x) {
        return mint::pow(x, MOD - 2);
    }

    int x;

    mint() : x(0) {}
    mint(long x) : x(norm(x)) {}

    friend ostream& operator<<(ostream& out, const mint& m) {
        return out << m.x;
    }

    static mint from_unchecked(int x) {
        mint m;
        m.x = x;
        return m;
    }

    mint operator-() const {
        if (!x) {
            return {};
        }
        return mint::from_unchecked(MOD - x);
    }

    mint& operator+=(const mint& m) {
        x += m.x;
        if (x >= MOD) {
            x -= MOD;
        }
        return *this;
    }
    mint& operator-=(const mint& m) {
        return *this += -m;
    }

    mint& operator*=(const mint& m) {
        x = int(long(x) * m.x % MOD);
        return *this;
    }
    mint& operator/=(const mint& m) {
        return *this *= mint::inv(m);
    }

    friend mint operator+(mint a, const mint& b) {
        return a += b;
    }
    friend mint operator-(mint a, const mint& b) {
        return a -= b;
    }
    friend mint operator*(mint a, const mint& b) {
        return a *= b;
    }
    friend mint operator/(mint a, const mint& b) {
        return a /= b;
    }
};

struct M {
    static constexpr int MAXN = 100;

    mint fact[MAXN], ifact[MAXN];

    M() {
        fact[0] = ifact[0] = 1;
        for (int i = 1; i < MAXN; i++) {
            fact[i] = fact[i - 1] * i;
        }
        ifact[MAXN - 1] = mint::inv(fact[MAXN - 1]);
        for (int i = MAXN - 2; i >= 1; i--) {
            ifact[i] = (i + 1) * ifact[i + 1];
        }
    }

    mint choose(int n, int k) const {
        return fact[n] * ifact[k] * ifact[n - k];
    }

    mint choose2(int n, int k) const {
        if (n < 0 || k < 0 || k > n) {
            return 0;
        }
        return choose(n, k);
    }

    mint sab2(int n, int s) const {
        return choose2(s + n - 1, s);
    }
} M;

struct Timer {
    clock_t start = clock();

    double elapsed_secs() const {
        return double(clock() - start) / CLOCKS_PER_SEC;
    }
};

constexpr int MAXN = 15;
long dp_ord[1 << MAXN] {}, dp_split[1 << MAXN][MAXN + 1] {},
    dp[1 << MAXN][MAXN + 1] {};

void solve() {
    Timer timer;
    int n, m, kv;
    cin >> n >> m >> kv;

    int graph[n] {};
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        graph[u] |= 1 << v;
    }

    int graph_u[1 << n] {};

    for (int i = 0; i < n; i++) {
        graph_u[1 << i] = graph[i];
    }
    for (int mask = 0; mask < (1 << n); mask++) {
        for (int i = 0; i < n; i++) {
            if (on(mask, i)) {
                graph_u[mask] |= graph_u[mask ^ (1 << i)];
            }
        }
    }

    // # of ways to permute submask to get dag in induced subgraph
    dp_ord[0] = 1;
    for (int mask = 1; mask < (1 << n); mask++) {
        for (int i = 0; i < n; i++) {
            if (!on(mask, i)) {
                continue;
            }

            int omask = mask ^ (1 << i);
            if (on(graph_u[omask], i)) {
                continue;
            }

            dp_ord[mask] += dp_ord[omask];
        }
        dp_ord[mask] %= MOD;
        dbg(mask, dp_ord[mask]);
    }

    // # of ways to split mask into submasks so mutually disjoint
    dp_split[0][0] = 1;
    for (int mask = 1; mask < (1 << n); mask++) {
        int m_desc = __builtin_ctz(mask), m_pop = __builtin_popcount(mask);

        for (int c1 = 1; c1 <= m_pop; c1++) {
            for (int smask = mask; smask; smask = (smask - 1) & mask) {
                int omask = mask ^ smask;
                if (!on(smask, m_desc) || (graph_u[omask] & smask) ||
                    (graph_u[smask] & omask)) {
                    continue;
                }

                dp_split[mask][c1] = (dp_split[mask][c1] +
                                      dp_ord[smask] * dp_split[omask][c1 - 1]) %
                                     MOD;
            }
            dbg(mask, c1, dp_split[mask][c1]);
        }
    }
    cout << timer.elapsed_secs() << endl;

    for (int blocks = 1; blocks <= n; blocks++) {
        int empty = kv + 1 - blocks;
        if (empty < 0) {
            continue;
        }

        mint cans = 1;

        cans *= M.fact[blocks];
        cans *= M.sab2(blocks + 1, kv - (blocks - 1));
        dbg(blocks, dp[0][blocks]);
        dp[0][blocks] = cans.x;
    }

    for (int mask = 1; mask < (1 << n); mask++) {
        int m_pop = __builtin_popcount(mask);

        for (int smask = mask; smask; smask = (smask - 1) & mask) {
            int omask = mask ^ smask;
            if (graph_u[omask] & smask) {
                continue;
            }

            for (int c1 = 0; c1 <= n - m_pop; c1++) {
                long& cans = dp[mask][c1];

                int c_pop = __builtin_popcount(smask),
                    o_pop = n - __builtin_popcount(omask),
                    mx = min(o_pop - c1, c_pop);
                for (int c2 = 1; c2 <= mx; c2 += 2) {
                    cans =
                        (cans + dp_split[smask][c2] * dp[omask][c1 + c2]) % MOD;
                }
                for (int c2 = 2; c2 <= mx; c2 += 2) {
                    cans =
                        (cans - dp_split[smask][c2] * dp[omask][c1 + c2]) % MOD;
                }
            }
        }
    }

    cout << dp[(1 << n) - 1][0] * M.ifact[n] / M.sab2(n + 1, kv) << endl;
}

int main() {
    cin.tie(nullptr);
    cin.exceptions(ios::failbit);
    ios_base::sync_with_stdio(false);
    solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 5996kb

input:

3 2 0
1 2
1 3

output:

0
333333336

result:

wrong answer 1st lines differ - expected: '333333336', found: '0'

Test #2:

score: 0
Wrong Answer
time: 1ms
memory: 6068kb

input:

5 7 5
1 4
2 3
1 2
4 5
2 5
2 4
1 5

output:

0
895039689

result:

wrong answer 1st lines differ - expected: '895039689', found: '0'

Test #3:

score: 0
Wrong Answer
time: 72ms
memory: 10124kb

input:

13 12 13
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13

output:

0.018294
76923078

result:

wrong answer 1st lines differ - expected: '76923078', found: '0.018294'

Test #4:

score: 0
Wrong Answer
time: 261ms
memory: 8512kb

input:

14 13 14
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

output:

0.062658
535714290

result:

wrong answer 1st lines differ - expected: '535714290', found: '0.062658'

Test #5:

score: 0
Wrong Answer
time: 147ms
memory: 8760kb

input:

14 13 0
2 9
1 2
1 3
3 8
6 11
2 7
1 5
5 12
2 13
3 14
3 10
3 6
2 4

output:

0.049418
700595243

result:

wrong answer 1st lines differ - expected: '700595243', found: '0.049418'

Test #6:

score: 0
Wrong Answer
time: 131ms
memory: 8588kb

input:

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

output:

0.049744
465070080

result:

wrong answer 1st lines differ - expected: '465070080', found: '0.049744'

Test #7:

score: 0
Wrong Answer
time: 112ms
memory: 9992kb

input:

13 0 13

output:

0.034776
1

result:

wrong answer 1st lines differ - expected: '1', found: '0.034776'

Test #8:

score: 0
Wrong Answer
time: 57ms
memory: 9232kb

input:

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

output:

0.043255
4362623

result:

wrong answer 1st lines differ - expected: '4362623', found: '0.043255'

Test #9:

score: 0
Wrong Answer
time: 1ms
memory: 6016kb

input:

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

output:

0
426526937

result:

wrong answer 1st lines differ - expected: '426526937', found: '0'

Test #10:

score: 0
Wrong Answer
time: 1ms
memory: 6100kb

input:

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

output:

0
214820829

result:

wrong answer 1st lines differ - expected: '214820829', found: '0'

Test #11:

score: 0
Wrong Answer
time: 22ms
memory: 7116kb

input:

13 68 0
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
2 5
2 6
2 7
2 8
2 9
2 10
2 11
2 12
2 13
3 4
3 5
3 6
3 7
3 8
3 9
3 10
3 11
3 12
3 13
4 5
4 6
4 7
4 8
4 9
4 10
4 11
4 12
4 13
5 6
5 7
5 8
5 9
5 10
5 11
5 12
5 13
6 7
6 8
6 9
6 10
6 11
6 12
6 13
7 8
7 10
7 11
7 12
7 13
8 13
9 10
9 11
9 12
9 13...

output:

0.013978
65784302

result:

wrong answer 1st lines differ - expected: '65784302', found: '0.013978'

Test #12:

score: 0
Wrong Answer
time: 30ms
memory: 7172kb

input:

13 39 13
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
3 4
3 5
3 6
3 7
3 8
3 9
3 10
3 11
3 12
3 13
4 8
4 9
4 10
4 12
4 13
5 6
5 7
5 10
5 11
5 13
6 7
6 11
6 13
8 9
8 12
8 13
11 13

output:

0.013051
361557272

result:

wrong answer 1st lines differ - expected: '361557272', found: '0.013051'

Test #13:

score: 0
Wrong Answer
time: 160ms
memory: 9484kb

input:

14 11 14
2 13
4 11
4 14
5 14
6 9
6 14
7 11
9 14
10 12
10 14
12 14

output:

0.051589
696132576

result:

wrong answer 1st lines differ - expected: '696132576', found: '0.051589'

Test #14:

score: 0
Wrong Answer
time: 82ms
memory: 8584kb

input:

14 46 14
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
2 9
2 10
2 11
2 13
2 14
3 4
3 6
3 10
3 11
3 12
3 13
3 14
4 6
4 10
4 11
4 12
4 13
4 14
5 7
5 8
5 9
5 11
5 13
5 14
7 8
7 9
7 14
10 11
10 13
10 14
11 13
11 14
13 14

output:

0.043258
258614192

result:

wrong answer 1st lines differ - expected: '258614192', found: '0.043258'

Test #15:

score: 0
Wrong Answer
time: 61ms
memory: 12220kb

input:

14 70 14
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
2 3
2 4
2 5
2 7
2 8
2 9
2 10
2 11
2 12
2 13
2 14
3 11
3 13
3 14
4 5
4 7
4 8
4 10
4 11
4 12
4 13
4 14
5 7
5 8
5 10
5 12
5 13
5 14
6 7
6 8
6 9
6 10
6 11
6 12
6 13
6 14
7 8
7 10
7 12
7 13
7 14
8 10
8 12
8 13
8 14
9 11
9 12
9 13
9 14
10 1...

output:

0.045723
209616080

result:

wrong answer 1st lines differ - expected: '209616080', found: '0.045723'

Test #16:

score: 0
Wrong Answer
time: 66ms
memory: 12244kb

input:

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

output:

0.044863
701202127

result:

wrong answer 1st lines differ - expected: '701202127', found: '0.044863'

Test #17:

score: 0
Wrong Answer
time: 208ms
memory: 9384kb

input:

14 15 14
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
4 6
5 9

output:

0.051817
263013541

result:

wrong answer 1st lines differ - expected: '263013541', found: '0.051817'

Test #18:

score: 0
Wrong Answer
time: 652ms
memory: 12512kb

input:

15 17 15
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
3 13
3 15
13 15

output:

0.175786
963180835

result:

wrong answer 1st lines differ - expected: '963180835', found: '0.175786'

Test #19:

score: 0
Wrong Answer
time: 537ms
memory: 12588kb

input:

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

output:

0.15534
722208430

result:

wrong answer 1st lines differ - expected: '722208430', found: '0.15534'

Test #20:

score: 0
Wrong Answer
time: 607ms
memory: 12448kb

input:

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

output:

0.170588
560801339

result:

wrong answer 1st lines differ - expected: '560801339', found: '0.170588'