Accoders 3.7 省选模拟

· · 个人记录

split

不难看出n充分大时的构造是平凡的……但在出题人良心地下发完整checker.exe的情况下,为什么动作就不能更快一点呢?

容易想到:一层一层地分裂,每次保留一个。如果无法分裂了,设当前还需rem,若rem \neq 1再分裂一个之前保留的球即可。

如果rem = 1,我们撤销最后一次分裂,则rem = x \;or\;x + 1。(x为分裂到的层数)n充分大时x也充分大,这时分裂\lfloor x/2 \rfloor与\lceil x/2 \rceil即可。

n比较小的时候(在这里,就是n \leq 11的情况),层数比较小(不超过4),需要单独处理。

既然checker能帮你确定是否有解,那么问题就不大了……

#include <bits/stdc++.h>

#define For(i, a, b) for (int i = a; i <= b; ++i)
#define rFor(i, b, a) for (int i = b; i >= a; --i)
#define eFor(i, u, v) for (int i = head[u], v = e[i].to; i; i = e[i].next, v = e[i].to)

typedef long long ll;

using std::cin;
using std::cout;
using std::endl;

typedef std::pair<int, int> pii;
#define fi first
#define se second

std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());

template <typename T>
T myrand(T l, T r) {
    return std::uniform_int_distribution<T>(l, r)(rnd);
}

void close_stdio() {
    std::ios::sync_with_stdio(false);
    std::cerr << "DO NOT use scanf/printf!\n";
}

const int kD = 1e7 + 5;
int T;
ll n, c[kD];

int main() {
    freopen("split.in", "r", stdin);
    freopen("split.out", "w", stdout);
    scanf("%d", &T);
    while (T--) {
        scanf("%lld", &n);
        if (n == 3 || n == 5 || n == 8) {
            printf("-1\n");
            continue;
        }
        if (n == 1) {
            printf("1\n1 1\n");
            continue;
        }
        if (n == 11) {
            printf("4\n");
            For(i, 2, 5) printf("%d %d\n", i, (i == 5) + i - 1);
            continue;
        }
        ll rem = n - 2, x = 2;
        c[2] = 2;
        while (rem) {
            if (rem / x >= (c[x] - 1)) {
                rem -= (c[x] - 1) * x;
                c[x + 1] = (c[x] - 1) * (x + 1);
                c[x++] = 1;
                continue;
            }
            ll t = rem / x;
            rem %= x, c[x] -= t, c[x + 1] = t * (x + 1);
            if (rem == 1) {
                if (t == 0) {
                    ++c[x - 1], c[x] -= x;
                    rem = x;
                    // [2, x - 1]
                    ll p = x / 2, q = x - p;
                    if (p == q)
                        --p, ++q;
                    assert(p >= 2 && q < x);
                    assert(c[p] && c[q]);
                    --c[p], c[p + 1] += p + 1;
                    --c[q], c[q + 1] += q + 1;
                } else {
                    ++c[x], c[x + 1] -= x + 1;
                    rem = x + 1;
                    // [2, x]
                    ll p = rem / 2, q = rem - p;
                    if (p == q)
                        --p, ++q;
                    assert(p >= 2 && q <= x);
                    assert(c[p] && c[q]);
                    --c[p], c[p + 1] += p + 1;
                    --c[q], c[q + 1] += q + 1;
                }
            } else if (rem) {
                --c[rem], c[rem + 1] += rem + 1;
            }
            break;
        }
        int tot = 0;
        For(i, 1, x + 1) if (c[i])++ tot;
        printf("%d\n", tot);
        For(i, 1, x + 1) if (c[i]) printf("%d %lld\n", i, c[i]), c[i] = 0;
    }
    return 0;
}
/* PAY ATTENTION TO: */
/* 1. Memory Limit, Array Size */
/* 2. Integer Overflow */
/* 3. Multi-test */
/* 4. Potential Constant-speedup */
/* Stay organized and write things down. */

future

因为开小数组白给32分……

首先把三种字母对应0、1、2,那么操作就是a_x \leftarrow -(a_x + a_{x+1}) \pmod 3。

负号可以最后一次再乘上去。

对每个a算贡献。上面的操作可以描述为:在环上走,每次可以选择走或不走,总共m步,顺时针前进k的方案数。

也就是c_i = \sum_j {m \choose nj+i} \pmod 3​。这个式子不难用类似lucas的方法计算,最后断环为链卷一次即可。

#include <bits/stdc++.h>

#define For(i, a, b) for (int i = a; i <= b; ++i)
#define rFor(i, b, a) for (int i = b; i >= a; --i)
#define eFor(i, u, v) for (int i = head[u], v = e[i].to; i; i = e[i].next, v = e[i].to)

typedef long long ll;

using std::cin;
using std::cout;
using std::endl;

typedef std::pair<int, int> pii;
#define fi first
#define se second

std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());

template <typename T>
T myrand(T l, T r) {
    return std::uniform_int_distribution<T>(l, r)(rnd);
}

void close_stdio() {
    std::ios::sync_with_stdio(false);
    std::cerr << "DO NOT use scanf/printf!\n";
}

const int kN = 3e6 + 5;
const int P = 998244353;
const int bn[][3] = { { 1 }, { 1, 1 }, { 1, 2, 1 } };

int n, lev, c[kN], a[kN], d[40][kN / 2];
char s[kN];
ll m, sav[40];

ll qpow(ll b, int k) {
    ll res = 1;
    for (; k; k >>= 1, b = b * b % P)
        if (k & 1)
            res = res * b % P;
    return res;
}

int norm(int x) { return x >= P ? x - P : x; }

int rev[kN];

void NTT(int *f, int len, bool idft) {
    For(i, 0, len - 1) {
        rev[i] = rev[i >> 1] >> 1;
        if (i & 1)
            rev[i] |= (len >> 1);
        if (rev[i] < i)
            std::swap(f[i], f[rev[i]]);
    }
    for (int h = 2; h <= len; h <<= 1) {
        int wn = qpow(3, (P - 1) / h);
        for (int i = 0; i < len; i += h) {
            ll w = 1, t = 1;
            for (int j = 0; j < (h >> 1); ++j, w = w * wn % P) {
                t = w * f[i + j + (h >> 1)] % P;
                f[i + j + (h >> 1)] = norm(f[i + j] + P - t), f[i + j] = norm(f[i + j] + t);
            }
        }
    }
    if (idft) {
        ll inv = qpow(len, P - 2);
        std::reverse(f + 1, f + len);
        For(i, 0, len - 1) f[i] = f[i] * inv % P;
    }
}

int main() {
    freopen("future.in", "r", stdin);
    freopen("future.out", "w", stdout);
    scanf("%d%lld", &n, &m);
    scanf("%s", s);
    std::reverse(s, s + n);
    For(i, n, n * 2 - 1) s[i] = s[i - n];
    For(i, 0, n * 2 - 1) {
        if (s[i] == 'r')
            a[i] = 0;
        else if (s[i] == 'g')
            a[i] = 1;
        else
            a[i] = 2;
    }
    for (ll t = m; t; t /= 3) {
        sav[lev++] = t;
    }
    d[lev][0] = 1;
    rFor(i, lev - 1, 0) {
        ll m = sav[i];
        For(j, 0, n - 1) {
            if (j > m)
                continue;
            d[i][j] = (bn[m % 3][j % 3] * d[i + 1][j / 3] + bn[m % 3][(j + n) % 3] * d[i + 1][(j + n) / 3] +
                       bn[m % 3][(j + n * 2) % 3] * d[i + 1][(j + n * 2) / 3]) %
                      3;
        }
    }
    // For(i, 0, n * 2 - 1) fprintf(stderr, "a[%d]=%d\n", i, a[i]);
    For(i, 0, n - 1) c[i] = d[0][i];  // , fprintf(stderr, "c[%d]=%d\n", i, c[i]);
    int len = 1;
    while (len < n * 3 - 1) len <<= 1;
    NTT(c, len, false), NTT(a, len, false);
    // For(i, 0, len - 1) {
    //  fprintf(stderr, "after NTT, a[%d] = %d, c[%d] = %d\n", i, a[i], i, c[i] );
    // }
    For(i, 0, len - 1) a[i] = (ll)a[i] * c[i] % P;
    NTT(a, len, true);
    For(i, n, n * 2 - 1) {
        a[i] %= 3;
        // fprintf(stderr, "a[%d]=%d\n", i, a[i]);
        if (m & 1)
            a[i] = (3 - a[i]) % 3;
    }
    rFor(i, n * 2 - 1, n) {
        if (a[i] == 0)
            putchar('r');
        else if (a[i] == 1)
            putchar('g');
        else
            putchar('b');
    }
    return 0;
}
/* PAY ATTENTION TO: */
/* 1. Memory Limit, Array Size */
/* 2. Integer Overflow */
/* 3. Multi-test */
/* 4. Potential Constant-speedup */
/* Stay organized and write things down. */

然而我是小丑,根本不用卷。

直接在一开始把m和步数同时拆位。搞一个类似完全背包的东西即可。

只可意会不可言传,贴一个代码。

memory

一开始,毛估估,把连通性和连通块大小最小表示哈希一下,理论得分至少有65。

然而这个东西写起来实在是太恶心了……于是越写越摆,最后搞了个裸暴力上去。

有点梦想,不要哈希,用一个map<pair<vector<int>, vector<int> >, int>就能过了。

至少状态数是不会特别大的,输出了一下大概几十万的样子。

这个map一看调用开销就特别大,用一个(int &, +=)的取模优化(大概可以少一次find)能快整整一倍。(不用的效果是:跑得奇慢,但时限5s,它就是能过)

计时函数一定要删。

#include <bits/stdc++.h>

#define For(i, a, b) for (int i = a; i <= b; ++i)
#define rFor(i, b, a) for (int i = b; i >= a; --i)
#define eFor(i, u, v) for (int i = head[u], v = e[i].to; i; i = e[i].next, v = e[i].to)

typedef long long ll;

using std::cin;
using std::cout;
using std::endl;

typedef std::pair<int, int> pii;
#define fi first
#define se second

std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());

template <typename T>
T myrand(T l, T r) {
    return std::uniform_int_distribution<T>(l, r)(rnd);
}

void close_stdio() {
    std::ios::sync_with_stdio(false);
    std::cerr << "DO NOT use scanf/printf!\n";
}

const int kN = 45;
const int kM = 7;
const int kS = (1 << kM) + 5;
const int P = 998244353;
const int dx[] = { 1, -1, 0, 0 };
const int dy[] = { 0, 0, 1, -1 };

int n, m, a[kN][kN], p[kN][kS];

#define isok(x, y) (x >= 0 && x < n && y >= 0 && y < m)

namespace ZO {

bool check() {
    For(i, 0, n - 1) For(j, 0, m - 1) if (a[i][j] != 0 && a[i][j] != 1) return false;
    return true;
}

int tot = 0, mx = 0, c[kN][kN], hd = 1, tl = 0;
pii que[kN];

void bfs(int sx, int sy) {
    que[hd = tl = 1] = { sx, sy };
    int sz = 1;
    c[sx][sy] = ++tot;
    while (hd <= tl) {
        int x = que[hd].fi, y = que[hd].se;
        hd++;
        For(k, 0, 3) if (isok(x + dx[k], y + dy[k]) && a[x + dx[k]][y + dy[k]] && !c[x + dx[k]][y + dy[k]]) {
            c[x + dx[k]][y + dy[k]] = tot;
            ++sz;
            que[++tl] = { x + dx[k], y + dy[k] };
        }
    }
    mx = std::max(mx, sz);
}

int solve() {
    For(i, 0, n - 1) For(j, 0, m - 1) if (a[i][j] && !c[i][j]) bfs(i, j);
    return mx;
}

}  // namespace ZO

typedef std::pair<std::vector<int>, std::vector<int> > Node;
std::map<Node, int> d[2];  // Awe.

int f[kM], hd[kM];
int getf(int x) { return x == f[x] ? x : (f[x] = getf(f[x])); }
void merge(int x, int y) {
    x = getf(x), y = getf(y);
    if (x ^ y)
        f[x] = y;
}

// double tadd = 0;

Node operator+(const Node &cur, const int &msk) {
    // double tim = clock();
    Node res;
    res.fi.resize(m, -1), res.se.resize(m + 1, 0);
    For(i, 0, m - 1) f[i] = i;
    memset(hd, -1, sizeof(hd));
    For(i, 0, m - 1) if (msk >> i & 1) {
        if (cur.fi[i] != -1) {
            if (hd[cur.fi[i]] == -1)
                hd[cur.fi[i]] = i;
            else
                merge(i, hd[cur.fi[i]]);
        }
        if (i > 0 && (msk >> (i - 1) & 1))
            merge(i, i - 1);
    }
    For(i, 0, m - 1) if (msk >> i & 1) {
        res.fi[i] = getf(i);
        ++res.se[res.fi[i]];
        if (cur.fi[i] != -1 && hd[cur.fi[i]] == i) {
            res.se[res.fi[i]] += cur.se[i];
        }
    }
    For(i, 0, m - 1) if (msk >> i & 1) res.se[i] = res.se[res.fi[i]];
    res.se[m] = cur.se[m];
    For(i, 0, m - 1) res.se[m] = std::max(res.se[m], res.se[i]);
    // tadd += clock() - tim;
    return res;
}

void reduce(int &x) {
    if (x >= P)
        x -= P;
}

int main() {
    freopen("memory.in", "r", stdin);
    freopen("memory.out", "w", stdout);
    // freopen("in", "r", stdin);
    scanf("%d%d", &n, &m);
    For(i, 0, n - 1) For(j, 0, m - 1) scanf("%d", &a[i][j]);
    if (n < m) {
        For(i, 0, n - 1) For(j, i + 1, m - 1) std::swap(a[i][j], a[j][i]);
        std::swap(n, m);
    }
    if (ZO::check()) {
        printf("%d", ZO::solve());
        return 0;
    }
    For(i, 0, n - 1) {
        For(msk, 0, (1 << m) - 1) {
            p[i][msk] = 1;
            For(j, 0, m - 1) {
                if (msk >> j & 1)
                    p[i][msk] = (ll)p[i][msk] * a[i][j] % P;
                else
                    p[i][msk] = (ll)p[i][msk] * (P + 1 - a[i][j]) % P;
            }
        }
    }
    int cur = 0;
    Node vec0;
    vec0.fi.resize(m, -1);
    vec0.se.resize(m + 1, 0);
    d[0][vec0] = 1;
    // double tr = 0, tim;
    // int sta = 0;
    For(i, 0, n - 1) {
        // tim = clock();
        For(msk, 0, (1 << m) - 1) if (p[i][msk]) {
            for (auto &u : d[cur]) {
                // ++sta;
                reduce(d[cur ^ 1][u.fi + msk] += (ll)p[i][msk] * u.se % P);
            }
        }
        d[cur].clear(), cur ^= 1;
        // tr += clock() - tim;
    }
    ll ans = 0;
    for (auto u : d[cur]) (ans += (ll)u.se * u.fi.se[m]) %= P;
    // fprintf(stderr, "tadd=%lf\ntr=%lf\nsta=%d\n", tadd / CLOCKS_PER_SEC, tr / CLOCKS_PER_SEC, sta);
    printf("%lld", ans);
    return 0;
}
/* PAY ATTENTION TO: */
/* 1. Memory Limit, Array Size */
/* 2. Integer Overflow */
/* 3. Multi-test */
/* 4. Potential Constant-speedup */
/* Stay organized and write things down. */