QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#536405 | #8746. 排列游戏 | Kostlin | TL | 0ms | 0kb | C++14 | 2.2kb | 2024-08-29 11:00:43 | 2024-08-29 11:00:44 |
answer
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
#define pii pair<int,int>
#define fi first
#define sc second
#define mkp make_pair
using namespace std;
const int N=500,M=5000,mod=1e9+7;
int T,n,m,f[2][N+5][M+5][2],Ans[M];
vector< pii > qwq[N+5];
inline int read() {
int x=0,fl=0;char ch=getchar();
while(ch<'0'||ch>'9') {fl|=(ch=='-'); ch=getchar();}
while('0'<=ch&&ch<='9') {x=(x<<3)+(x<<1)+ch-'0'; ch=getchar();}
return fl?-x:x;
}
inline int mx(int x,int y) {return x>y?x:y;}
inline int mn(int x,int y) {return x<y?x:y;}
inline void ADD(int &x,int y) {x=(x+y>=mod?x+y-mod:x+y);}
int main() {
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
#endif
T=read();
for(int i=1;i<=T;++i) {
n=read();m=read();
qwq[n].push_back(mkp(m,i));
}
f[0][0][0][0]=1;
for(int i=1;i<=N;++i) {
for(int j=0;j<=i;++j) {
for(int p=0;p<=M;++p) {
if(p>=j) ADD(f[i&1][j][p][0],f[(i-1)&1][j][p-j][1]),ADD(f[i&1][j][p][1],f[(i-1)&1][j][p-j][0]);
if(p>j)ADD(f[i&1][j][p][0],1ll*f[(i-1)&1][j+1][p-j-1][1]*(j+1)*j%mod),
ADD(f[i&1][j][p][1],1ll*f[(i-1)&1][j+1][p-j-1][0]*(j+1)*j%mod);
if(p>j) ADD(f[i&1][j][p][0],1ll*f[(i-1)&1][j+1][p-j-1][0]*(j+1)%mod),
ADD(f[i&1][j][p][1],1ll*f[(i-1)&1][j+1][p-j-1][1]*(j+1)%mod);
if(p>=j) ADD(f[i&1][j][p][0],2ll*f[(i-1)&1][j][p-j][0]*j%mod),ADD(f[i&1][j][p][1],2ll*f[(i-1)&1][j][p-j][1]*j%mod);
if(j&&p>=j-1) ADD(f[i&1][j][p][0],f[(i-1)&1][j-1][p-j+1][1]),ADD(f[i&1][j][p][1],f[(i-1)&1][j-1][p-j+1][0]);
}
}
for(int j=0;j<=i;++j)
for(int p=0;p<=M;++p) {
// if(f[i&1][j][p][0])printf("%d %d %d %d\n",i,j,p,f[i&1][j][p][0]);
// if(f[i&1][j][p][1])printf("%d %d %d %d\n",i,j,p,f[i&1][j][p][1]);
f[(i-1)&1][j][p][0]=f[(i-1)&1][j][p][1]=0;
}
for(auto &qaq:qwq[i])
for(int p=0;p<=qaq.fi;++p)
ADD(Ans[qaq.sc],f[i&1][0][p][0]);
}
for(int i=1;i<=T;++i) printf("%d\n",Ans[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
6 2 1 3 1 5 2 7 5 10 20 15 24