QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#853195#9922. Mah-jongruoye123456TL 333ms5832kbC++232.4kb2025-01-11 16:07:342025-01-11 16:07:35

Judging History

This is the latest submission verdict.

  • [2025-01-11 16:07:35]
  • Judged
  • Verdict: TL
  • Time: 333ms
  • Memory: 5832kb
  • [2025-01-11 16:07:34]
  • Submitted

answer

#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define N 100005
#define MOD 998244353
using namespace std;

int c[9];
int b[9];
int w[9] = {0, 1, 3, 15, 105, 735, 5145, 36015, 180075};
bitset<550000> ok;

void check() {
    memcpy(b,c,sizeof(b));
    for(int i=1;i<=6;++i) {
        if(b[i] < 0) return;
        b[i]%=3;
        b[i+1]-=b[i];
        b[i+2]-=b[i];
        b[i]=0;
    }
    if(b[7]>=0 && b[8]>=0 && b[7]%3==0 && b[8]%3==0) {
        int idx = 0;
        for(int i=1;i<=8;++i) idx += w[i]*c[i];
        ok[idx] = 1;
    }
}

void init() {
    for(c[1]=0;c[1]<=2;++c[1])
        for(c[2]=0;c[2]<=4;++c[2])
            for(c[3]=0;c[3]<=6;++c[3])
                for(c[4]=0;c[4]<=6;++c[4])
                    for(c[5]=0;c[5]<=6;++c[5])
                        for(c[6]=0;c[6]<=6;++c[6])
                            for(c[7]=0;c[7]<=4;++c[7])
                                for(c[8]=0;c[8]<=2;++c[8])
                                    check();
}

int n, a[N], sum[N][9];

inline int get_state(int t1, int t2, int t3, int t4, int t5, int t6, int t7, int t8) {
    return w[1]*t1 + w[2]*t2 + w[3]*t3 + w[4]*t4 
         + w[5]*t5 + w[6]*t6 + w[7]*t7 + w[8]*t8;
}

void solve() {
    cin>>n;
    for(int i=1;i<=n;++i) {
        cin>>a[i];
        for(int j=1;j<=8;++j) sum[i][j] = sum[i-1][j];
        ++sum[i][a[i]];
    }
    
    int ans = 0;
    for(int i=1;i<=n;++i) {
        for(int j=i+2;j<=n;j+=3) {
            int t1 = sum[j][1]-sum[i-1][1]; if(t1>=3) t1%=3;
            int t2 = sum[j][2]-sum[i-1][2]; if(t2>=5) t2=(t2-2)%3+2;
            int t3 = sum[j][3]-sum[i-1][3]; if(t3>=7) t3=(t3-4)%3+4;
            int t4 = sum[j][4]-sum[i-1][4]; if(t4>=7) t4=(t4-4)%3+4;
            int t5 = sum[j][5]-sum[i-1][5]; if(t5>=7) t5=(t5-4)%3+4;
            int t6 = sum[j][6]-sum[i-1][6]; if(t6>=7) t6=(t6-4)%3+4;
            int t7 = sum[j][7]-sum[i-1][7]; if(t7>=5) t7=(t7-2)%3+2;
            int t8 = sum[j][8]-sum[i-1][8]; if(t8>=3) t8%=3;
            
            ans += ok[get_state(t1,t2,t3,t4,t5,t6,t7,t8)];
        }
    }
    cout<<ans<<'\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int T;
    cin>>T;
    init();
    
    while(T--) {
        memset(sum[0], 0, sizeof(sum[0]));  // 每个测试用例前清零
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 3708kb

input:

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

output:

2
5
1
3
2

result:

ok 5 number(s): "2 5 1 3 2"

Test #2:

score: 0
Accepted
time: 333ms
memory: 5832kb

input:

100
992
8 1 8 1 2 3 6 6 1 3 1 8 7 7 4 7 7 1 6 6 4 8 3 7 3 5 1 4 4 7 5 7 5 7 4 3 7 5 2 8 7 1 6 3 6 2 4 3 2 3 1 6 3 1 3 2 6 6 7 4 6 1 1 4 6 4 7 7 8 5 6 4 1 5 4 8 2 4 4 2 1 3 5 7 6 8 3 7 6 6 5 6 4 2 5 4 3 7 3 5 5 3 3 2 7 8 2 7 2 4 4 3 4 1 1 3 5 5 4 6 3 3 3 2 6 1 2 6 4 8 8 6 6 8 7 3 1 1 8 8 7 2 5 6 3 5 ...

output:

51699
61486
5146
1960
241675
6274
11224
46170
435241
1219228
17198
139542
299436
960439
217729
1174
87401
141087
69813
1
78895
0
39510
16757
86551
0
100302
161956
3
512157
58619
196941
26116
61635
28879
11511
2061
4394
74620
907389
172780
23952
524
87857
1305332
1413
11784
156368
11746
23217
25189
9...

result:

ok 100 numbers

Test #3:

score: -100
Time Limit Exceeded

input:

1
100000
7 6 3 7 1 2 5 2 4 5 3 2 6 2 2 2 5 6 5 8 6 2 1 8 2 2 1 1 4 8 2 6 4 1 8 6 6 7 8 4 4 5 4 7 8 6 2 3 3 7 5 7 1 1 3 5 2 8 5 6 3 6 2 3 3 8 4 5 7 8 1 5 6 1 3 4 5 7 1 5 4 4 4 6 6 4 2 3 5 2 7 3 5 8 7 1 5 4 5 4 1 5 8 7 2 2 8 2 4 3 5 7 6 6 1 6 6 3 1 1 3 1 7 8 1 7 3 7 8 3 6 3 5 7 5 1 8 7 4 7 5 4 8 1 3 4...

output:


result: