QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#113213#1944. Circle of Friendsrgnerdplayer#AC ✓790ms39920kbC++205.1kb2023-06-16 17:50:002023-06-16 17:50:04

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:50:04]
  • 评测
  • 测评结果:AC
  • 用时:790ms
  • 内存:39920kb
  • [2023-06-16 17:50:00]
  • 提交

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;
                i64 cur = -1;
                for (int b = lg; 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];
            }

            // for (int i = 0; i < n; i++) {
            //     cout << a[i] << " \n"[i == n - 1];
            // }
            // for (int i = 0; i < n; i++) {
            //     cout << dp[i] << " \n"[i == n - 1];
            // }

            ans += dp[0];

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

            while (true) {
                i64 prv = cur;
                cur &= a[p];
                if (cur != prv) {
                    break;
                }
                ans += dp[++p];
            }

            a.back() = cur;
            return vector<i64>(a.begin() + p + 1, a.end());
        };

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

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

詳細信息

Test #1:

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

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: 4604kb

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: 22ms
memory: 4620kb

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: 0
Accepted
time: 31ms
memory: 39860kb

input:

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

output:

697849429

result:

ok single line: '697849429'

Test #6:

score: 0
Accepted
time: 51ms
memory: 39852kb

input:

200000
38878877261704467
606583538776039621
325557318447172624
513696235393139082
288309595180245073
22000404187620950
28465258695911754
432534927738538528
217932069505099992
8444275091048961
378456596858036610
909745820721741897
10707049231951490
869769773255361289
1054197601224687632
2974146034978...

output:

472024961

result:

ok single line: '472024961'

Test #7:

score: 0
Accepted
time: 90ms
memory: 39848kb

input:

200000
231835759121035702
510237390901680560
1089228954142887951
546561500990948259
452812301317437416
674492659073810407
610376405449845423
336095507987037999
1104321677521773827
257064906190991194
551711390461204674
320506912224540602
57990152440076744
845010071877357771
403229887439671683
2693178...

output:

117345155

result:

ok single line: '117345155'

Test #8:

score: 0
Accepted
time: 149ms
memory: 39888kb

input:

200000
1041523633558056719
1133745259996491747
395753651864850428
1151926721450571413
1114422491360386046
1152921229520204786
1116662492370558395
1006273041715269578
86101463265049534
359143197109542363
242065648916576479
903956062749589338
1091893347132366767
670735077154993663
1142330444352434159
...

output:

648136624

result:

ok single line: '648136624'

Test #9:

score: 0
Accepted
time: 279ms
memory: 39776kb

input:

200000
1148136361283288575
864128178497519103
504403123704430591
1080722898202656703
1150660907089100671
1148417885111055871
1133781167519004367
1152921229712162815
1152902245931548635
1152846703454314239
1152358485925492735
1134766329820016511
1080863910560530363
1071715973758713727
468354552857362...

output:

21378100

result:

ok single line: '21378100'

Test #10:

score: 0
Accepted
time: 708ms
memory: 39920kb

input:

200000
576460752303419383
1150669704793159679
1152921504606846847
1152921504598458367
1143914030474199039
1152921504606846975
1152921504606846967
1152921504590069759
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152921504606846975
1152640029630136319
11529215046068...

output:

557285638

result:

ok single line: '557285638'

Test #11:

score: 0
Accepted
time: 726ms
memory: 39836kb

input:

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

output:

438039103

result:

ok single line: '438039103'

Test #12:

score: 0
Accepted
time: 790ms
memory: 39920kb

input:

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

output:

440098984

result:

ok single line: '440098984'

Test #13:

score: 0
Accepted
time: 450ms
memory: 39580kb

input:

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

output:

413438309

result:

ok single line: '413438309'