QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#19645#2472. Counting Haybaleswlzhouzhuan100 ✓598ms126100kbC++177.4kb2022-02-07 13:13:222022-05-06 06:34:14

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-05-06 06:34:14]
  • 评测
  • 测评结果:100
  • 用时:598ms
  • 内存:126100kb
  • [2022-02-07 13:13:22]
  • 提交

answer

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

#define rep(i,l,r) for(int i=(l);i<=(r);i++)
#define per(i,l,r) for(int i=(l);i>=(r);i--)
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define mset(s,t) memset(s,t,sizeof(s))
#define mcpy(s,t) memcpy(s,t,sizeof(t))
#define SZ(x) ((int)x.size())
#define pb push_back
#define eb emplace_back
#define fir first
#define sec second

template<class T1,class T2>void ckmax(T1 &a,T2 b){if(a<b)a=b;}
template<class T1,class T2>void ckmin(T1 &a,T2 b){if(a>b)a=b;}

inline int read(){
    int x=0,f=0;char ch=getchar();
    while(!isdigit(ch))f|=ch=='-',ch=getchar();
    while(isdigit(ch))x=10*x+ch-'0',ch=getchar();
    return f?-x:x;
}
template<typename T>void print(T x){
    if(x<0)putchar('-'),x=-x;
    if(x>=10)print(x/10);
    putchar(x%10+'0');
}
template<typename T>void print(T x,char ch){
    print(x),putchar(ch);
}

const int N=5002;
const int mod=1e9+7;

inline void add(int &x,int y){
    if((x+=y)>=mod)x-=mod;
}

// bool vis[N][N];
vector<int>adj[N];
int dp[N][N]/*,las1[N],las2[N],las[N],End[N]*/;
int a[N],n;

/*
void dfs(int x,int y){
    if(vis[x][y])return;
    vis[x][y]=1;
    if(x==y){
        if(End[x])return;
        if(las1[x+1]||las2[x+1]){
            deg[x+1][las1[x+1]|las2[x+1]]++;
            dfs(x+1,las1[x+1]|las2[x+1]);
        }else{
            deg[x+1][x+1]++;
            dfs(x+1,x+1);
        }
    }else{
        if(las[x]){
            if(las[x]<y){
                deg[las[x]][y]++;
                dfs(las[x],y);
            }else{
                deg[y][las[x]]++;
                dfs(y,las[x]);
            }
        }else{
            deg[y][y]++;
            dfs(y,y);
        }
        if(las[y]){
            deg[x][las[y]]++;
            dfs(x,las[y]);
        }else{
            deg[x][x]++;
            dfs(x,x);
        }
    }
}
*/

bool g[N][N];
int deg[N];
void link(int x,int y){
    adj[x].pb(y),g[x][y]=1,deg[y]++;
    rep(i,1,n)if(g[y][i])g[x][i]=1;
}
void solve(){
    n=read();rep(i,1,n)a[i]=read(),adj[i].clear(),deg[i]=0;
    rep(i,1,n)rep(j,1,n)dp[i][j]=g[i][j]=0;
    per(i,n,1){
        rep(j,i+1,n){
            if(!g[i][j]&&abs(a[i]-a[j])!=1)
                link(i,j);
        }
    }
    rep(i,1,n)g[i][i]=1;
    rep(i,1,n)rep(j,1,i)g[i][j]=g[j][i];

    int _=2,ans=0;
    while(_<=n&&deg[_])_++;
    // while(_<=n&&abs(a[_]-a[1])!=1)_++;
    if(_>n)dp[1][1]=1;
    else dp[1][_]=1;
    
    // printf("init dp[%d][%d]\n",1,_);

    // rep(i,1,n){
    //     printf("adj[%d]=",i);
    //     for(auto j:adj[i])printf("%d ",j);
    //     puts("");
    // }
    rep(i,1,n){
        rep(j,i+1,n){
            add(dp[i][j],dp[j][i]);
            if(!dp[i][j])continue;
            // printf("dp[%d][%d]=%d\n",i,j,dp[i][j]);
            if(!SZ(adj[i]))add(dp[j][j],dp[i][j]);
            else if(SZ(adj[i])==1){
                int u=adj[i][0];
                if(!g[j][u])add(dp[u][j],dp[i][j]);
                else add(dp[j][j],dp[i][j]);
            }else{
                int u=adj[i][0],v=adj[i][1];
                if(g[u][j]&&g[v][j])add(dp[j][j],dp[i][j]);
                else if(g[u][j])add(dp[v][j],dp[i][j]);
                else if(g[v][j])add(dp[u][j],dp[i][j]);
                else assert(0);
            }

            if(!SZ(adj[j]))add(dp[i][i],dp[i][j]);
            else if(SZ(adj[j])==1){
                int u=adj[j][0];
                if(!g[i][u])add(dp[i][u],dp[i][j]);
                else add(dp[i][i],dp[i][j]);
            }else{
                int u=adj[j][0],v=adj[j][1];
                if(g[u][i]&&g[v][i])add(dp[i][i],dp[i][j]);
                else if(g[u][i])add(dp[i][v],dp[i][j]);
                else if(g[v][i])add(dp[i][u],dp[i][j]);
                else assert(0);
            }
        }
        // printf("dp[%d][%d]=%d\n",i,i,dp[i][i]);
        if(!SZ(adj[i]))add(ans,dp[i][i]);
        else if(SZ(adj[i])==1){
            add(dp[adj[i][0]][adj[i][0]],dp[i][i]);
        }else{
            int x=adj[i][0],y=adj[i][1];
            assert(x<y);
            if(g[x][y])add(dp[x][x],dp[i][i]);
            else add(dp[x][y],dp[i][i]);
        }
    }
/*
    rep(i,1,n)las[i]=las1[i]=las2[i]=End[i]=0;
    map<int,int>mp,mpc;
    per(i,n,1){
        if(mp.count(a[i]))las[i]=mp[a[i]];
        if(mp.count(a[i]+1))las1[i]=mp[a[i]+1];
        if(mp.count(a[i]-1))las2[i]=mp[a[i]-1];
        if(mpc[a[i]-1]+mpc[a[i]+1]==n-i)End[i]=1;
        mp[a[i]]=i,mpc[a[i]]++;
    }
*/
    // printf("init dp[%d][%d]\n",1,_);
    
    /*
    rep(x,1,n){
        rep(y,1,n){
            if(x==y){
                if(las1[x+1]||las2[x+1])
                    deg[x+1][las1[x+1]|las2[x+1]]++;
                else
                    deg[x+1][x+1]++;
            }else{
                if(las[x]){
                    if(las[x]<y)deg[las[x]][y]++;
                    else deg[y][las[x]]++;
                }else{
                    deg[y][y]++;
                }
                if(las[y])deg[x][las[y]]++;
                else deg[x][x]++;
            }
        }
    }
    */
    /*
    queue<pii>q;
    rep(x,1,n)rep(y,x,n)if(!deg[x][y])q.push({x,y});
    int ans=0;
    while(!q.empty()){
        int x=q.front().fir,y=q.front().sec;q.pop();
        if(!dp[x][y])continue;
        printf("now dp[%d][%d]=%d\n",x,y,dp[x][y]);
        if(x==y){
            if(End[x])add(ans,dp[x][x]);
            if(las1[x+1]||las2[x+1]){
                add(dp[x+1][las1[x+1]|las2[x+1]],dp[x][y]);
                if(--deg[x+1][las1[x+1]|las2[x+1]]==0)
                    q.push({x+1,las1[x+1]|las2[x+1]});
            }else{
                add(dp[x+1][x+1],dp[x][y]);
                if(--deg[x+1][x+1]==0)
                    q.push({x+1,x+1});
            }
        }else{
            if(las[x]){
                if(las[x]<y){
                    add(dp[las[x]][y],dp[x][y]);
                    if(--deg[las[x]][y]==0)q.push({las[x],y});
                }else{
                    add(dp[y][las[x]],dp[x][y]);
                    if(--deg[y][las[x]]==0)q.push({y,las[x]});
                }
            }else{
                add(dp[y][y],dp[x][y]);
                if(--deg[y][y]==0)q.push({y,y});
            }
            if(las[y]){
                add(dp[x][las[y]],dp[x][y]);
                if(--deg[x][las[y]]==0)q.push({x,las[y]});
            }else{
                printf("in!\n");
                add(dp[x][x],dp[x][y]);
                if(--deg[x][x]==0)q.push({x,x});
            }
        }
    }
    */
    /*
    rep(x,1,n){
        rep(y,x,n){
            if(x==y){
                if(las1[x+1]||las2[x+1])
                    assert(!las1[x+1]||!las2[x+1]),
                    add(dp[x+1][las1[x+1]+las2[x+1]],dp[x][y]);
                else
                    add(dp[x+1][x+1],dp[x][y]);
            }else{
                // choose x
                if(las[x]){
                    if(las[x]<y)add(dp[las[x]][y],dp[x][y]);
                    else add(dp[y][las[x]],dp[x][y]);
                }
                // choose y
                if(las[y])add(dp[x][las[y]],dp[x][y]);
                if(!las[x]&&!las[y]){
                    add(dp[y][y],dp[x][y]);
                    add(dp[x][x],dp[x][y]);
                }
            }
        }
    }
    */
    print(ans,'\n');
}

int main(){
    int T=read();
    while(T--)solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 4.7619
Accepted
time: 3ms
memory: 3772kb

input:

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

output:

4
4
5
15
9
8
19

result:

ok 7 lines

Test #2:

score: 4.7619
Accepted
time: 3ms
memory: 3776kb

input:

10
10
447773962 773442532 122816 137572579 324627123 157577940 253498609 99147813 425825313 199995380
10
416515986 416515986 416515987 416515987 416515988 416515988 416515989 416515989 416515988 416515989
10
563229302 563229301 563229301 563229302 563229301 563229300 563229300 563229301 563229302 56...

output:

1
186
210
133
150
133
175
231
155
154

result:

ok 10 lines

Test #3:

score: 4.7619
Accepted
time: 3ms
memory: 3928kb

input:

10
10
468145963 198730372 27838076 590195590 467423861 520495379 451366491 344173378 354694313 165814381
10
219739800 219739801 219739801 219739800 219739799 219739799 219739798 219739798 219739798 219739799
10
568161994 568161995 568161994 568161994 568161994 568161994 568161993 568161994 568161995...

output:

1
186
120
120
51
231
252
115
252
231

result:

ok 10 lines

Test #4:

score: 4.7619
Accepted
time: 598ms
memory: 125964kb

input:

1
5000
2 1 3 3 1 1 2 2 3 2 3 1 1 1 1 1 1 1 2 3 1 1 2 2 2 1 1 2 3 2 2 3 1 3 2 3 1 2 2 2 1 1 3 2 2 1 1 2 2 3 3 3 1 1 1 3 2 3 3 1 3 1 3 1 3 3 3 2 2 2 1 2 2 3 2 3 2 1 1 1 2 2 2 3 1 2 1 1 2 2 3 2 2 3 1 3 1 2 2 1 1 3 1 1 2 3 1 3 2 2 2 2 3 2 3 2 3 3 3 1 2 3 3 2 2 2 2 3 1 2 2 1 1 1 1 3 3 1 1 1 3 2 2 1 3 2 2...

output:

493836655

result:

ok single line: '493836655'

Test #5:

score: 4.7619
Accepted
time: 8ms
memory: 9052kb

input:

10
500
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1...

output:

523068127
490281121
335146668
378222245
662250428
677229935
432072782
2379013
65560564
320300148

result:

ok 10 lines

Test #6:

score: 4.7619
Accepted
time: 14ms
memory: 8960kb

input:

10
500
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101...

output:

523068127
580931200
279753758
9368367
561595432
690890919
369268447
846226737
285457745
261543809

result:

ok 10 lines

Test #7:

score: 4.7619
Accepted
time: 299ms
memory: 126004kb

input:

1
5000
2 3 2 3 6 5 6 8 8 11 11 11 13 14 15 16 17 18 19 21 21 23 24 24 25 25 27 27 29 31 32 33 34 34 35 37 38 39 40 40 40 43 44 43 46 46 48 48 50 50 51 52 52 53 56 55 57 57 58 61 62 62 64 64 64 67 68 69 69 71 72 73 74 75 75 77 76 77 79 81 82 82 84 84 85 87 88 88 89 91 91 93 94 93 94 97 97 97 100 101 ...

output:

290956746

result:

ok single line: '290956746'

Test #8:

score: 4.7619
Accepted
time: 4ms
memory: 4580kb

input:

10
100
1 1 4 2 2 4 3 2 4 2 3 2 3 1 1 2 2 1 2 3 3 4 4 2 2 2 1 3 2 3 4 3 1 2 2 4 4 2 1 4 3 2 4 4 1 1 3 4 4 4 1 2 3 2 4 2 3 1 2 1 1 2 2 3 2 4 2 2 2 2 4 2 4 4 2 1 3 1 2 1 2 3 2 2 3 2 2 4 2 2 3 2 2 4 4 4 1 2 3 2
100
2 3 2 3 3 3 3 2 2 2 1 3 1 2 3 4 3 3 3 3 4 2 3 4 1 1 2 3 3 3 1 4 4 1 4 2 4 3 2 3 4 4 1 3 2...

output:

762655378
567764360
228518291
538721530
87546958
974544188
16895117
983492342
273849796
708122840

result:

ok 10 lines

Test #9:

score: 4.7619
Accepted
time: 0ms
memory: 4556kb

input:

10
100
4 1 4 1 4 2 3 4 1 2 1 3 2 4 2 2 3 4 2 4 3 1 3 3 4 2 3 4 3 1 2 2 2 2 4 4 2 1 1 2 1 2 4 3 1 2 2 1 1 2 1 3 2 4 4 3 2 3 4 1 1 2 2 2 2 3 4 3 4 2 2 1 2 1 2 3 4 1 3 2 1 3 3 1 4 3 1 1 2 4 4 1 4 3 3 2 1 1 2 2
100
1 2 3 2 1 4 1 4 3 3 1 2 3 3 4 3 4 2 2 3 3 2 3 3 3 3 2 2 1 1 1 1 2 2 2 4 1 4 3 1 3 3 3 4 2...

output:

46441250
54311602
250886215
794423726
648710286
557560030
81113960
925275253
859511541
923517702

result:

ok 10 lines

Test #10:

score: 4.7619
Accepted
time: 3ms
memory: 4548kb

input:

10
100
1 2 3 2 2 1 1 2 1 1 1 4 2 1 1 1 1 2 2 2 1 2 4 2 4 3 1 4 3 3 3 1 4 1 3 4 3 3 3 4 2 3 3 3 4 3 2 4 4 2 4 4 4 1 1 1 3 1 4 2 3 1 4 4 4 4 1 3 1 3 4 4 4 1 4 3 2 1 2 1 3 2 2 3 2 1 2 4 4 4 1 4 3 2 3 2 4 2 1 4
100
3 1 3 1 2 2 1 2 1 3 1 1 4 1 1 1 1 1 2 3 2 4 3 2 4 1 2 4 4 2 1 1 2 4 1 4 2 1 2 1 2 3 2 2 1...

output:

621199694
33992159
596987282
686898161
413935763
385374226
400768792
8955878
825420054
312467980

result:

ok 10 lines

Test #11:

score: 4.7619
Accepted
time: 2ms
memory: 4552kb

input:

10
100
8 3 1 5 7 5 8 9 5 6 3 1 2 8 8 3 2 3 1 2 7 7 10 9 1 4 6 9 9 2 7 1 8 9 3 8 10 6 8 2 6 5 2 8 4 6 7 2 5 9 5 2 7 3 6 1 7 1 7 8 9 1 6 7 10 4 4 6 1 4 4 5 2 1 9 9 1 5 7 2 6 8 9 2 2 8 9 9 4 4 8 6 3 9 9 10 4 5 2 9
100
4 3 1 2 9 9 2 7 4 6 10 5 10 4 5 6 9 2 3 2 6 4 10 7 6 4 6 1 1 6 4 2 1 1 4 3 4 7 8 3 1 ...

output:

2488320
629856000
258048
20321280
183500786
64512
335923200
3175200
13996800
906992640

result:

ok 10 lines

Test #12:

score: 4.7619
Accepted
time: 2ms
memory: 4528kb

input:

10
100
193563111 193563110 193563109 193563108 193563109 193563108 193563109 193563109 193563110 193563111 193563110 193563110 193563109 193563111 193563110 193563109 193563108 193563110 193563109 193563107 193563108 193563110 193563110 193563110 193563109 193563110 193563108 193563107 193563106 193...

output:

725199468
872897576
382289091
242465517
742404204
320509008
320509008
338635045
538992043
742404204

result:

ok 10 lines

Test #13:

score: 4.7619
Accepted
time: 4ms
memory: 4580kb

input:

10
100
165531093 165531092 165531090 165531090 165531088 165531089 165531089 165531090 165531091 165531090 165531090 165531091 165531089 165531089 165531089 165531087 165531088 165531088 165531086 165531086 165531085 165531084 165531084 165531083 165531084 165531083 165531084 165531084 165531085 165...

output:

977126526
360509330
504170368
74201732
273521609
382289091
120934403
137205613
742404204
137205613

result:

ok 10 lines

Test #14:

score: 4.7619
Accepted
time: 39ms
memory: 16520kb

input:

5
1000
835051605 835051606 835051607 835051608 835051609 835051609 835051608 835051608 835051609 835051610 835051609 835051609 835051609 835051610 835051610 835051609 835051609 835051608 835051607 835051607 835051606 835051606 835051606 835051606 835051605 835051606 835051606 835051606 835051604 835...

output:

93384863
568698435
969293251
826421027
573539193

result:

ok 5 lines

Test #15:

score: 4.7619
Accepted
time: 37ms
memory: 16408kb

input:

5
1000
551842461 551842460 551842459 551842459 551842457 551842457 551842459 551842458 551842456 551842455 551842456 551842458 551842459 551842457 551842457 551842458 551842456 551842456 551842455 551842454 551842452 551842451 551842453 551842451 551842453 551842453 551842453 551842454 551842453 551...

output:

167959558
386342361
519316672
524108839
744133081

result:

ok 5 lines

Test #16:

score: 4.7619
Accepted
time: 41ms
memory: 16412kb

input:

5
1000
911411059 911411057 911411057 911411055 911411055 911411053 911411054 911411055 911411056 911411055 911411055 911411055 911411054 911411055 911411056 911411058 911411058 911411058 911411057 911411058 911411059 911411059 911411060 911411060 911411060 911411059 911411059 911411059 911411060 911...

output:

749921974
933593379
262392129
475137418
229257102

result:

ok 5 lines

Test #17:

score: 4.7619
Accepted
time: 29ms
memory: 16492kb

input:

5
1000
239756971 239756970 239756971 239756970 239756969 239756970 239756969 239756970 239756969 239756968 239756969 239756968 239756969 239756969 239756968 239756967 239756966 239756966 239756967 239756966 239756966 239756966 239756966 239756966 239756967 239756965 239756966 239756967 239756967 239...

output:

205752624
180116804
708871014
425431808
621863530

result:

ok 5 lines

Test #18:

score: 4.7619
Accepted
time: 300ms
memory: 126032kb

input:

1
5000
316394141 316394140 316394140 316394141 316394141 316394141 316394141 316394140 316394140 316394141 316394140 316394141 316394141 316394140 316394141 316394141 316394141 316394140 316394140 316394139 316394140 316394139 316394139 316394139 316394139 316394140 316394140 316394140 316394139 316...

output:

651398981

result:

ok single line: '651398981'

Test #19:

score: 4.7619
Accepted
time: 332ms
memory: 126100kb

input:

1
5000
698334028 698334028 698334027 698334028 698334028 698334028 698334028 698334027 698334027 698334028 698334028 698334028 698334027 698334027 698334027 698334028 698334028 698334027 698334028 698334028 698334027 698334028 698334028 698334027 698334028 698334027 698334028 698334027 698334028 698...

output:

192833632

result:

ok single line: '192833632'

Test #20:

score: 4.7619
Accepted
time: 385ms
memory: 126036kb

input:

1
5000
104725913 104725912 104725912 104725912 104725912 104725913 104725913 104725913 104725913 104725912 104725913 104725913 104725912 104725912 104725912 104725912 104725913 104725912 104725913 104725912 104725913 104725913 104725913 104725913 104725913 104725912 104725913 104725912 104725913 104...

output:

433813305

result:

ok single line: '433813305'

Test #21:

score: 4.7619
Accepted
time: 597ms
memory: 125980kb

input:

1
5000
631500634 631500634 631500634 631500633 631500634 631500633 631500634 631500634 631500633 631500633 631500634 631500634 631500634 631500634 631500633 631500633 631500635 631500634 631500634 631500634 631500635 631500635 631500634 631500634 631500634 631500634 631500635 631500634 631500634 631...

output:

218954931

result:

ok single line: '218954931'