Accoders 3.8 省选模拟

· · 个人记录

3.8 省选模拟

graph

忘记可以重复操作了/kk

首先缩点,如果一个SCC有空点那么总是可以通过反复转动空点完成所有任务。

那么可以网络流:缩点后dag上的不同SCC连边,表示满点的转移;有空的,向空连容量=空位数的边。

如果SCC是满的且必须操作,那么从源点向它连边,容量为1,表示一个跑掉的需求。满的SCC也得向汇点连容量为1的边,表示空一个之后就前驱可以从这里走;同时为了防止自己从自己处走的不合法情况,得新建一个点,源点改为向新建的点连边,避免这种问题。

#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 = 105;
const int kV = 62;
const int kInf = 0x3f3f3f3f;

int kase, n, m;
bool has[kV];
char s[kN];

int hs(char c) {
    if ('a' <= c && c <= 'z')
        return c - 'a';
    else if ('A' <= c && c <= 'Z')
        return c - 'A' + 26;
    else
        return c - '0' + 52;
}

std::vector<int> G[kV];
int dfn[kV], low[kV], clk = 0, sz[kV + 5], rem[kV + 5], scc[kV], deg[kV + 5], cc = 0;
int st[kV + 5], top = 0;

void tarjan(int u) {
    dfn[u] = low[u] = ++clk;
    st[++top] = u;
    for (auto v : G[u]) {
        if (!dfn[v])
            tarjan(v), low[u] = std::min(low[u], low[v]);
        else if (!scc[v])
            low[u] = std::min(low[u], dfn[v]);
    }
    if (dfn[u] == low[u]) {
        ++cc;
        sz[cc] = rem[cc] = 0;
        do {
            scc[st[top]] = cc;
            ++sz[cc];
            rem[cc] += has[st[top]];
        } while (st[top--] != u);
    }
}

struct Arc {
    int next, to, cap;
} e[kV * kV * 4];

int S, T, vtot, head[kV * 4], cur[kV * 4], dis[kV * 4], que[kV * 4], hd = 1, tl = 0, ecnt = 2;

void addEdge(int from, int to, int cap) {
    e[ecnt] = (Arc){ head[from], to, cap };
    head[from] = ecnt++;
    e[ecnt] = (Arc){ head[to], from, 0 };
    head[to] = ecnt++;
}

bool bfs() {
    memset(dis, -1, sizeof(dis));
    dis[que[hd = tl = 1] = S] = 0;
    while (hd <= tl) {
        int u = que[hd++];
        if (u == T) {
            while (hd <= tl) dis[que[hd++]] = -1;
            return true;
        }
        eFor(i, u, v) if (e[i].cap && dis[v] == -1) {
            dis[v] = dis[u] + 1;
            que[++tl] = v;
        }
    }
    return false;
}

int dfs(int u, int r) {
    if (u == T)
        return r;
    int flow = 0, f = 0;
    for (int i = cur[u], v = e[i].to; i; i = e[i].next, v = e[i].to) {
        cur[u] = i;
        if (e[i].cap && dis[v] == dis[u] + 1) {
            f = dfs(v, std::min(r, e[i].cap));
            if (f)
                e[i].cap -= f, r -= f, e[i ^ 1].cap += f, flow += f;
            else
                dis[v] = -1;
        }
    }
    return flow;
}

int maxflow() {
    int res = 0;
    while (bfs()) {
        memcpy(cur, head, sizeof(head));
        res += dfs(S, kInf);
    }
    return res;
}

int main() {
    freopen("graph.in", "r", stdin);
    freopen("graph.out", "w", stdout);
    scanf("%d", &kase);
    while (kase--) {
        scanf("%s%d", s + 1, &m);
        n = strlen(s + 1);
        For(i, 0, kV - 1) G[i].clear();
        memset(has, false, sizeof(has));
        int htot = 0;
        For(i, 1, n) has[hs(s[i])] = true;
        For(i, 0, kV - 1) if (has[i])++ htot;
        char edge[5];
        For(i, 1, m) {
            scanf("%s", edge);
            G[hs(edge[0])].push_back(hs(edge[1]));
        }
        memset(dfn, 0, sizeof(dfn));
        memset(low, 0, sizeof(low));
        memset(scc, 0, sizeof(scc));
        memset(deg, 0, sizeof(deg));
        clk = cc = top = 0;
        For(i, 0, kV - 1) if (!dfn[i]) tarjan(i);
        memset(head, 0, sizeof(head));
        ecnt = 2, S = 0, T = vtot = cc + 1;
        For(i, 0, kV - 1) for (auto j : G[i]) if (scc[i] ^ scc[j]) {
            ++deg[scc[i]];
            addEdge(scc[i], scc[j], kInf);
        }
        For(u, 1, cc) {
            if (rem[u] == sz[u] && (sz[u] > 1 || deg[u])) {
                ++vtot;
                eFor(i, u, v) if (e[i].cap) addEdge(vtot, v, e[i].cap);
                addEdge(S, vtot, 1);
                addEdge(u, T, 1);
            } else
                addEdge(u, T, sz[u] - rem[u]);
        }
        // fprintf(stderr, "htot=%d vtot=%d cc=%d\n", vtot, cc);
        printf("%d\n", htot - (vtot - cc - 1) + maxflow());
    }
    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. */

surface

先转一下坐标系。不难看出总区域数就是交点数-n+射线相交构成的连通块数。

连通块数和交点数很难一起控制,但是注意到,如果连通块数减少一定会带来交点数的增加,所以我们只用最大化交点数即可。

一个不难看出的性质是:一个点如果向斜向上,那么它左上方的点一定水平向右。

这怎么利用呢?“左上方”是偏序关系,这条性质指出,拓扑序的一个“前缀”向右,相应的后缀向上。

拓扑序本身一般难以处理,但是注意如果一个元素在边界上,我们可以确定它的决策:比较左上和右下的点个数即可。

而对于不在边界上的点,因为本题要最大化的值充分独立(数点问题),不难证出也可以通过比较这个个数确定决策。(感性理解:边界上的决策一定是最模糊的)于是就解决了。

#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 = 2005;
const double s3 = sqrt(3);
const double eps = 1e-6;

int n, x[kN], y[kN], xord[kN], f[kN], tot;

inline bool eq(double a, double b) { return std::abs(a - b) < eps; }

bool cmpx(int i, int j) { return s3 * x[i] + y[i] > s3 * x[j] + y[j]; }

bool cmpy(int i, int j) { return -s3 * x[i] + y[i] > -s3 * x[j] + y[j]; }

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, --tot;
}

int dir[kN];

int main() {
    freopen("surface.in", "r", stdin);
    freopen("surface.out", "w", stdout);
    scanf("%d", &n);
    tot = n;
    For(i, 1, n) scanf("%d%d", &x[i], &y[i]);
    For(i, 1, n) xord[i] = f[i] = i;
    std::sort(xord + 1, xord + n + 1, cmpx);
    For(i, 1, n) {
        int lhs = 0, rhs = 0;
        For(j, 1, i - 1) if (cmpy(xord[i], xord[j]))++ lhs;
        For(j, i + 1, n) if (cmpy(xord[j], xord[i]))++ rhs;
        dir[i] = lhs > rhs;  // up if it is one
    }
    int ans = 0;
    For(i, 1, n) For(j, 1, i - 1) if (cmpy(xord[i], xord[j]) && dir[i] && !dir[j]) { merge(i, j), ++ans; }
    fprintf(stderr, "tot=%d\n", tot);
    printf("%d\n", ans - n + tot);
    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. */

count

单选一个物品的OGF:

F(x) = \sum_i {x^{l_i} - x^{r_i + 1} \over 1-x}

现在要选多个物品,加一个元,答案就是(令k = 4)

\begin{aligned} [y^k]\prod_u(1 + yx^u) &=(-1)^k \prod_u(1 - yx^u)\\ &= (-1)^k \exp(\sum_u \ln(1-yx^u)) \\ &= (-1)^k \exp(-\sum_{i>0}{F(x^i)y^i \over i}) \end{aligned}

手动展开(注意,以y为主元)可以得到

ANS = F^4(x) - 6F^2(x)F(x^2) + 3F^2(x^2) + 8F(x)F(x^3) - 6F(x^4)

F是个分式,令G = (1-x)F。

后两项,可以直接卷积后使用(1-x^p)^{-q}的展开式计算答案。

对于前两项,我们枚举一部分的贡献,快速求另一部分。注意,分母必须完全转移到另一部分。

[x^k](1-x)^{-4} G^2(x) = \sum_{0 \leq i \leq k}[x^i] G^2(x){k-i+3\choose 3}

将组合数拆成多项式后做一个前缀和即可。

[x^k](1-x^2)^{-1}(1-x)^{-2}G^2(x) = \sum_{0\leq i \leq k}[x^i]G^2(x)\begin{cases}{(k-i+2)^2 /4} & (2|(k-i)) \\ {(k-i+1)(k-i+3)/4} & (2|(k-i+1))\end{cases}

分奇偶性讨论后方法同上。

#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 = 805;
const int P = 998244353;
const int inv24 = 291154603;
const int inv6 = 166374059;
const int inv4 = 748683265;

int n, s, l[kN], r[kN];

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

typedef std::vector<pii> poly;

poly G;  // \sum x^{l[i]} - x^{r[i] + 1}

void reduce(poly &res) {
    std::sort(res.begin(), res.end());
    static poly tmp;
    tmp.clear();
    for (auto x : res) {
        if (x.fi > s)
            break;
        if (tmp.empty() || x.fi != tmp.back().fi)
            tmp.push_back(x);
        else
            tmp.back().se = norm(tmp.back().se + x.se);
    }
    res.swap(tmp);
}

poly operator*(poly lhs, poly rhs) {
    poly res;
    for (auto x : lhs)
        for (auto y : rhs) {
            res.emplace_back(x.fi + y.fi, (ll)x.se * y.se % P);
        }
    reduce(res);
    return res;
}

void ps(poly &f) {
    int sum = 0;
    for (auto &x : f) {
        sum = norm(sum + x.se);
        x.se = sum;
    }
}

int gett(poly &f, int i, bool strict = false) {
    auto it = std::lower_bound(f.begin(), f.end(), std::make_pair(i + 1, 0));
    if (it == f.begin() || (strict && std::prev(it)->fi != i))
        return 0;
    return std::prev(it)->se;
}

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;
}

namespace Part1 {  // F^4(x)

poly sG, tG[4];
ll coe[4];

ll solve() {
    ll res = 0;
    sG = G * G;
    for (auto x : sG) {
        tG[0].emplace_back(x.fi, (ll)x.se * inv6 % P);
        tG[1].emplace_back(x.fi, (ll)(P - inv6) * x.fi % P * x.se % P);
        tG[2].emplace_back(x.fi, (ll)inv6 * x.fi % P * x.fi % P * x.se % P);
        tG[3].emplace_back(x.fi, (ll)(P - inv6) * x.fi % P * x.fi % P * x.fi % P * x.se % P);
    }
    For(i, 0, 3) ps(tG[i]);
    for (auto x : sG) {
        ll k = s - x.fi;
        // fprintf(stderr, "%d %d\n", x.fi, x.se);
        coe[0] = (k + 1) * (k + 2) % P * (k + 3) % P;
        coe[1] = ((k + 1) * (k + 2) + (k + 2) * (k + 3) + (k + 1) * (k + 3)) % P;
        coe[2] = (3 * k + 6) % P;
        coe[3] = 1;
        For(i, 0, 3)(res += coe[i] * gett(tG[i], k) % P * x.se) %= P;
    }
    return res;
}

ll bf() {
    poly t;
    For(i, 1, n) For(j, l[i], r[i]) t.emplace_back(j, 1);
    t = t * t * t * t;
    return gett(t, s, true);
}

}  // namespace Part1

namespace Part2 {  // F^2(x) * F(x^2)

poly sG, Gs, tG[2][3];
ll coe[2][3];

ll solve() {
    ll res = 0;
    sG = G * G, Gs = G;
    for (auto &x : Gs) x.fi *= 2;
    while (!Gs.empty() && Gs.back().fi > s) Gs.pop_back();
    for (auto x : sG) {
        tG[x.fi & 1][0].emplace_back(x.fi, (ll)x.se * inv4 % P);
        tG[x.fi & 1][1].emplace_back(x.fi, (ll)(P - inv4) * x.fi % P * x.se % P);
        tG[x.fi & 1][2].emplace_back(x.fi, (ll)inv4 * x.fi % P * x.fi % P * x.se % P);
    }
    For(i, 0, 1) For(j, 0, 2) ps(tG[i][j]);
    for (auto x : Gs) {
        ll k = s - x.fi;
        coe[0][0] = (k + 2) * (k + 2) % P;
        coe[0][1] = (2 * k + 4) % P;
        coe[0][2] = 1;
        coe[1][0] = (k + 1) * (k + 3) % P;
        coe[1][1] = (2 * k + 4) % P;
        coe[1][2] = 1;
        For(i, 0, 1) For(j, 0, 2)(res += coe[i][j] * gett(tG[(k & 1) ^ i][j], k) % P * x.se) %= P;
    }
    return res;
}

ll bf() {
    poly t, tt;
    For(i, 1, n) For(j, l[i], r[i]) t.emplace_back(j, 1), tt.emplace_back(j * 2, 1);
    tt = tt * t * t;
    return gett(tt, s, true);
}

}  // namespace Part2

namespace Part3 {  // F^2(x^2)

poly t;

ll solve() {
    if (s & 1)
        return 0;
    ll res = 0;
    t = G;
    for (auto &x : t) x.fi *= 2;
    while (!t.empty() && t.back().fi > s) t.pop_back();
    t = t * t;
    for (auto x : t) {
        (res += (ll)x.se * ((s - x.fi) / 2 + 1)) %= P;
    }
    return res;
}

ll bf() {
    poly t, tt;
    For(i, 1, n) For(j, l[i], r[i]) tt.emplace_back(j * 2, 1);
    tt = tt * tt;
    return gett(tt, s, true);
}

}  // namespace Part3

namespace Part4 {  // F * F(x^3)

poly t;

ll solve() {
    ll res = 0;
    t = G;
    while (!t.empty() && s / 3 < t.back().fi) t.pop_back();
    for (auto &x : t) x.fi *= 3;
    t = t * G;
    for (auto x : t) {
        assert(x.fi <= s);
        (res += (ll)x.se * ((s - x.fi) / 3 + 1)) %= P;
    }
    return res;
}

ll bf() {
    poly t, tt;
    For(i, 1, n) For(j, l[i], r[i]) tt.emplace_back(j * 3, 1), t.emplace_back(j, 1);
    tt = tt * t;
    return gett(tt, s, true);
}

}  // namespace Part4

namespace Part5 {  // F(x^4)

poly t;

ll solve() {
    ll res = 0;
    t = G;
    while (!t.empty() && s / 4 < t.back().fi) t.pop_back();
    for (auto &x : t) x.fi *= 4;
    for (auto x : t)
        if ((s - x.fi) % 4 == 0)
            res += x.se;
    return res % P;
}

}  // namespace Part5

int main() {
    freopen("count.in", "r", stdin);
    freopen("count.out", "w", stdout);
    scanf("%d%d", &n, &s);
    For(i, 1, n) scanf("%d%d", &l[i], &r[i]);
    For(i, 1, n) {
        G.emplace_back(l[i], 1);
        G.emplace_back(r[i] + 1, P - 1);
    }
    std::sort(G.begin(), G.end());
    reduce(G);
    // printf("%lld %lld %lld %lld\n", Part1::bf(), Part2::bf(), Part3::bf(), Part4::bf());
    // printf("%lld %lld %lld %lld\n", Part1::solve(), Part2::solve(), Part3::solve(), Part4::solve());
    printf("%lld", (Part1::solve() + (P - 6) * Part2::solve() + 3 * Part3::solve() + 8 * Part4::solve() +
                    (P - 6) * Part5::solve()) %
                       P * inv24 % P);
    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. */