QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#197239 | #4436. Link with Bracket Sequence II | yiyiyi# | AC ✓ | 271ms | 12776kb | C++14 | 1.1kb | 2023-10-02 13:34:33 | 2023-10-02 13:34:34 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define sqr(x) ((x)*(x))
#define N 1000
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
const int mo=1000000007;
inline int read(){
int x=0,w=0;char ch=getchar();
while (!isdigit(ch))w|=ch=='-',ch=getchar();
while (isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return w?-x:x;
}
ll f[N][N][2];
int a[N];
void solve(){
int n=read(),m=read();
// memset(f,0,sizeof(f));
for (int i=1;i<=n;++i)a[i]=read(),f[i+1][i][0]=f[i+1][i][1]=1;
for (int len=2;len<=n;len+=2){
for (int i=1;i+len-1<=n;++i){
int j=i+len-1;//cout<<i<<" "<<j<<"!!"<<endl;
for (int k=0;k<2;++k)
if (a[i]==-a[j]&&a[i]>=0){
if (a[i])f[i][j][k]=f[i+1][j-1][1];
else f[i][j][k]=1ll*f[i+1][j-1][1]*m%mo;
}else{
if (a[i]&&a[j]||a[i]<0||a[j]>0)f[i][j][k]=0;
else f[i][j][k]=f[i+1][j-1][1];
}
for (int k=i+1;k<j-1;k+=2)
f[i][j][1]=(f[i][j][1]+1ll*f[i][k][0]*f[k+1][j][1])%mo;
}
}
cout<<f[1][n][1]<<endl;
}
signed main(){
int T=read();
while (T--)solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 271ms
memory: 12776kb
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