QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#334696#4939. Red Black BalllamRE 20ms56556kbC++145.4kb2024-02-22 11:57:082024-02-22 11:57:08

Judging History

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

  • [2024-02-22 11:57:08]
  • 评测
  • 测评结果:RE
  • 用时:20ms
  • 内存:56556kb
  • [2024-02-22 11:57:08]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N=51;
const int mod = 998244353;

int dp[N][N][N][N];
int s[N];
int cnt[N][N], cnt2[4 * N];
int LIM = 2 * N;
int kdp[5 * N][4 * N], it_kdp = 0;

vector <int> sizes;

inline void add(int &x, int y) {
    x += y; if (x >= mod) x -= mod;
    x = (x + mod) % mod;
}

void calc(vector <int> a, int L, int xL, int R, int xR)
{
    int n=a.size();
    sizes.push_back(n);
    vector <int> p; p.push_back(L);
    for (int i:a) p.push_back(i); p.push_back(R);
    memset(cnt, 0, sizeof(cnt));
    memset(cnt2, 0, sizeof(cnt2));
    if (xL == xR) {
        if (xL == 0) cnt[n][0] = 1;
        else cnt[0][n] = 1;
        for (int i = 0; i <= n; i++)
            for (int j = 0; j <= n; j++)
                if (cnt[i][j] != 0)
                    add(cnt2[i - j + LIM], cnt[i][j]);
        return;
    }

    for (int i = 0; i <= n + 1; i++)
        for (int j = 0; j <= n + 1; j++)
            for (int x = 0; x <= n + 1; x++)
                for (int y = 0; y <= n + 1; y++)
                    dp[i][j][x][y] = 0;

    dp[0][0][0][n+1] = 1;
    for (int len = 0; len < n; len++) {
        for (int i = 0; i <= n; i++) {
            int j = len - i;
            for (int lr = 0; lr <= n + 1; lr++)
                for (int lb = 0; lb <= n + 1; lb++)
                if (dp[i][j][lr][lb] != 0) {
                    int x = dp[i][j][lr][lb];
//                    cout << i << ' ' << j << ' ' << lr << ' ' << lb << " = " << x << '\n';
                    if (i + 1 <= lr) add(dp[i+1][j][lr][lb],x*(lr - i)%mod);
                    if (j + 1 <= n + 1 - lb) add(dp[i][j+1][lr][lb], x*(n + 1 - lb - j)%mod);
                    for (int new_pos = lr + 1; new_pos < lb; new_pos++) {
                        if (p[new_pos]-p[lr] <= p[lb]-p[new_pos])
                            add(dp[i+1][j][new_pos][lb],x);
                        else add(dp[i][j+1][lr][new_pos],x);
                    }
                }
        }
    }
    for (int i = 0; i <= n; i++) {
        int j = n - i;
        for (int lr = 0; lr <= n + 1; lr++)
            for (int lb = 0; lb <= n + 1; lb++)
        if (dp[i][j][lr][lb]) {
//            cout << i << ' ' << lr << " & " << j << ' ' << lb << " === " << dp[i][j][lr][lb] << '\n';
            add(cnt[i][j], dp[i][j][lr][lb]);
        }
    }

    for (int i = 0; i <= n; i++)
        for (int j = 0; j <= n; j++)
        if (cnt[i][j] != 0) {
            int x;
            if (xL == 0) x = (i - j + LIM);
            else x = (j - i + LIM);
            add(cnt2[x],cnt[i][j]);
        }
}

void upd_cnt() {
    it_kdp++;
    for (int sum = 0; sum < 4 * N; sum++)
        if (kdp[it_kdp-1][sum] != 0)
            for (int sum2 = 0; sum2 < 4 * N; sum2++)
                if (cnt2[sum2] != 0)
                {
                    int sum3 = sum + sum2 - LIM;
                    add(kdp[it_kdp][sum3],kdp[it_kdp-1][sum] * cnt2[sum2] % mod);
                }
}

int p[5 * N], inv[5 * N];

const string vkt = "BLACK";

int ppow(int x, int y) {
    int temp = 1LL;
    while (y) {
        if (y & 1) temp = temp *  x % mod;
        x = x * x % mod;
        y /= 2;
    }
    return temp;
}

void precalc()
{
    int lim = 110;
    p[0] = 1LL;
    for (int i = 1; i <= lim; i++) p[i] = p[i-1] * i % mod;
    inv[lim] = ppow(p[lim], mod - 2);
    for (int i = lim - 1; i >= 0; i--) inv[i] = inv[i+1] * (i+1) % mod;
}

void solve() {
    precalc();
    int n, m;
    cin >> m >> n;
    vector <int> a(n);
    vector <pair<int,int>> b(m + 2);
    for (int i = 1; i <= m; i++) {
        cin >> b[i].first;
        string s; cin >> s;
        b[i].second = (s == vkt);
    }
    b[0]={-1e18,0};
    b[m+1]={1e18,1};
    sort(b.begin(), b.end());
    for (int i = 0; i < n; i++) cin >> a[i];
    sort(a.begin(), a.end());
    it_kdp = 0;
    kdp[it_kdp][LIM] = 1;
    for (int i = 1; i < b.size(); i++) {
        int L = b[i-1].first; int xL = b[i-1].second;
        int R = b[i].first; int xR = b[i].second;
        vector <int> tmp;
        for (int j = 0; j < n; j++)
            if (a[j] > L && a[j] < R) tmp.push_back(a[j]);
        calc(tmp, L, xL, R, xR);
        upd_cnt();
    }
    memset(cnt,0,sizeof(cnt));
    memset(cnt2,0,sizeof(cnt2));
    int sum = 0;
    for (int i = 1; i <= m; i++) {
        if (b[i].second == 0) sum++;
        else sum--;
    }
    cnt2[sum + LIM] = 1;
    upd_cnt();
    int ans = 0;
    for (int sum = 0; sum < 4 * N; sum++)
        if (sum > LIM) add(ans, kdp[it_kdp][sum]);
//    int sum = 1;
//    cout << ans << '\n';
    int S = 0;
    for (int i : sizes) S += i;
    ans = ans * p[S] % mod;
    for (int i : sizes) ans = ans * inv[i] % mod;
    cout << ans << '\n';
}

signed main()
{
    ios_base::sync_with_stdio(false);
//    cin.tie(nullptr); cout.tie(nullptr);
    solve();
    return 0;
//    int L, R, xL, xR;
//    cout << "L = "; cin >> L;
//    cout << "xL = "; cin >> xL;
//    cout << "R = "; cin >> R;
//    cout << "xR = "; cin >> xR;
//    int n; cout << "n = "; cin >> n;
//    vector <int> a(n);
//    for (int i = 0; i < n; i++) cin >> a[i];
//    it_kdp = 0;
//    kdp[it_kdp][LIM] = 1;
//    calc(a, L, xL, R, xR);
//    upd_cnt();
//    int ans = 0;
//    for (int sum = 0; sum < 4 * N; sum++)
//        if (sum > LIM) add(ans, kdp[it_kdp][sum]);
//    cout << ans << '\n';
}

/**
2 3
1 RED
10 BLACK
2 4 7
**/

詳細信息

Test #1:

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

input:

2 3
1 BLACK
10 RED
2 5 7

output:

3

result:

ok single line: '3'

Test #2:

score: 0
Accepted
time: 1ms
memory: 7752kb

input:

2 3
1 RED
10 BLACK
2 4 7

output:

6

result:

ok single line: '6'

Test #3:

score: 0
Accepted
time: 1ms
memory: 8024kb

input:

2 3
1 RED
10 BLACK
7 4 2

output:

6

result:

ok single line: '6'

Test #4:

score: 0
Accepted
time: 0ms
memory: 18152kb

input:

20 46
238846592 BLACK
199923217 RED
526626128 BLACK
62308338 RED
523811748 RED
59432 BLACK
273113193 BLACK
730729301 BLACK
973259012 RED
225318015 BLACK
611574923 RED
234880345 RED
485448383 BLACK
405607946 BLACK
747693124 RED
794086621 BLACK
91585417 BLACK
466451303 RED
244184598 RED
334788273 RED
...

output:

850819974

result:

ok single line: '850819974'

Test #5:

score: 0
Accepted
time: 2ms
memory: 14096kb

input:

24 46
1579 BLACK
10321 BLACK
7159 RED
13111 RED
11437 RED
277 BLACK
11623 BLACK
9391 RED
1765 RED
6787 BLACK
12367 BLACK
8647 RED
1207 RED
3811 RED
4183 BLACK
649 BLACK
11995 RED
6043 RED
2137 BLACK
8833 BLACK
7345 BLACK
463 RED
4369 RED
5113 BLACK
10507 7717 2509 4741 3997 5299 10879 8275 9763 5671...

output:

988708148

result:

ok single line: '988708148'

Test #6:

score: 0
Accepted
time: 20ms
memory: 56556kb

input:

2 49
747 RED
38197 BLACK
23966 11982 30707 20970 32205 23217 1496 24715 36699 20221 8986 8237 32954 29209 35950 18723 11233 13480 5990 22468 12731 27711 7488 2245 26213 16476 17225 34452 19472 15727 14978 9735 3743 4492 2994 33703 6739 17974 5241 21719 14229 28460 25464 29958 26962 35201 37448 10484...

output:

554620557

result:

ok single line: '554620557'

Test #7:

score: 0
Accepted
time: 0ms
memory: 36380kb

input:

5 49
18120 BLACK
7140 BLACK
39348 RED
552 RED
10068 RED
28368 2748 23244 10800 21780 34956 20316 15192 13728 6408 3480 22512 18852 2016 36420 4944 31296 37152 17388 7872 35688 32760 16656 34224 37884 19584 1284 11532 33492 26904 24708 15924 21048 4212 9336 5676 32028 29100 29832 23976 12996 27636 26...

output:

680298042

result:

ok single line: '680298042'

Test #8:

score: 0
Accepted
time: 0ms
memory: 24228kb

input:

13 49
107689803 BLACK
17641182 BLACK
719317410 RED
998074505 RED
629653125 BLACK
596623825 RED
302505396 BLACK
755371774 BLACK
894467843 BLACK
4346620 RED
67686851 RED
195025022 RED
843051518 RED
549024576 858137241 623250360 760797781 289351341 882742196 363095414 925898076 534873997 550178294 5220...

output:

48457350

result:

ok single line: '48457350'

Test #9:

score: 0
Accepted
time: 2ms
memory: 16148kb

input:

16 47
57366995 RED
142257512 RED
269104096 BLACK
85386190 BLACK
947963705 RED
327042464 RED
249359510 RED
779101020 BLACK
572804408 RED
14976978 BLACK
994513563 BLACK
12270136 RED
832385903 BLACK
827404778 RED
349299284 BLACK
157782755 BLACK
194509365 275027783 635585542 141178577 746922135 28144721...

output:

110578970

result:

ok single line: '110578970'

Test #10:

score: 0
Accepted
time: 0ms
memory: 30920kb

input:

4 47
557094997 BLACK
977689947 RED
524426310 RED
37970126 BLACK
490604903 841435598 941812205 445138440 510338155 186951281 171655969 622981900 583907196 970645377 428450182 138362981 621613180 99063603 462141100 154435607 547626426 298066536 620132019 171659177 777569078 731639638 947801880 4924222...

output:

371087856

result:

ok single line: '371087856'

Test #11:

score: -100
Runtime Error

input:

2 50
211 BLACK
160 RED
195 205 189 187 179 175 163 164 161 199 169 194 182 192 162 197 173 165 174 167 201 176 207 206 184 196 181 204 190 191 198 183 180 210 186 208 209 203 171 178 193 202 168 185 172 200 170 188 177 166

output:


result: