QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#533114 | #4436. Link with Bracket Sequence II | xuzhihaodedie# | AC ✓ | 250ms | 5704kb | C++20 | 1.6kb | 2024-08-25 17:26:24 | 2024-08-25 17:26:25 |
Judging History
answer
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int,int>
#define lson 2*p
#define rson 2*p+1
#define x first
#define y second
//#define endl "\n"
const int N=2e5+10;
const int mod=1e9+7;
int n,m;
int dp[505][505];
int a[N];
void solve() {
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i];
if(n&1) {
cout<<0<<endl;
return ;
}
for(int i=0;i<=n;i++) {
for(int j=0;j<=n;j++) {
dp[i][j]=0;
}
}
for(int i=1;i<n;i++) {
if(a[i]!=0&&a[i+1]!=0) {
if(a[i]+a[i+1]==0&&a[i]>0) dp[i][i+1]=1;
} else if(!a[i]&&!a[i+1]) {
dp[i][i+1]=m;
} else if((a[i]>0&&!a[i+1])||(!a[i]&&a[i+1]<0)) {
dp[i][i+1]=1;
}
}
for(int len=4;len<=n;len+=2) {
for(int i=1;i+len-1<=n;i++) {
int j=i+len-1;
for(int k=i+3;k<=j-2;k+=2) {
if(a[i]+a[k]==0&&a[i]>0) dp[i][j]=(dp[i][j]+dp[i+1][k-1]*dp[k+1][j]%mod)%mod;
else if(!a[i]&&!a[k]) dp[i][j]=(dp[i][j]+dp[i+1][k-1]*dp[k+1][j]%mod*m%mod)%mod;
else if((a[i]>0&&!a[k])||(!a[i]&&a[k]<0)) dp[i][j]=(dp[i][j]+dp[i+1][k-1]*dp[k+1][j]%mod)%mod;
}
dp[i][j]=(dp[i][j]+dp[i][i+1]*dp[i+2][j]%mod)%mod;
if(a[i]!=0&&a[j]!=0) {
if(a[i]+a[j]==0&&a[i]>0) dp[i][j]=(dp[i][j]+dp[i+1][j-1])%mod;
} else if(a[i]==0&&a[j]==0) {
dp[i][j]=(dp[i][j]+dp[i+1][j-1]*m%mod)%mod;
} else if((a[i]>0&&!a[j])||(!a[i]&&a[j]<0)) {
dp[i][j]=(dp[i][j]+dp[i+1][j-1])%mod;
}
}
}
cout<<dp[1][n]<<endl;
}
signed main() {
// ios::sync_with_stdio(false);
// cin.tie(0);
// cout.tie(0);
int T=1;
cin>>T;
while(T--) {
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 250ms
memory: 5704kb
input:
20 10 1 1 -1 0 -1 -1 1 -1 1 0 0 10 2 0 1 1 -2 1 -2 -1 1 -2 1 8 5 0 0 4 0 0 2 -2 0 9 5 0 0 0 -3 0 0 0 0 0 8 5 0 1 0 0 0 0 0 0 498 249013689 239722195 0 0 0 -59682797 187213467 0 0 220688278 0 0 -133178217 165866643 -165866643 216987003 55229518 -55229518 -216987003 0 82546192 0 0 0 0 -62330427 -19687...
output:
0 0 75 0 1125 469841384 200768531 102789125 188155310 573855452 1 10742885 839674900 273705999 280134765 397511344 679455456 227852148 343052576 776801212
result:
ok 20 lines