QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#407401#8228. 排序farfar100 ✓4369ms396888kbC++143.8kb2024-05-08 17:23:392024-05-08 17:23:41

Judging History

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

  • [2024-05-08 17:23:41]
  • 评测
  • 测评结果:100
  • 用时:4369ms
  • 内存:396888kb
  • [2024-05-08 17:23:39]
  • 提交

answer

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <vector>
#include <cstring>
#include <algorithm>
#include <map>
#include <stack>
#include <random>
#include <bitset>
#include <list>
#include <assert.h>
#include <unordered_map>
// #define int long long
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 30, M = 11, stc = 3100000, bs = (1 << 16) - 1, mod = 998244353;
int tot, d[M];
int t0, sts[stc];
unordered_map<int, int> m1;// (S, 轮数)
unordered_map<ll, int> m2;
int n, m, orr[M][M], pre[M][N];
int nxt[stc];
int dp[1 << 20][2];
short cost[stc][N];
// cost[S][i][j] S i~m 轮在 j
int id;
int ppc[1 << 16];
inline int popc(int x) {
    return ppc[x & bs] + ppc[x >> 16];
}
void dfs(int p, int now) {
    if (p >= d[id]) {
        m2[(1ll * now) << 4 | id] = ++tot;
        if (!id)
            sts[++t0] = now;
        int res = (1 << n) - 1;
        for (int i = 0; i < d[id + 1]; i++) {
            int cnt = 0;
            for (int j = i; j < n; j += d[id + 1]) {
                cnt += !(now & (1 << j));
            }
            for (int j = 0; j < cnt; j++) {
                res ^= (1 << (i + j * d[id + 1]));
            }
        }
        nxt[tot] = res;
        return ;
    }
    dfs(p + 1, now);
    for (int i = p; i < n; i += d[id]) {
        now ^= (1 << i);
        dfs(p + 1, now);
    }
}
short calc(int s, int i, int j/*pos*/) {
    // assert(!((s >> j) & 1));
    // assert(i >= 0 && i < 16);
    if (i >= m)
        return 0;
    int k = m2[(1ll * s) << 4 | i];
    // assert(j >= 0);
    // cerr << (k << 6 | j) << ' ' << k * 64 + j << "!\n";
    // assert(s <= stc);
    if (~cost[k][j])
        return cost[k][j];
    int to = nxt[k];
    // cerr << s << ' ' << i << ' ' << k << ' ' <<j << ' ' << to << ' ' << d[i + 1] << ' ' << orr[i + 1][j % d[i + 1]] << ' ' << to << ' ' << (to ^ ((1 << n) - 1)) << "|\n";
    return cost[k][j] = calc(to, i + 1, j % d[i + 1] + d[i + 1] * (popc((to ^ ((1 << n) - 1)) & orr[i + 1][j % d[i + 1]]) - 1)) + popc(s & pre[i + 1][j]);
}
signed main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= bs; i++)
        ppc[i] = ppc[i ^ (i & -i)] + 1;
    for (int i = 1; i <= m; i++) {
        scanf("%d", &d[i]);
        for (int j = 0; j < n; j++) {
            orr[i][j % d[i]] |= (1 << j);
            pre[i][j] = ((j >= d[i] ? pre[i][j - d[i]] : 0) | (1 << j));
        }
    }
    clock_t fi, st = clock();
    d[id = 0] = d[1];
    dfs(0, 0);
    for (int i = 1; i <= m; i++) {
        id = i;
        dfs(0, (1 << n) - 1);
    }
    memset(cost, -1, sizeof(cost));
    sort(sts + 1, sts + t0 + 1);
    for (int sti = 1; sti <= t0; sti++)
        m1[sts[sti]] = sti;
    dp[m1[0]][1] = 1;
    // cerr << tot << ' ' << t0 << "!\n";
    for (int sti = 1; sti < t0; sti++) {
        int i = sts[sti];
        // cout << sti << ' ' << i << ' ' << dp[sti][0] << ' ' << dp[sti][1] << "!\n";
        for (int j = 0; j < n; j++) {
            if ((i >> j) & 1)
                continue;
            int to = (i | (1 << j));
            if (m1.count(to)) {
                to = m1[to];
                // assert(!((i >> j) & 1));
                int v = dp[sti][0] + calc(i, 0, j);
                // cout << j << ' ' << to << ' ' << v << "?\n";
                if (dp[to][0] < v) {
                    dp[to][0] = v;
                    dp[to][1] = dp[sti][1];
                } else if (dp[to][0] == v)
                    dp[to][1] = (dp[to][1] + dp[sti][1]) % mod;
            }
        }
    }
    // cerr << m3.size() << '\n';
    int ed = m1[(1 << n) - 1];
    printf("%d %d\n", dp[ed][0], dp[ed][1]);
    fi = clock();
    cerr << (double)(fi - st) / CLOCKS_PER_SEC;
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 18
Accepted

Test #1:

score: 18
Accepted
time: 11ms
memory: 190084kb

input:

2 2
2 1

output:

1 1

result:

ok 2 number(s): "1 1"

Test #2:

score: 18
Accepted
time: 3ms
memory: 190312kb

input:

5 4
5 4 2 1

output:

6 4

result:

ok 2 number(s): "6 4"

Test #3:

score: 18
Accepted
time: 0ms
memory: 190336kb

input:

8 4
6 3 2 1

output:

15 4

result:

ok 2 number(s): "15 4"

Test #4:

score: 18
Accepted
time: 4ms
memory: 191808kb

input:

8 6
8 7 5 4 2 1

output:

14 2

result:

ok 2 number(s): "14 2"

Test #5:

score: 18
Accepted
time: 11ms
memory: 190044kb

input:

8 3
8 7 1

output:

22 7

result:

ok 2 number(s): "22 7"

Test #6:

score: 18
Accepted
time: 7ms
memory: 191048kb

input:

8 1
1

output:

28 1

result:

ok 2 number(s): "28 1"

Subtask #2:

score: 27
Accepted

Dependency #1:

100%
Accepted

Test #7:

score: 27
Accepted
time: 4ms
memory: 191776kb

input:

16 2
6 1

output:

77 15

result:

ok 2 number(s): "77 15"

Test #8:

score: 27
Accepted
time: 29ms
memory: 192708kb

input:

16 8
10 9 8 7 6 5 4 1

output:

57 5

result:

ok 2 number(s): "57 5"

Test #9:

score: 27
Accepted
time: 30ms
memory: 194064kb

input:

16 10
10 9 8 7 6 5 4 3 2 1

output:

57 3

result:

ok 2 number(s): "57 3"

Test #10:

score: 27
Accepted
time: 15ms
memory: 193204kb

input:

16 7
10 9 8 6 5 4 1

output:

49 1

result:

ok 2 number(s): "49 1"

Test #11:

score: 27
Accepted
time: 12ms
memory: 191788kb

input:

16 4
7 6 2 1

output:

52 9

result:

ok 2 number(s): "52 9"

Subtask #3:

score: 21
Accepted

Dependency #2:

100%
Accepted

Test #12:

score: 21
Accepted
time: 11ms
memory: 192268kb

input:

22 3
5 3 1

output:

100 1

result:

ok 2 number(s): "100 1"

Test #13:

score: 21
Accepted
time: 0ms
memory: 191452kb

input:

22 1
1

output:

231 1

result:

ok 2 number(s): "231 1"

Test #14:

score: 21
Accepted
time: 204ms
memory: 205736kb

input:

22 4
10 8 3 1

output:

97 4

result:

ok 2 number(s): "97 4"

Test #15:

score: 21
Accepted
time: 191ms
memory: 205640kb

input:

22 5
10 7 6 3 1

output:

92 70

result:

ok 2 number(s): "92 70"

Test #16:

score: 21
Accepted
time: 226ms
memory: 208844kb

input:

22 6
10 9 8 7 3 1

output:

97 1

result:

ok 2 number(s): "97 1"

Test #17:

score: 21
Accepted
time: 239ms
memory: 210332kb

input:

22 10
10 9 8 7 6 5 4 3 2 1

output:

109 1

result:

ok 2 number(s): "109 1"

Subtask #4:

score: 10
Accepted

Test #18:

score: 10
Accepted
time: 3ms
memory: 191420kb

input:

14 2
10 1

output:

61 210

result:

ok 2 number(s): "61 210"

Test #19:

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

input:

18 2
2 1

output:

117 1

result:

ok 2 number(s): "117 1"

Test #20:

score: 10
Accepted
time: 1266ms
memory: 262408kb

input:

30 2
9 1

output:

264 84

result:

ok 2 number(s): "264 84"

Test #21:

score: 10
Accepted
time: 950ms
memory: 255052kb

input:

29 2
9 1

output:

253 36

result:

ok 2 number(s): "253 36"

Test #22:

score: 10
Accepted
time: 132ms
memory: 202856kb

input:

25 2
8 1

output:

195 8

result:

ok 2 number(s): "195 8"

Subtask #5:

score: 24
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

100%
Accepted

Test #23:

score: 24
Accepted
time: 3863ms
memory: 359904kb

input:

30 4
10 9 5 1

output:

188 40

result:

ok 2 number(s): "188 40"

Test #24:

score: 24
Accepted
time: 4329ms
memory: 396888kb

input:

30 9
10 9 8 7 6 5 4 3 1

output:

184 6

result:

ok 2 number(s): "184 6"

Test #25:

score: 24
Accepted
time: 4241ms
memory: 393504kb

input:

30 8
10 9 8 7 4 3 2 1

output:

154 1

result:

ok 2 number(s): "154 1"

Test #26:

score: 24
Accepted
time: 3714ms
memory: 355668kb

input:

30 8
10 8 7 6 5 4 3 1

output:

155 1

result:

ok 2 number(s): "155 1"

Test #27:

score: 24
Accepted
time: 3626ms
memory: 352052kb

input:

30 6
10 8 6 4 3 1

output:

150 4

result:

ok 2 number(s): "150 4"

Test #28:

score: 24
Accepted
time: 4369ms
memory: 395364kb

input:

30 10
10 9 8 7 6 5 4 3 2 1

output:

184 6

result:

ok 2 number(s): "184 6"

Test #29:

score: 24
Accepted
time: 2759ms
memory: 333276kb

input:

29 6
10 9 7 5 3 1

output:

129 200

result:

ok 2 number(s): "129 200"

Extra Test:

score: 0
Extra Test Passed