QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#113203#1944. Circle of Friendsrgnerdplayer#WA 22ms38924kbC++204.8kb2023-06-16 17:39:592023-06-16 17:40:00

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-16 17:40:00]
  • 评测
  • 测评结果:WA
  • 用时:22ms
  • 内存:38924kb
  • [2023-06-16 17:39:59]
  • 提交

answer

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

using i64 = long long;

template <int MOD, int ROOT = 3>
struct Modint {
    static constexpr int P = MOD;
    static constexpr int RT = ROOT;

private:
    int v;

    static int minv(int a, int m) {
        a %= m;
        assert(a);
        return a == 1 ? 1 : m - 1LL * minv(m, a) * m / a;
    }

public:
    Modint() : v(0) {}
    Modint(i64 v_) : v(v_ % P) {
        if (v < 0) v += P;
    }
    explicit operator int() const { return v; }
    friend ostream& operator<<(ostream &out, const Modint &n) {
        return out << int(n);
    }
    friend istream& operator>>(istream &in, Modint &n) {
        i64 v;
        in >> v;
        n = Modint(v);
        return in;
    }
    friend string to_string(Modint a) {
        return to_string(int(a));
    }

    friend bool operator==(const Modint &a, const Modint &b) {
        return a.v == b.v;
    }
    friend bool operator!=(const Modint &a, const Modint &b) {
        return a.v != b.v;
    }

    Modint inv() const {
        Modint res;
        res.v = minv(v, P);
        return res;
    }

    Modint operator-() const {
        Modint res;
        res.v = v ? P - v : 0;
        return res;
    }

    Modint& operator++() {
        v++;
        if (v == P) v = 0;
        return *this;
    }
    Modint& operator--() {
        if (v == 0) v = P;
        v--;
        return *this;
    }
    Modint& operator+=(const Modint &o) {
        v -= P - o.v;
        v = (v < 0) ? v + P : v;
        return *this;
    }
    Modint& operator-=(const Modint &o) {
        v -= o.v;
        v = (v < 0) ? v + P : v;
        return *this;
    }
    Modint& operator*=(const Modint &o) {
        v = 1LL * v * o.v % P;
        return *this;
    }
    Modint& operator/=(const Modint &o) { return *this *= o.inv(); }

    friend Modint operator++(Modint &a, int) {
        Modint r = a;
        ++a;
        return r;
    }
    friend Modint operator--(Modint &a, int) {
        Modint r = a;
        --a;
        return r;
    }

    friend Modint operator+(const Modint &a, const Modint &b) {
        return Modint(a) += b;
    }
    friend Modint operator-(const Modint &a, const Modint &b) {
        return Modint(a) -= b;
    }
    friend Modint operator*(const Modint &a, const Modint &b) {
        return Modint(a) *= b;
    }
    friend Modint operator/(const Modint &a, const Modint &b) {
        return Modint(a) /= b;
    }
    
    Modint qpow(i64 p) {
        Modint res = 1, x = v;
        while (p > 0) {
            if (p & 1) res *= x;
            x *= x;
            p >>= 1;
        }
        return res;
    }
};

constexpr int P = 998244353;
using Mint = Modint<P>;

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    auto solve = [&]() {
        int n;
        cin >> n;

        vector<i64> a(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }

        {
            i64 all = -1;
            for (int i = 0; i < n; i++) {
                all &= a[i];
            }
            if (all != 0) {
                cout << Mint(2).qpow(n) - n << '\n';
                return;
            }
        }

        Mint ans = 0;

        auto work = [&](vector<i64> a) {
            int n = a.size();
            int lg = __lg(n) + 1;
            vector f(lg + 1, vector<i64>(n));
            f[0] = a;
            for (int i = 1; i <= lg; i++) {
                for (int j = 0; j + (1 << i) <= n; j++) {
                    f[i][j] = f[i - 1][j] & f[i - 1][j + (1 << i - 1)];
                }
            }

            vector<Mint> dp(n + 1), suf(n + 2);
            dp[n] = suf[n] = 1;

            for (int i = n - 1; i >= 0; i--) {
                int j = i;
                for (int b = lg, cur = -1; b >= 0; b--) {
                    if (j + (1 << b) <= n && (cur & f[b][j]) != 0) {
                        cur &= f[b][j];
                        j += 1 << b;
                    }
                }
                dp[i] = suf[i + 1] - suf[j + 1];
                suf[i] = suf[i + 1] + dp[i];
            }

            ans += dp[0];

            int p = 0, cur = a.back();

            while (p < n && (cur & a[p]) == a.back()) {
                cur &= a[p];
                ans += dp[++p];
            }
            
            cur &= a[p++];
            a.back() = cur;
            return vector<i64>(a.begin() + p, a.end());
        };

        while (!count(a.begin(), a.end(), 0)) {
            auto b = work(a);
            swap(a, b);
        }

        cout << ans << '\n';
    };
    
    solve();
    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3428kb

input:

1
1

output:

1

result:

ok single line: '1'

Test #2:

score: 0
Accepted
time: 1ms
memory: 3456kb

input:

1
1152921504606846975

output:

1

result:

ok single line: '1'

Test #3:

score: 0
Accepted
time: 7ms
memory: 4580kb

input:

200000
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1...

output:

792053081

result:

ok single line: '792053081'

Test #4:

score: 0
Accepted
time: 2ms
memory: 4608kb

input:

200000
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606...

output:

792053081

result:

ok single line: '792053081'

Test #5:

score: -100
Wrong Answer
time: 22ms
memory: 38924kb

input:

200000
18031990763159554
108095195748368386
36029346783428610
184085739649376272
577639437895209218
72057594042191904
4508276314083330
9078671805521920
144255925580990466
432490704060547904
72059793094738017
562967269607456
26424786302976
68719476738
333275855713337352
11370333479109156
290271086772...

output:

727664561

result:

wrong answer 1st lines differ - expected: '697849429', found: '727664561'