QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#615414#9449. New School Termucup-team4435#RE 0ms3636kbC++207.7kb2024-10-05 18:29:562024-10-05 18:29:56

Judging History

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

  • [2024-10-05 18:29:56]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3636kb
  • [2024-10-05 18:29:56]
  • 提交

answer

#pragma GCC optimize("Ofast")

#include "bits/stdc++.h"

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
#define rep1n(i, n) for (int i = 1; i <= (n); ++i)
#define repr(i, n) for (int i = (n) - 1; i >= 0; --i)
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define each(x, a) for (auto &x : a)
#define ar array
#define vec vector
#define range(i, n) rep(i, n)

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;

using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pair<int, int>>;
using vvi = vector<vi>;

int Bit(int mask, int b) { return (mask >> b) & 1; }

template<class T>
bool ckmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
bool ckmax(T &a, const T &b) {
    if (b > a) {
        a = b;
        return true;
    }
    return false;
}


const ll INF = 2e18;
const int INFi = 1e9;
const int N = 2e5 + 50;
const int LG = 20;

template<typename T>
int normalize(T value, int mod) {
    if (value < -mod || value >= 2 * mod) value %= mod;
    if (value < 0) value += mod;
    if (value >= mod) value -= mod;
    return value;
}

template<int mod>
struct static_modular_int {
    using mint = static_modular_int<mod>;

    int value;

    static_modular_int() : value(0) {}
    static_modular_int(const mint &x) : value(x.value) {}

    template<typename T, typename U = std::enable_if_t<std::is_integral<T>::value>>
    static_modular_int(T value) : value(normalize(value, mod)) {}

    template<typename T>
    mint power(T degree) const {
        degree = normalize(degree, mod - 1);
        mint prod = 1, a = *this;
        for (; degree > 0; degree >>= 1, a *= a)
            if (degree & 1)
                prod *= a;

        return prod;
    }

    mint inv() const {
        return power(-1);
    }

    mint& operator=(const mint &x) {
        value = x.value;
        return *this;
    }

    mint& operator+=(const mint &x) {
        value += x.value;
        if (value >= mod) value -= mod;
        return *this;
    }

    mint& operator-=(const mint &x) {
        value -= x.value;
        if (value < 0) value += mod;
        return *this;
    }

    mint& operator*=(const mint &x) {
        value = int64_t(value) * x.value % mod;
        return *this;
    }

    mint& operator/=(const mint &x) {
        return *this *= x.inv();
    }

    friend mint operator+(const mint &x, const mint &y) {
        return mint(x) += y;
    }

    friend mint operator-(const mint &x, const mint &y) {
        return mint(x) -= y;
    }

    friend mint operator*(const mint &x, const mint &y) {
        return mint(x) *= y;
    }

    friend mint operator/(const mint &x, const mint &y) {
        return mint(x) /= y;
    }

    mint& operator++() {
        ++value;
        if (value == mod) value = 0;
        return *this;
    }

    mint& operator--() {
        --value;
        if (value == -1) value = mod - 1;
        return *this;
    }

    mint operator++(int) {
        mint prev = *this;
        value++;
        if (value == mod) value = 0;
        return prev;
    }

    mint operator--(int) {
        mint prev = *this;
        value--;
        if (value == -1) value = mod - 1;
        return prev;
    }

    mint operator-() const {
        return mint(0) - *this;
    }

    bool operator==(const mint &x) const {
        return value == x.value;
    }

    bool operator!=(const mint &x) const {
        return value != x.value;
    }

    bool operator<(const mint &x) const {
        return value < x.value;
    }

    template<typename T>
    explicit operator T() {
        return value;
    }

    friend std::istream& operator>>(std::istream &in, mint &x) {
        std::string s;
        in >> s;
        x = 0;
        for (const auto c : s)
            x = x * 10 + (c - '0');

        return in;
    }

    friend std::ostream& operator<<(std::ostream &out, const mint &x) {
        return out << x.value;
    }

