QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#620008#4514. Schrödinger and PavlovAysct0 471ms3852kbC++142.1kb2024-10-07 16:17:252024-10-07 16:17:28

Judging History

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

  • [2024-10-07 16:17:28]
  • 评测
  • 测评结果:0
  • 用时:471ms
  • 内存:3852kb
  • [2024-10-07 16:17:25]
  • 提交

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()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T;
    cin >> T;
    while (T--)
        sol();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

score: 0
Wrong Answer
time: 10ms
memory: 3764kb

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:

75340207
248320570
1
501005577
0
0
0
988185614
0
840220993
140130951
8
0
0
570065479
128
67108864
0
0
560523800
0
0
770999807
0
0
8044588
0
810284813
0
640520040
494092823
747045391
0
747046415
0
32
0
0
562080146
536870912
121039409
715073135
121047345
3
0
0
0
3
836088195
8
873522699
0
0
8388608
262...

result:

wrong answer 1st lines differ - expected: 'Case #1: 75340207', found: '75340207'

Test #2:

score: 0
Wrong Answer
time: 471ms
memory: 3852kb

input:

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

output:

13595505
0
370024028
332957307
0
401898087
588719939
0
0
0
893344250
816205426
902710840
735342227
0
623834768
195615577
446926994
89412709
0
200118782
318897047
0
535636316
980210302
959637757
0
657699170
0
700061603
925004641
17439348
0
0
435282934
837785542
447692331
0
430707786
950017671
4967748...

result:

wrong answer 1st lines differ - expected: 'Case #1: 13595505', found: '13595505'