QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#620018#4514. Schrödinger and PavlovAysct50 ✓474ms3788kbC++142.2kb2024-10-07 16:19:082024-10-07 16:19:09

Judging History

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

  • [2024-10-07 16:19:09]
  • 评测
  • 测评结果:50
  • 用时:474ms
  • 内存:3788kb
  • [2024-10-07 16:19:08]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7, inv2 = 500000004;
int n, b[5005], cut;
char s[5005];
struct dsu
{
    int fa[5005];
    void init()
    {
        for (int i = 1; i <= n; i++)
            fa[i] = i;
    }
    int find(int x) { return (x == fa[x] ? x : (fa[x] = find(fa[x]))); }
} t1, t2;
int f[5005];
inline int fan(int x) { return (1 - x + mod) % mod; }
void sol()
{
    cin >> n >> s + 1;
    t1.init();
    t2.init();
    for (int i = 1; i <= n; i++)
    {
        cin >> b[i];
        if (t1.find(i) != t1.find(b[i]))
        {
            t1.fa[t1.find(i)] = t1.find(b[i]);
        }
    }
    for (int i = n; i >= 1; i--)
    {
        if (t1.find(i) != t1.find(n))
            continue;
        if (t2.find(i) == t2.find(b[i]))
        {
            cut = i;
        }
        t2.fa[t2.find(i)] = t2.find(b[i]);
    }
    int p, ans = 0;
    for (int c1 = 0; c1 <= 1; c1++)
    {
        for (int c2 = 0; c2 <= 1; c2++)
        {
            for (int i = 1; i <= n; i++)
            {
                if (s[i] == 'C')
                    f[i] = 1;
                else if (s[i] == '?')
                    f[i] = inv2;
                else
                    f[i] = 0;
            }
            for (int i = 1; i <= n; i++)
            {
                if (i == cut)
                {
                    p = (c1 ? f[cut] : fan(f[cut])) * (c2 ? f[b[cut]] : fan(f[b[cut]])) % mod;
                    f[cut] = c1;
                    f[b[cut]] = c2;
                }
                int pi = f[i], pb = f[b[i]];
                f[i] = pi * pb % mod;
                f[b[i]] = (pi + pb * fan(pi) % mod) % mod;
            }
            ans = (ans + p * f[n] % mod) % mod;
        }
    }
    for (int i = 1; i <= n; i++)
        if (s[i] == '?')
            ans = ans * 2 % mod;
    cout << ans << '\n';
}
signed main()
{
    // freopen("experiment.in", "r", stdin);
    // freopen("experiment.out", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T;
    cin >> T;
    for (int i = 1; i <= T; i++)
    {
        cout<<"Case #" << i << ": ";
        sol();
    }
    return 0;
}

详细


Pretests


Final Tests

Test #1:

score: 8
Accepted
time: 10ms
memory: 3732kb

input:

1234
92
????????????????????????????????????????????????????????????????????????????????????????????
2 1 2 3 4 7 5 7 8 9 15 9 15 12 14 15 16 19 16 17 17 20 22 23 24 25 26 27 28 29 30 31 32 33 34 38 34 39 35 39 45 40 42 43 44 45 46 47 48 49 50 51 52 57 53 55 56 56 57 59 60 61 62 63 64 64 66 67 68 69 ...

output:

Case #1: 75340207
Case #2: 248320570
Case #3: 1
Case #4: 501005577
Case #5: 0
Case #6: 0
Case #7: 0
Case #8: 988185614
Case #9: 0
Case #10: 840220993
Case #11: 140130951
Case #12: 8
Case #13: 0
Case #14: 0
Case #15: 570065479
Case #16: 128
Case #17: 67108864
Case #18: 0
Case #19: 0
Case #20: 5605238...

result:

ok 1234 lines

Test #2:

score: 42
Accepted
time: 474ms
memory: 3788kb

input:

1234
4693
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????...

output:

Case #1: 13595505
Case #2: 0
Case #3: 370024028
Case #4: 332957307
Case #5: 0
Case #6: 401898087
Case #7: 588719939
Case #8: 0
Case #9: 0
Case #10: 0
Case #11: 893344250
Case #12: 816205426
Case #13: 902710840
Case #14: 735342227
Case #15: 0
Case #16: 623834768
Case #17: 195615577
Case #18: 44692699...

result:

ok 1234 lines