QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#95763 | #5826. 错排 | william555 | 1 | 163ms | 178516kb | C++14 | 1.4kb | 2023-04-11 20:44:35 | 2023-04-11 20:44:39 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5,mod=998244353;
inline int add(int a,int b){return a+b>=mod?a+b-mod:a+b;}
inline int mul(int a,int b){return 1ll*a*b%mod;}
inline int qpow(int a,int b){
int c=1;
for(;b;b>>=1,a=mul(a,a))if(b&1)c=mul(c,a);
return c;
}
int fac[N],ifac[N];
int n,m;
int f[5005][5005],ans[5005][5005];
void dp(){
f[0][0]=f[1][1]=1;
for(int i=2;i<=5000;i++){
for(int j=0;j<=i;j++){
if(j)f[i][j]=add(f[i-1][j-1],mul(i-1,add(f[i-1][j-1],f[i-2][j-1])));
f[i][j]=add(f[i][j],mul(i-1,add(f[i-1][j],f[i-2][j])));
}
}
for(int i=0;i<=5000;i++){
for(int j=0;j<<1<=i;j++){
ans[i][j]=mul(f[i-j][j],fac[j]);
}
}
}
int solve(int n,int m){
if(m<<1>n)return 0;
int ans=0;
for(int i=0;i<=n-m-m;i++){
int val=1ll*fac[n-m]*fac[n-m-i]%mod*ifac[i]%mod*ifac[n-m-m-i]%mod;
if(i&1)ans=add(ans,mod-val);
else ans=add(ans,val);
}
return ans;
}
int main(){
fac[0]=1;
for(int i=1;i<=200000;i++)fac[i]=mul(fac[i-1],i);
ifac[200000]=qpow(fac[200000],mod-2);
for(int i=200000;i>=1;i--)ifac[i-1]=mul(ifac[i],i);
int T;scanf("%d",&T);
// while(T--)solve();
dp();
while(T--){
int n,m;scanf("%d%d",&n,&m);
if(n<=5000&&m<=5000)printf("%d\n",f[n][m]);
else printf("%d\n",solve(n,m));
}
return 0;
}
详细
Subtask #1:
score: 1
Accepted
Test #1:
score: 1
Accepted
time: 85ms
memory: 175796kb
input:
0
output:
result:
ok 0 number(s): ""
Subtask #2:
score: 0
Wrong Answer
Test #2:
score: 0
Wrong Answer
time: 105ms
memory: 178516kb
input:
10 8 6 5 1 4 2 6 3 8 1 3 1 6 2 3 1 4 1 6 2
output:
866880 265 84 8520 133496 9 5430 9 44 5430
result:
wrong answer 1st numbers differ - expected: '0', found: '866880'
Subtask #3:
score: 0
Time Limit Exceeded
Test #7:
score: 0
Time Limit Exceeded
input:
200000 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10 0 11 0 12 0 13 0 14 0 15 0 16 0 17 0 18 0 19 0 20 0 21 0 22 0 23 0 24 0 25 0 26 0 27 0 28 0 29 0 30 0 31 0 32 0 33 0 34 0 35 0 36 0 37 0 38 0 39 0 40 0 41 0 42 0 43 0 44 0 45 0 46 0 47 0 48 0 49 0 50 0 51 0 52 0 53 0 54 0 55 0 56 0 57 0 58 0 59 0 60 0 61...
output:
0 1 2 9 44 265 1854 14833 133496 1334961 14684570 176214841 294304226 127281753 910981941 600290115 222488424 11814221 224470198 496426549 442513998 751108780 305347938 340640042 530046225 804025262 745550660 910531421 451058030 554564312 221339670 95158970 145512950 954462889 464137465 737039093 31...
result:
Subtask #4:
score: 0
Wrong Answer
Test #8:
score: 0
Wrong Answer
time: 163ms
memory: 175532kb
input:
200000 4303 1473 1276 72 967 234 3619 984 1316 384 2679 50 4426 1744 3782 1179 4919 4 805 63 3933 158 1574 528 1277 435 3826 915 2739 68 2286 349 3017 527 3036 476 4280 1764 1504 686 4584 917 1379 145 4764 2178 1881 45 4808 1565 3663 165 4730 2209 2258 103 4181 1687 1636 770 4339 1173 2355 777 3201 ...
output:
874894380 4553296 970141776 637997692 259274314 655073041 207797779 827622440 433553695 173759194 853068418 281561372 345033310 366514831 729852241 700032180 236818823 481865246 829092407 498240959 782046208 836132688 444117215 54033306 320005616 207785865 653418982 369940639 25337075 450054278 2194...
result:
wrong answer 1st numbers differ - expected: '855518783', found: '874894380'
Subtask #5:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%
Subtask #6:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%