QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#606263#1944. Circle of FriendsNafeeszxAC ✓1861ms36648kbC++203.2kb2024-10-02 23:59:192024-10-02 23:59:19

Judging History

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

  • [2024-10-02 23:59:19]
  • 评测
  • 测评结果:AC
  • 用时:1861ms
  • 内存:36648kb
  • [2024-10-02 23:59:19]
  • 提交

answer

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define trav(a, x) for(auto& a : x)
#define FOR(i, a, b) for (int i=(a); i<=(signed)(b); i++)
#define ROF(i, a, b) for (int i=(a); i>=(signed)(b); i--)
#define F0R(i, a) for (int i=0; i<(signed)(a); i++)
#define vi vector<int>
#define f first
#define s second
#define all(v) (v).begin(), (v).end()
typedef long long ll;
using P = pair<int, int>;

const ll mod = 998244353;

template <typename T>
struct sparse_table{
    vector<vector<T>> ST1;
    sparse_table(vector<T> &A){
        int N = A.size();
        int LOG = 32 - __builtin_clz(N);
        ST1 = vector<vector<T>>(LOG, vector<T>(N));
        for (int i = 0; i < N; i++){
            ST1[0][i] = A[i];
        }
        for (int i = 0; i < LOG - 1; i++){
            for (int j = 0; j < N - (1 << i); j++){
                ST1[i + 1][j] = (ST1[i][j] & ST1[i][j + (1 << i)]);
            }
        }
    }
    T range_and(int L, int R){
        int d = 31 - __builtin_clz(R - L);
        return ST1[d][L] & ST1[d][R - (1 << d)];
    }
};

struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

int main() 
{	
    ios_base::sync_with_stdio(0); cin.tie(0);
    int n; cin >> n;
    vector<ll> a(n);
    ll sm = ~0LL;
    F0R(i, n) {cin >> a[i]; sm&=a[i];}
    mint res;
    bool first = true;
    int i = n-1;
    while(i>=0) {
        vector<ll> curr;
        F0R(j, i+1) curr.push_back(a[j]);
        sparse_table<ll> ST(curr);
        vector<mint> dp(i+2);
        dp[0]=1;
        F0R(l, i+1) {
            int lo = -1, hi = l;
            while(hi-lo>1) {
                int mid = (hi+lo)/2;
                if(ST.range_and(mid, l+1)) hi = mid;
                else lo = mid;
            }
            dp[l+1] = dp[l]-(lo>=0?dp[lo]:mint(0)) + dp[l];
        }
        res += dp[i+1]-dp[i];
        while(i > 0 && (a[i]&a[0])==a[0]) {i--; res += dp[i+1]-dp[i];}
        if(i) {
            a[0] = (a[0]&a[i]);
        }
        i--;    

        if(a[0]==0) break;
    }
    //cout << res << "\n";
    if(sm) res -= (n-1);
    cout << res << "\n"; 
    return 0;
}   

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

1
1

output:

1

result:

ok single line: '1'

Test #2:

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

input:

1
1152921504606846975

output:

1

result:

ok single line: '1'

Test #3:

score: 0
Accepted
time: 36ms
memory: 36100kb

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: 42ms
memory: 36216kb

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: 80ms
memory: 36428kb

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: 124ms
memory: 36420kb

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: 180ms
memory: 36628kb

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: 307ms
memory: 36432kb

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: 748ms
memory: 36448kb

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: 1575ms
memory: 36648kb

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: 1861ms
memory: 36416kb

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: 1660ms
memory: 36436kb

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: 775ms
memory: 36212kb

input:

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

output:

413438309

result:

ok single line: '413438309'