    static int primitive_root() {
        if constexpr (mod == 1'000'000'007) return 5;
        if constexpr (mod == 998'244'353) return 3;
        if constexpr (mod == 786433) return 10;

        static int root = -1;
        if (root != -1)
            return root;

        std::vector<int> primes;
        int value = mod - 1;
        for (int i = 2; i * i <= value; i++)
            if (value % i == 0) {
                primes.push_back(i);
                while (value % i == 0)
                    value /= i;
            }

        if (value != 1) primes.push_back(value);
        for (int r = 2;; r++) {
            bool ok = true;
            for (auto p : primes) {
                if ((mint(r).power((mod - 1) / p)).value == 1) {
                    ok = false;
                    break;
                }
            }
            if (ok) return root = r;
        }
    }
};

 constexpr int MOD = 1'000'000'009;
// constexpr int MOD = 998'244'353;
using mint = static_modular_int<MOD>;

mint dp[5005];
int n;
int need;

void add(int x, int y) {
    need -= min(x, y);
    int v = abs(x - y);
    if (!v) return;
    for(int i = n; i >= v; --i) dp[i] += dp[i - v];
}

void del(int x, int y) {
    need += min(x, y);
    int v = abs(x - y);
    if (!v) return;
    for(int i = v; i <= n; ++i) dp[i] -= dp[i - v];
}

void solve() {
    int m; cin >> n >> m;
    vi a(m), b(m);
    rep(i, m) cin >> a[i] >> b[i];
    rep(i, m) {
        a[i]--;
        b[i]--;
    }
    reverse(all(a));
    reverse(all(b));
    need = n;
    dp[0] = 1;
    set<ar<int, 4>> mem;

    vi comp(n * 2);
    vi col(n * 2);
    vector<ar<int, 2>> sz(n * 2, {1, 0});
    rep(i, n * 2) {
        comp[i] = i;
        col[i] = 0;
        add(1, 0);
    }
    auto Merge = [&] (int x, int y) {
        int cx = comp[x];
        int cy = comp[y];
        int v = col[x] == col[y];
        rep(i, n * 2) {
            if (comp[i] == cy) {
                comp[i] = cx;
                col[i] ^= v;
                sz[cx][col[i]]++;
            }
        }
    };
    auto Try = [&] (int x, int y) {
        int cx = comp[x];
        int cy = comp[y];
        if (cx == cy) return;

        ar<int, 4> cur = {sz[cx][col[x]], sz[cx][col[x]^1], sz[cy][col[y]], sz[cy][col[y]^1]};
        {
            auto best = cur;
            for (int mask = 1; mask < 4; ++mask) {
                ar<int, 4> nxt = cur;
                rep(j, 4) nxt[j ^ mask] = cur[j];
                ckmin(best, nxt);
            }
            cur = best;
        }

        if (mem.contains(cur)) return;

        mem.insert(cur);

        del(cur[0], cur[1]);
        del(cur[2], cur[3]);
        add(cur[0] + cur[3], cur[1] + cur[2]);

        if (dp[need] != 0) {
            mem.clear();
            Merge(x, y);
        } else {
            del(cur[0] + cur[3], cur[1] + cur[2]);
            add(cur[0], cur[1]);
            add(cur[2], cur[3]);
        }
    };
    rep(i, m) {
        Try(a[i], b[i]);
    }

    for(int i = 1; i < 2 * n; ++i) {
        Try(0, i);
    }

    assert(accumulate(all(col), 0) == n);
    rep(i, n * 2) {
        cout << col[i];
    }
    cout << '\n';
}


signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
//    cin >> t;
    rep(i, t) {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3636kb

input:

2 4
1 3
2 4
1 4
1 2

output:

0101

result:

ok Output is valid. OK

Test #2:

score: 0
Accepted
time: 0ms
memory: 3548kb

input:

3 7
2 5
1 3
4 6
2 6
4 5
2 4
5 6

output:

011010

result:

ok Output is valid. OK

Test #3:

score: 0
Accepted
time: 0ms
memory: 3636kb

input:

1 0

output:

01

result:

ok Output is valid. OK

Test #4:

score: 0
Accepted
time: 0ms
memory: 3636kb

input:

1 1
1 2

output:

01

result:

ok Output is valid. OK

Test #5:

score: 0
Accepted
time: 0ms
memory: 3616kb

input:

2 3
2 4
3 4
1 2

output:

0110

result:

ok Output is valid. OK

Test #6:

score: 0
Accepted
time: 0ms
memory: 3504kb

input:

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

output:

010101

result:

ok Output is valid. OK

Test #7:

score: -100
Runtime Error

input:

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

output:


result: