Accoders 3.5 省选模拟

· · 个人记录

divisor

观察二维情况,不难得出最大的满足\sum e_k = i​的某个点集一定是最长反链。(只需证存在一个这么大的链剖分即可)

#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 = 2e6 + 5;
const int kW = 40;

ll n, p[kW];
int tot, e[kW], d[kW];

int main() {
    freopen("divisor.in", "r", stdin);
    freopen("divisor.out", "w", stdout);
    scanf("%lld", &n);
    ll t = n;
    for (ll x = 2; x * x <= t; ++x)
        if (t % x == 0) {
            p[++tot] = x;
            while (t % x == 0) {
                ++e[tot];
                t /= x;
            }
        }
    if (t > 1)
        p[++tot] = t, e[tot] = 1;
    int sum = 0;
    For(i, 1, tot) sum += e[i];
    d[0] = 1;
    For(i, 1, tot) {
        rFor(j, sum, 0) {
            For(k, 1, std::min(j, e[i])) d[j] += d[j - k];
            // fprintf(stderr, "d[%d][%d] = %d\n", i, j, d[j]);
        }
    }
    // For(i, 0, sum) fprintf(stderr, "d[%d]=%d\n", i, d[i]);
    int ans = 0;
    For(i, 0, sum) ans = std::max(ans, d[i]);
    printf("%d", 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. */

graph

首先容易想到让L = \prod p_i​,这样只有L+1不连通。它一定是一个孤立点。

同样容易想到:让L贡献一个因子p_x,还给L+1一个因子p_x,使得后者存在连通的希望。

但这样产生了新的孤立点L + p_x,看起来非常没戏。

题目做不出来的时候,不要轻易的否决掉一个方案,尝试更精确、直观的刻画一个方案。

原来的情况形如这样:

转移了一个质因子后,形如

只要保证p_x > \sqrt n,就能满足仅剩孤立点L + p_x。同时,仔细看看上面的图,我们多了一些环!

有蓝边相连的点是L + 1 + kp_x。只要p_x \leq(n-1)/2且kp_x + 1中有某个质数,我们就能干净地割掉一条红边,将其与L + p_x连起来。一种要求相对容易满足的情形是:

\begin{cases} 2p_x + 1 \; is\; prime \\ p_x | (L + 1) \\ (2p_x + 1) | (L + p_x) \end{cases}

事实证明对32 \leq n \leq 10^5总是可以找到这样的p_x。感性理解一下,素数均匀分布,我们只要在一个较大的范围内找到其中的两个即可。

#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 = 1e5 + 5;

int n, p[kN], f[kN], pc = 0, tab[50];
bool np[kN];

int getf(int x) { return x == f[x] ? x : (f[x] = getf(f[x])); }

bool check(int L) {
    For(i, 0, n - 1) f[i] = i;
    For(j, 0, pc - 1) {
        int x = L / p[j] * p[j];
        if (x < L)
            x += p[j];
        for (int y = x + p[j]; y <= L + n - 1; y += p[j]) {
            int fx = getf(x - L), fy = getf(y - L);
            if (fx ^ fy)
                f[fx] = fy;
        }
    }
    For(i, 1, n - 1) if (getf(i) != getf(0)) return false;
    return true;
}

struct bInt {
    static const int kB = 1e8;
    std::vector<ll> a;
    bInt(int x = 0) {
        while (x) {
            a.push_back(x % kB);
            x /= kB;
        }
    }
    void swift() {
        for (int i = 0; i + 1 < a.size(); ++i) {
            a[i + 1] += a[i] / kB;
            a[i] %= kB;
        }
        while (!a.empty() && a.back() >= kB) {
            a.push_back(a.back() / kB);
            a[a.size() - 2] %= kB;
        }
    }
    bInt &operator*=(ll x) {
        // fprintf(stderr, "mul %lld\n", x);
        for (auto &i : a) i *= x;
        swift();
        return *this;
    }
    void disp() {
        if (a.empty())
            printf("0");
        else {
            printf("%lld", a.back());
            if (a.size() > 1)
                for (int i = a.size() - 2; ~i; --i) printf("%.8lld", a[i]);
        }
        putchar('\n');
    }
};

ll exgcd(ll a, ll b, ll &x, ll &y) {
    if (b == 0) {
        x = 1, y = 0;
        return a;
    }
    ll d = exgcd(b, a % b, y, x);
    y -= (a / b) * x;
    return d;
}

int main() {
    freopen("graph.in", "r", stdin);
    freopen("graph.out", "w", stdout);
    scanf("%d", &n);
    if (n <= 16) {
        printf("No solution");
        return 0;
    }
    np[1] = true;
    For(i, 2, n) {
        if (!np[i])
            p[pc++] = i;
        for (int j = 0; j < pc && i * p[j] <= n; ++j) {
            np[i * p[j]] = true;
            if (i % p[j] == 0)
                break;
        }
    }
    if (n <= 40) {
        tab[30] = 151061;
        tab[31] = 151060;
        tab[32] = 151059;
        tab[33] = 151058;
        tab[34] = 7106718;
        tab[35] = 7106718;
        tab[36] = 7567747;
        tab[37] = 7567746;
        tab[38] = 7567745;
        tab[39] = 7567744;
        tab[40] = 7567743;
        if (n >= 30)
            printf("%d", tab[n]);
        else {
            ll prod = 1;
            For(j, 0, pc - 1) prod *= p[j];
            For(i, 1, prod) if (check(i)) {
                printf("%d", i);
                return 0;
            }
        }
        return 0;
    }
    int x = 0, y = 0;
    For(i, 1, n) if ((ll)i * i > n && !np[i] && !np[2 * i + 1]) {
        x = i, y = 2 * i + 1;
        break;
    }
    // fprintf(stderr, "x=%d y=%d\n", x, y);
    ll prod = 1, mod = (ll)x * y, goal = (-3 * x - 1) % mod + mod;
    bInt ans(1);
    For(i, 0, pc - 1) if (p[i] != x && p[i] != y) {
        prod *= p[i], prod %= mod;
        ans *= p[i];
        // ans.disp();
    }
    ll u, v;
    exgcd(prod, mod, u, v);
    (u *= goal) %= mod;
    u += mod;
    // fprintf(stderr, "u=%lld prod=%lld goal=%lld\n", u, prod, goal);
    ans *= u;
    ans.disp();
    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. */

dag

我可能从来没有真正理解根号重构。

每根号个事件放一起。每次询问时,分别算块内的修改(直接枚举)和块外修改的影响。

考虑分开两种操作的影响。对于赋值,我们求出块外,到达某个点的最后一次赋值操作;这样在chkmin时,我们只需对一段区间chkmin,而不需要考虑赋值的干扰。

维护每个块到某个点最小的chkmin(不考虑赋值)以及最后一次赋值。计算时还需对当前块内每个询问,求出可达这个询问的点。传递闭包即可做到O(n^2/w)事件,O(n\sqrt n/w)空间。

卡常:计算传递闭包时剪枝。注意要用bitset::none()而非!bitset::count()。

#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 = 1e5 + 5;
const int kB = 500;
const int kInf = 0x3f3f3f3f;

int n, m, q, bc = 0, que[kN], a[kN], hd = 1, tl = 0;
int ord[kN];
std::vector<int> G[kN], rG[kN];

struct Event {
    int tp, u, x;
} qry[kN];
int beg = 1, clk, minv[kN / kB + 10][kN], lb[kB], rb[kB];
pii lst[kN];
std::bitset<kB> to[kN];

void toposort() {
    static int deg[kN];
    For(i, 1, n) for (auto v : G[i])++ deg[v];
    For(i, 1, n) if (!deg[i]) que[++tl] = i;
    int cur = 0;
    while (hd <= tl) {
        int u = que[hd++];
        ord[++cur] = u;
        for (auto v : G[u])
            if (!--deg[v])
                que[++tl] = v;
    }
}

double tget = 0;
void getTo() {
    double tim = clock();
    For(i, 1, n) to[i].reset();
    For(i, beg, clk) to[qry[i].u][i - beg] = 1;
    rFor(i, n, 2) {
        int u = ord[i];
        if (to[u].none())
            continue;
        for (auto v : rG[u]) to[v] |= to[u];
    }
    tget += clock() - tim;
}

double tans = 0;

void reb() {
    ++bc;
    lb[bc] = beg, rb[bc] = clk;
    getTo();
    double tim = clock();
    For(i, beg, clk) if (qry[i].tp == 3) {
        int u = qry[i].u;
        pii clst = lst[u];
        rFor(j, i - 1, beg) if (qry[j].tp == 1 && to[qry[j].u][i - beg]) {
            clst = std::make_pair(j, qry[j].x);
            break;
        }
        int ans = clst.se, j;
        for (j = i - 1; j >= std::max(clst.fi + 1, beg); --j)
            if (qry[j].tp == 2 && to[qry[j].u][i - beg])
                ans = std::min(ans, qry[j].x);
        for (j = bc - 1; lb[j] > clst.fi; --j) ans = std::min(ans, minv[j][u]);
        if (j) {
            for (int k = clst.fi + 1; k < i && k <= rb[j]; ++k)
                if (qry[k].tp == 2 && to[qry[k].u][i - beg])
                    ans = std::min(ans, qry[k].x);
        }
        printf("%d\n", ans);
    }
    tans += clock() - tim;
    For(i, beg, clk) if (qry[i].tp == 1) {
        lst[qry[i].u] = std::max(lst[qry[i].u], std::make_pair(i, qry[i].x));
    }
    For(i, beg, clk) if (qry[i].tp == 2) {
        minv[bc][qry[i].u] = std::min(minv[bc][qry[i].u], qry[i].x);  // ignore affine
    }
    For(i, 1, n) {
        int u = ord[i];
        for (auto v : G[u]) {
            lst[v] = std::max(lst[v], lst[u]);
            minv[bc][v] = std::min(minv[bc][v], minv[bc][u]);
        }
    }
}

int main() {
    freopen("dag.in", "r", stdin);
    freopen("dag.out", "w", stdout);
    double tim = clock();
    scanf("%d%d%d", &n, &m, &q);
    For(i, 1, m) {
        int u, v;
        scanf("%d%d", &u, &v);
        G[u].push_back(v), rG[v].push_back(u);
    }
    toposort();
    memset(minv, 0x3f, sizeof(minv));
    For(i, 1, q) {
        scanf("%d%d", &qry[i].tp, &qry[i].u);
        if (qry[i].tp != 3)
            scanf("%d", &qry[i].x);
        ++clk;
        if (clk % kB == 0)
            reb(), beg = clk + 1;
    }
    if (clk % kB)
        reb();
    fprintf(stderr, "%lf\n", (clock() - tim) / CLOCKS_PER_SEC);
    fprintf(stderr, "tget %lf\n", tget / CLOCKS_PER_SEC);
    fprintf(stderr, "tans %lf\n", tans / CLOCKS_PER_SEC);
    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. */