QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#874968#9735. Japanese BandsAndyqian7WA 1688ms3584kbC++263.1kb2025-01-28 22:20:072025-01-28 22:20:08

Judging History

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

  • [2025-01-28 22:20:08]
  • 评测
  • 测评结果:WA
  • 用时:1688ms
  • 内存:3584kb
  • [2025-01-28 22:20:07]
  • 提交

answer

#include <bits/stdc++.h>
#define ppc __builtin_popcount
#define ctz __builtin_ctz
using namespace std;
const int P = 1e9 + 7;
int inv[21], vis[21], f[21], g[21];
int pre1[21], pre2[21], fac[21], ifac[21];
vector<int> G[21];
int C(int n, int m)
{
    if (m < 0 || m > n)
        return 0;
    return 1ll * fac[n] * ifac[n - m] % P * ifac[m] % P;
}
int bfs(int s, int st)
{
    queue<int> Q;
    Q.push(s);
    int cnt = 0;
    vis[s] = 1;
    while (Q.size())
    {
        int t = Q.front();
        Q.pop();
        cnt++;
        for (int son : G[t])
        {
            if (st >> (son - 1) & 1)
                continue;
            if (vis[son] == vis[t])
                return -1;
            if (!vis[son])
            {
                Q.push(son);
                vis[son] = 3 - vis[t];
            }
        }
    }
    return cnt;
}
int cal(int m, int st)
{
    vector<int> v;
    for (int j = 1; j <= m; j++)
    {
        vis[j] = 0;
    }
    for (int j = 1; j <= m; j++)
    {
        if (~st >> (j - 1) & 1 && !vis[j] && G[j].size())
        {
            v.push_back(bfs(j, st));
        }
    }
    int odd = 0, base = ppc(st);
    for (int x : v)
    {
        if (x == -1)
            return 0;
        odd += x & 1;
        base += x >> 1;
    }
    int ev = v.size() - odd;
    int ans = 0;
    for (int ex = 0; ex <= odd; ex++)
    {
        // base+ex ones on the left side
        // base+odd-ex ones ont the right side
        ans = (ans + 1ll * C(odd, ex) * f[base + ex] % P * g[base + odd - ex]) % P;
    }
    return 1ll * ans * (1 << ev) % P;
}
int main()
{
    fac[0] = fac[1] = ifac[0] = ifac[1] = inv[1] = 1;
    for (int i = 2; i <= 20; i++)
    {
        fac[i] = 1ll * fac[i - 1] * i % P;
        inv[i] = 1ll * (P - P / i) * inv[P % i] % P;
        ifac[i] = 1ll * ifac[i - 1] * inv[i] % P;
    }
    int T;
    cin >> T;
    while (T--)
    {
        int n1, n2, m, k;
        cin >> n1 >> n2 >> m >> k;
        for (int i = 1; i <= m; i++)
            G[i].clear();
        for (int i = 1; i <= k; i++)
        {
            int a,b;
            cin >> a >> b;
            G[a].push_back(b);
            G[b].push_back(a);
        }
        pre1[1] = pre2[1] = 1;
        for (int i = 2; i <= m; i++)
        {
            pre1[i] = 1ll * pre1[i - 1] * (n1 - i + 1) % P * inv[i - 1] % P;
            pre2[i] = 1ll * pre2[i - 1] * (n2 - i + 1) % P * inv[i - 1] % P;
        }
        int fr = 0;
        for (int i = 1; i <= m; i++)
            fr |= G[i].empty() << (i - 1);
        for (int m1 = 0; m1 + ppc(fr) <= m; m1++)
        {
            f[m1] = g[m1] = 0;
            for (int i = 0; i <= ppc(fr); i++)
            {
                f[m1] = (f[m1] + 1ll * C(ppc(fr), i) * pre1[m1 + i]) % P;
                g[m1] = (g[m1] + 1ll * C(ppc(fr), i) * pre2[m1 + i]) % P;
            }
        }
        int ans = 0;
        for (int i = 0; i < 1 << m; i++)
        {
            if (i & fr)
                continue;
            ans = (ans + cal(m, i)) % P;
        }
        cout << ans << endl;
    }
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3584kb

input:

3
2 3 3 3
2 3
1 1
2 3
2 2 2 1
1 1
1 1 10 2
1 2
1 3

output:

6
4
0

result:

ok 3 lines

Test #2:

score: -100
Wrong Answer
time: 1688ms
memory: 3584kb

input:

500
481199252 336470888 8 58
4 7
7 6
4 7
2 6
2 6
2 7
8 3
8 7
8 1
2 6
2 6
1 2
1 5
3 1
6 4
2 4
4 7
2 6
2 3
7 1
4 4
5 1
2 7
6 7
8 1
8 7
5 6
4 2
4 2
8 5
5 4
6 8
5 6
3 8
1 7
1 6
7 1
5 6
6 3
1 1
2 7
3 3
1 5
5 5
8 7
3 4
6 3
8 8
7 4
1 6
2 1
8 7
4 5
2 7
3 5
2 6
4 5
4 3
2 6 2 2
2 1
1 1
500330171 987581927 10 ...

output:

724920553
11
544161160
846759476
528107862
1
245313614
144990327
1
269113412
946380890
1
0
996348464
376698469
453289929
53346774
238565800
51241757
956196844
0
487514263
842710229
8376584
16
300260118
933415828
808801372
1
612901047
137099259
14974173
0
733717075
1
522718622
264859767
1
1
826436243...

result:

wrong answer 3rd lines differ - expected: '966099120', found: '544161160'