QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#768220#9735. Japanese BandsNlllTL 47ms3812kbC++173.3kb2024-11-21 02:58:022024-11-21 02:58:02

Judging History

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

  • [2025-01-18 23:34:05]
  • hack成功,自动添加数据
  • (/hack/1459)
  • [2024-11-21 02:58:02]
  • 评测
  • 测评结果:TL
  • 用时:47ms
  • 内存:3812kb
  • [2024-11-21 02:58:02]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int Maxm = 25;
const ll P = 1e9 + 7;
int mm, OK, must[Maxm], f[Maxm], ok[Maxm], pd[Maxm];
int A, B, c[Maxm], p[Maxm];
vector<int> edge[Maxm];
ll c1[Maxm], c2[Maxm], inv[Maxm];
int _norm(int x)
{
    return x > P ? x - P : x;
}
void adde(int x, int y)
{
    edge[x].push_back(y);
    edge[y].push_back(x);
}
void dfs(int x, int now)
{
    if(!OK) return;
    c[x] = now;
    if(now == 1) A++;
    else B++;
    for(auto &y : edge[x])
    {
        if(must[y] || ok[y]) continue;
        if(c[y])
        {
            if(c[y] != 3 - now)
            {
                OK = 0;
                return;
            }
            continue;
        }
        dfs(y, 3 - now);
        if(!OK) return;
    }
}
ll C1(int x)
{
    if(x < 0) return 0;
    return c1[x];
}
ll C2(int x)
{
    if(x < 0) return 0;
    return c2[x];
}
void solve()
{
    int n1, n2, m, k;
    cin >> n1 >> n2 >> m >> k;
    vector<pair<int, int>> limit;
    int need = 0;
    for(int i = 1; i <= k; i++)
    {
        int x, y;
        cin >> x >> y;
        pd[x] = pd[y] = 1;
        if(x == y)
        {
            if(must[x]) continue;
            must[x] = 1;
            need++;
        }
        else
        {
            limit.push_back({x, y});
        }
    }
    int num = 0;
    mm = 0;
    for(int i = 1; i <= m; i++)
    {
        if(!pd[i])
        {
            num++;
            need++;
            continue;
        }
        if(must[i]) continue;
        f[++mm] = i;
    }
    for(auto &[x, y] : limit)
    {
        adde(x, y);
    }
    int Lim = 1 << mm;
    int ans = 0;
    c1[0] = c2[0] = inv[1] = 1;
    for(int i = 2; i <= m; i++) inv[i] = P - P / i * inv[P % i] % P;
    for(int i = 1; i <= m; i++)
    {
        c1[i] = c1[i - 1] * (n1 + num - i) % P * inv[i] % P;
        c2[i] = c2[i - 1] * (n2 + num - i) % P * inv[i] % P;
    }
    for(int t = 0; t < Lim; t++)
    {
        OK = 1;
        A = B = 0;
        int now = 0, tmp = need, z = mm;
        for(int i = 1; i <= mm; i++)
        {
            p[i] = c[f[i]] = 0;
            if(t & (1 << (i - 1)))
            {
                ok[f[i]] = 1;
                tmp++;
                z--;
            }
            else ok[f[i]] = 0;
        }
        p[0] = 1;
        for(int i = 1; i <= mm; i++)
        {
            if(!ok[f[i]] && !c[f[i]])
            {
                dfs(f[i], 1);
                if(!OK) break;
                if(A > B) swap(A, B);
                now = now + B;
                for(int i = now; i >= A; i--)
                {
                    p[i] = p[i - A];
                    if(i >= B) p[i] += p[i - B];
                }
                for(int i = A - 1; i >= 0; i--) p[i] = 0;
                A = B = 0;
            }
        }
        if(!OK) continue;
        for(int i = 0; i <= now; i++)
        {
            if(!p[i]) continue;
            ans = _norm(ans + C1(tmp + i - 1) * C2(tmp + (z - i) - 1) % P * p[i] % P);
        }
    }
    cout << ans << "\n";
    for(int i = 1; i <= m; i++)
    {
        must[i] = pd[i] = 0;
        vector<int>().swap(edge[i]);
    }
}
int main()
{
    cin.tie(0)->sync_with_stdio(false);
    int T;
    cin >> T;
    while(T--) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0
Accepted
time: 47ms
memory: 3812kb

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
966099120
846759476
528107862
1
245313614
144990327
1
269113412
946380890
1
0
996348464
376698469
453289929
53346774
238565800
260837053
956196844
0
487514263
842710229
8376584
16
300260118
933415828
808801372
1
612901047
137099259
14974173
0
531418108
1
522718622
264859767
1
1
59318545...

result:

ok 500 lines

Test #3:

score: 0
Accepted
time: 13ms
memory: 3644kb

input:

500
54748096 75475634 8 64
3 8
3 2
3 5
5 6
5 7
5 4
2 2
5 8
5 3
3 5
7 6
1 7
3 3
6 8
3 5
3 4
1 2
7 5
7 6
4 7
8 7
7 5
4 2
3 2
8 5
7 1
4 3
4 6
3 3
8 3
6 1
5 4
1 4
2 3
5 6
1 4
5 8
8 2
1 3
8 1
5 7
1 2
3 8
4 2
5 4
7 2
4 6
5 8
4 6
3 5
5 7
4 6
4 8
6 4
7 4
3 3
5 2
1 6
4 5
3 1
1 4
5 6
4 3
3 1
44007561 94403501...

output:

48662676
1
138912221
349671067
150052712
86775188
956490545
756234965
1
567881550
726030753
1
914856825
867349578
0
784807018
388018114
433007753
524010032
507842496
282218203
442034917
668340856
1
1
1
489645337
153477090
564466420
1673
0
390234222
604892803
264163973
601602052
135055881
27720
15744...

result:

ok 500 lines

Test #4:

score: 0
Accepted
time: 14ms
memory: 3588kb

input:

500
923264237 374288891 9 36
8 8
5 8
3 6
2 4
2 6
4 7
3 8
3 4
5 5
5 1
9 3
1 9
5 4
5 8
4 3
2 8
7 6
7 3
8 9
3 4
4 1
8 1
7 3
6 3
6 2
9 6
2 3
2 5
6 1
8 5
4 5
3 1
8 7
6 5
3 2
1 1
885955146 964950238 8 59
1 3
7 7
8 1
2 7
6 5
1 3
1 2
2 3
1 2
8 2
4 1
5 6
5 8
3 1
8 3
2 3
8 3
6 5
2 5
6 7
7 2
6 3
6 5
6 7
7 8
8 ...

output:

975815187
715739872
849684680
940532257
1
181582862
672614348
487379998
1
872849956
969457677
827780523
98088
1
496360790
568133531
231661973
1
981238143
13510
8
663802864
252
107191472
18522
415132978
697199493
116414735
1
10
912343690
81
583097677
223080594
33251656
117275734
1
419400938
630591794...

result:

ok 500 lines

Test #5:

score: -100
Time Limit Exceeded

input:

500
1 9 7 21
4 3
4 5
4 3
4 5
4 5
4 7
4 7
4 2
4 3
4 6
4 1
4 7
4 5
4 1
4 2
4 2
4 2
4 1
4 2
4 6
4 6
192019400 315755530 10 56
8 10
9 7
9 6
8 4
3 4
1 6
8 7
9 10
8 7
5 7
2 4
5 7
1 6
9 7
2 4
1 4
2 7
5 7
5 7
2 10
3 7
8 4
8 4
3 4
5 7
9 6
2 10
3 4
8 10
9 4
5 10
2 4
8 7
5 4
5 7
9 4
8 6
1 7
5 4
8 10
3 4
9 7
8 ...

output:


result: