QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#874270#9685. nim 游戏cooluo16 561ms30300kbC++237.8kb2025-01-27 21:39:582025-01-27 21:39:59

Judging History

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

  • [2025-01-27 21:39:59]
  • 评测
  • 测评结果:16
  • 用时:561ms
  • 内存:30300kb
  • [2025-01-27 21:39:58]
  • 提交

answer

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

#define ll long long
#define ul unsigned ll
#define LL __int128_t
#define db double
#define DB long db
#define pii pair<int, int>
#define fi first
#define se second
#define mkpr make_pair
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define rsz resize
#define eb emplace_back
#define all(c) (c).begin(), (c).end()
#define bit(k) (1 << (k))
#define Bit(k) (1ll << (k))
#define BIT(k) ((LL)1 << (k))
#define lowbit(x) ((x) & -(x))
#define bin(s, k) ((s) >> (k) & 1)
#define lg2(x) (31 - __builtin_clz(x))
#define LG2(x) (63 - __builtin_clzll(x))
#define popcnt(x) __builtin_popcount(x)
#define mem(a, x) memset(a, x, sizeof(a))
#define req(i, l, r) for (int i(l), i##End(r); i < i##End; i = -~i)
#define rep(i, l, r) for (int i(l), i##End(r); i <= i##End; i = -~i)
#define per(i, r, l) for (int i(r), i##End(l); i >= i##End; i = ~-i)

// #define FILERR

#ifdef JYR
#include "debug.h"
#ifdef FILERR
auto filerr = fopen("Test.err", "w");
#else
auto filerr = stderr;
#endif
#define errs(x) fputs(x "\n", filerr)
#define errm(x, ...) fprintf(filerr, x, ##__VA_ARGS__)
#else
#define dbg(...) 0
#define errs(x) 0
#define errm(x, ...) 0
#endif

template<typename T, typename U> void chkmx(T &_a, U _b) { if (_a < _b) _a = _b; }
template<typename T, typename U> void chkmn(T &_a, U _b) { if (_b < _a) _a = _b; }

bool Mbe;

struct FastIO {
    char buf[1 << 20], *p1, *p2;
    char puf[1 << 20], *pf;

    FastIO() : p1(buf), p2(buf), pf(puf) {}
    ~FastIO() { fwrite(puf, 1, pf - puf, stdout); }

    char gc() {
        if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin);
        return p1 == p2 ? EOF : *p1++;
    }

    bool blank(char c) { return c == ' ' || c == '\r' || c == '\n' || c == '\t'; }

    char rd() {
        char c = gc(); while (blank(c)) c = gc();
        return c;
    }

    template<typename T> T rd() {
        T x = 0; int f = 0; char c = gc();
        while (!isdigit(c)) f = (c == '-'), c = gc();
        while (isdigit(c)) x = (x << 1) + (x << 3) + (c - '0'), c = gc();
        return f ? -x : x;
    }

    int rds(char *s) {
        char c = gc(), *S = s;
        while (blank(c)) c = gc();
        while (!blank(c) && c != EOF) *s++ = c, c = gc();
        return *s = 0, abs(s - S);
    }

    int rdl(char *s) {
        char c = gc(), *S = s;
        while (c == '\r' || c == '\n') c = gc();
        while (c != '\r' && c != '\n' && c != EOF) *s++ = c, c = gc();
        return *s = 0, abs(s - S);
    }

    void rd(char &c) { c = rd(); }

    void rd(char *s) {
        char c = gc();
        while (blank(c)) c = gc();
        if (c == EOF) { *s = 0; return; }
        while (!blank(c) && c != EOF) *s++ = c, c = gc();
        *s = 0;
    }

    template<typename T> void rd(T &x) {
        x = 0; int f = 0; char c = gc();
        while (!isdigit(c)) f = (c == '-'), c = gc();
        while (isdigit(c)) x = (x << 1) + (x << 3) + (c - '0'), c = gc();
        if (f) x = -x;
    }

    template<typename T, typename... Ts>
    void rd(T& x, Ts&... xs) { rd(x), rd(xs...); }

    void pc(const char &c) {
        if (pf - puf == 1 << 20) fwrite(pf = puf, 1, 1 << 20, stdout);
        *pf++ = c;
    }

    void prt(char c) { pc(c); }
    void prt(char* s) { while (*s) pc(*s++); }
    void prt(const char* s) { while (*s) pc(*s++); }

    template<typename T> void prt(T x) {
        static int st[41], tp = 0;
        if (x == 0) { pc('0'); return; }
        if (x < 0) x = -x, pc('-');
        while (x) st[++tp] = x % 10, x /= 10;
        while (tp) pc(st[tp--] + '0');
    }

    template<typename T> void prt(T *x) { while (*x) pc(*x++); }

    template<typename T, typename... Ts>
    void prt(T x, Ts... xs) { prt(x), prt(xs...); }

    void prts(const char *s, char c = '\n') {
        while (*s) pc(*s++);
        if (c) pc(c);
    }
} IO;

#define rd IO.rd
#define rdl IO.rdl
#define ri rd<int>()
#define rl rd<ll>()
#define prt IO.prt
#define prs IO.prts
#define edl IO.pc('\n')

#define MC

#define N 100005
#define mod 998244353
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f

#define pli pair<ll, int>
#define vli vector<pli>
#define pb pop_back

int TID, n, m;
ll ans;
ll a[N];
int p[63][N];
int vt[N], tp;
vli st;
vector<vli> res;
bool vs[N];

ll dfs1(ll xs, int k, bool fg) {
    if (k < 0) return 0;
    ll K = BIT(k) - 1;
    if (bin(xs, k)) {
        rep(_, 1, n) if (int i = p[k][_]; !vs[i] && !bin(a[i], k)) {
            vs[i] = 1;
            ll x = K + 1 - (a[i] & K) + dfs1(xs ^ (a[i] & K), k - 1, 1);
            return vs[i] = 0, x;
        }
        if (fg) return K + 1 + dfs1(xs, k - 1, fg);
        return INF;
    }
    ll x = dfs1(xs, k - 1, fg);
    if (!fg) rep(_, 1, n) if (int i = p[k][_]; !vs[i]) {
        if (bin(a[i], k)) break;
        rep(__, _ + 1, n) if (int j = p[k][__]; !vs[j]) {
            if (bin(a[j], k)) break;
            vs[i] = vs[j] = 1;
            chkmn(x, K + 1 - (a[i] & K) - (a[j] & K) + K + 1
                 + dfs1(xs ^ (a[i] & K) ^ (a[j] & K), k - 1, 1));
            return vs[i] = vs[j] = 0, x;
        }
    }
    return x;
}

bool dfs2(ll xs, int k, bool fg, ll su) {
    if (su > ans) return 0;
    if (k < 0) {
        if (res.size() == m) throw 0;
        return res.eb(st), 1;
    }
    ll K = BIT(k) - 1;
    bool bk = 0;
    if (bin(xs, k)) {
        rep(_, 1, n) if (int i = p[k][_]; !vs[i]) {
            if (bin(a[i], k)) break;
            vs[i] = 1, st.eb(K + 1 - (a[i] & K), i), vt[++tp] = i;
            bool tg = dfs2(xs ^ (a[i] & K), k - 1, 1, su + K + 1 - (a[i] & K));
            vs[i] = 0, st.pb(), tp--;
            if (!tg) break; bk = 1;
        }
        if (fg) {
            rep(_, 1, tp) {
                int i = vt[_];
                st.eb(K + 1, i);
                bool tg = dfs2(xs, k - 1, fg, su + K + 1);
                if (st.pb(); !tg) break; bk = 1;
            }
        }
        return bk;
    }
    bk = dfs2(xs, k - 1, fg, su);
    rep(_, 1, n) if (int i = p[k][_]; !vs[i]) {
        if (bin(a[i], k)) break;
        rep(__, _ + 1, n) if (int j = p[k][__]; !vs[j]) {
            if (bin(a[j], k)) break;
            ll x = K + 1 - (a[i] & K), y = K + 1 - (a[j] & K);
            vs[i] = vs[j] = 1, st.eb(x, i), st.eb(y, j), vt[++tp] = i, vt[++tp] = j;
            bool tg = dfs2(xs ^ (a[i] & K) ^ (a[j] & K), k - 1, 1, su + x + y);
            vs[i] = vs[j] = 0, st.pb(), st.pb(), ----tp;
            if (!tg) break; bk = 1;
        }
    }
    return bk;
}

void mslv() {
    rd(n, m), st.clear(), res.clear();
    ll xs = 0;
    rep(i, 1, n) xs ^= a[i] = rl;
    rep(k, 0, 60) {
        rep(i, 1, n) p[k][i] = i;
        ll K = BIT(k) - 1;
        sort(p[k] + 1, p[k] + n + 1, [&](int i, int j) {
            return (bin(a[i], k) ? -1 : a[i] & K)
                 > (bin(a[j], k) ? -1 : a[j] & K);
        });
    }
    ans = dfs1(xs, 60, 0);
    try { dfs2(xs, 60, 0, 0); }
    catch(const std::exception& e) {}
    prt(ans, '\n', res.size(), '\n');
    for (auto ve : res) {
        sort(all(ve), [](pli x, pli y) { return x.se < y.se; });
        vli vc;
        for (auto [x, i] : ve) {
            if (vc.empty() || vc.back().se != i) vc.eb(x, i);
            else vc.back().fi += x;
        }
        prt(vc.size(), '\n');
        for (auto [x, i] : vc) prt(i, ' '); edl;
        for (auto [x, i] : vc) prt(x, ' '); edl;
    }
}

void mprw() {}

bool Med;

int main() {
    #ifdef JYR
    errs("Running!");
    freopen("Test.in", "r", stdin);
    freopen("Test.out", "w", stdout);
    #endif
    mprw();
    #ifdef MC
    int _; rd(TID, _);
    while (_--) mslv();
    #else
    mslv();
    #endif
    errm("%.3lfMB %.0lfms\n", abs(&Med - &Mbe) / 1048576., clock() * 1000. / CLOCKS_PER_SEC);
    return 0;
}

详细

Subtask #1:

score: 4
Accepted

Test #1:

score: 4
Accepted
time: 150ms
memory: 28436kb

input:

1 10000
2 1
324097321 555675086
2 1
304655177 991244276
2 1
9980291 383616352
2 1
1071036550 795625380
2 1
682098056 68370721
2 1
969101726 685975156
2 1
973896269 354857775
2 1
196188000 606494155
2 1
754416123 467588829
2 1
495704303 558090120
2 1
618002000 491488050
2 1
741575237 9937018
2 1
1002...

output:

231577765
1
1
1 
231577765 
686589099
1
1
1 
686589099 
373636061
1
1
1 
373636061 
275411170
1
1
2 
275411170 
613727335
1
1
2 
613727335 
283126570
1
1
2 
283126570 
619038494
1
1
2 
619038494 
410306155
1
1
1 
410306155 
286827294
1
1
2 
286827294 
62385817
1
1
1 
62385817 
126513950
1
1
2 
12651...

result:

ok correct answer

Subtask #2:

score: 12
Accepted

Test #2:

score: 12
Accepted
time: 0ms
memory: 30292kb

input:

2 5
5 2000
0 13 3 4 10
5 2000
0 3 9 1 11
5 2000
0 13 7 3 5
5 2000
0 1 13 9 2
5 2000
0 8 14 7 13

output:

0
1
0


0
1
0


2
2
2
2 3 
1 1 
2
3 5 
1 1 
3
2
2
1 5 
1 2 
1
5 
3 
2
1
2
4 5 
1 1 

result:

ok correct answer

Test #3:

score: 12
Accepted
time: 0ms
memory: 28260kb

input:

2 5
5 2000
0 4 14 5 7
5 2000
0 2 15 0 12
5 2000
0 1 14 0 5
5 2000
0 13 4 12 3
5 2000
0 10 10 1 11

output:

6
2
3
1 4 5 
4 1 1 
2
4 5 
1 5 
1
4
1
1 
1 
1
2 
1 
1
4 
1 
1
5 
1 
8
8
3
1 2 5 
2 3 3 
3
2 4 5 
3 2 3 
2
2 5 
3 5 
2
2 5 
5 3 
3
1 2 5 
4 1 3 
3
2 4 5 
1 4 3 
2
2 5 
1 7 
2
2 5 
7 1 
2
4
2
1 5 
1 1 
2
3 5 
1 1 
2
4 5 
1 1 
1
5 
2 
10
13
3
1 2 4 
2 1 7 
3
1 3 4 
2 1 7 
2
1 4 
2 8 
2
1 4 
3 7 
2
1 4 ...

result:

ok correct answer

Test #4:

score: 12
Accepted
time: 1ms
memory: 30292kb

input:

2 5
5 2000
0 6 15 10 1
5 2000
0 15 0 13 10
5 2000
0 5 7 5 1
5 2000
0 13 3 2 15
5 2000
0 2 4 7 0

output:

2
5
2
1 5 
1 1 
2
2 5 
1 1 
2
4 5 
1 1 
1
5 
2 
1
1 
2 
8
2
1
1 
8 
1
3 
8 
4
2
2
2 5 
1 3 
2
4 5 
1 3 
1
1
1
2 
1 
1
4
1
1 
1 
1
2 
1 
1
3 
1 
1
5 
1 

result:

ok correct answer

Subtask #3:

score: 0
Runtime Error

Dependency #2:

100%
Accepted

Test #5:

score: 0
Runtime Error

input:

3 5
6 2000
0 45 517 811 107 132
6 2000
0 382 576 805 419 579
6 2000
0 379 809 441 331 67
6 2000
0 565 776 959 852 383
6 2000
0 613 383 829 47 441

output:


result:


Subtask #4:

score: 0
Runtime Error

Test #8:

score: 0
Runtime Error

input:

4 257
100000 100
32768 65536 262144 32768 8388608 1048576 4 67108864 16384 32768 262144 8192 512 134217728 65536 4194304 262144 67108864 1024 262144 64 32 65536 2097152 268435456 1 2048 4194304 16777216 8 16384 2 2048 16777216 268435456 262144 1048576 8388608 16 268435456 2 128 4194304 262144 32768 ...

output:


result:


Subtask #5:

score: 0
Runtime Error

Test #18:

score: 12
Accepted
time: 561ms
memory: 30300kb

input:

5 10000
10 1
0 1073741823 1073741823 1073741823 1073741823 1073741823 1073741823 1073741823 1073741823 1073741823
10 1
0 1073741823 1073741823 1073741823 1073741823 1073741823 1073741823 1073741823 1073741823 1073741823
10 1
0 1073741823 1073741823 1073741823 1073741823 1073741823 1073741823 1073741...

output:

1073741823
1
1
1 
1073741823 
1073741823
1
1
1 
1073741823 
1073741823
1
1
1 
1073741823 
1073741823
1
1
1 
1073741823 
1073741823
1
1
1 
1073741823 
1073741823
1
1
1 
1073741823 
1073741823
1
1
1 
1073741823 
1073741823
1
1
1 
1073741823 
1073741823
1
1
1 
1073741823 
1073741823
1
1
1 
1073741823 
...

result:

ok correct answer

Test #19:

score: 0
Runtime Error

input:

5 2323
100000 1
0 3170633 888529329 347839787 101667249 273239696 1028446182 411994109 710973319 298677951 299452068 519308796 361451040 488605068 74238166 997794448 478367019 532094220 747266199 217905213 682359917 774814810 234838947 456387659 38459020 434013070 633290806 173828476 94076883 568288...

output:


result:


Subtask #6:

score: 0
Runtime Error

Test #22:

score: 0
Runtime Error

input:

6 23
1000 10
0 357293452 452461848 986047039 546588280 762710079 767831017 39741545 416114273 515599366 1018969624 603342125 928112286 1053016142 240953466 533088067 1028134429 504727014 371307863 834428873 968387878 478550336 1047217797 1046651542 777749850 866989319 92995163 251915198 363285573 10...

output:


result:


Subtask #7:

score: 0
Skipped

Dependency #3:

0%

Subtask #8:

score: 0
Skipped

Dependency #7:

0%

Subtask #9:

score: 0
Skipped

Dependency #8:

0%