QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#501568 | #8047. DFS Order 4 | snow_trace | WA | 1966ms | 10860kb | C++14 | 1.7kb | 2024-08-02 20:53:17 | 2024-08-02 20:53:17 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
int mod;
int qp(int p,int q){
int ans = 1,pro = p;
while(q){
if(q&1)ans = ans*pro%mod;
pro = pro*pro%mod;q>>=1;
}
return ans;
}
int n;
int f[1005][1005];
int inv[1005];
signed main(){
cin >> n >> mod;
if(n == 1){cout << 1 << endl;}
inv[0] = 1;for(int i = 1;i<=n;i++)inv[i] = qp(i,mod-2);
f[1][0] = 1;
//代码是抄的,素质比较低,但是可以写一下注释。
//我自己也尝试写了一下,根本写不出来,xtq老师上课讲的是非正解,虽然解法很优美但是细节是真的麻烦
/*
根节点比较特殊,我们把他拿出来特判,也就是在最后加进去。
这里深度的意思是你至少要走 j 步才能走回去,不是真的钦定了深度。
所以你每次可以跳 fa,从 dp_{i-1,0}->dp_{i,0}
然后另一个转移是枚举一个兄弟
你注意到这个兄弟节点不能直接作为一个子树考虑,要考虑他的儿子。
而且他儿子的子树大小应该至少为 1
还有一个转移是走到他兄弟的最后一个儿子
深度会加 1,我们仍然枚举兄弟最后一个儿子的儿子的子树。
但是这里这个子树大小为 0 是合法的,于是对其进行特判。
*/
for(int i = 2;i<=n;i++){
for(int j =0;j<=i;j++){
f[i][j] = f[i-1][j+1]*inv[i]%mod;
if(j == 0)f[i][j] = (f[i-1][j]*inv[i]+f[i][j])%mod;
for(int k = 1;k<i;k++){
f[i][j] = (f[i][j]+f[i-k][j]*f[k-1][0]%mod*inv[i])%mod;
if(j)f[i][j] = (f[i][j]+mod-(f[i-k][j-1]*(f[k-1][0]+(k==1))%mod*inv[i]%mod));
}
}
}
int res = f[n-1][0];
for(int i = 1;i<n;i++)res = res*i%mod;
cout << res << endl;
return 0;
}/*
4 114514199
*/
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3708kb
input:
4 114514199
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3616kb
input:
10 998244353
output:
11033
result:
ok 1 number(s): "11033"
Test #3:
score: 0
Accepted
time: 4ms
memory: 3936kb
input:
100 1000000007
output:
270904395
result:
ok 1 number(s): "270904395"
Test #4:
score: 0
Accepted
time: 1655ms
memory: 10436kb
input:
756 1001338769
output:
901942543
result:
ok 1 number(s): "901942543"
Test #5:
score: 0
Accepted
time: 1909ms
memory: 10232kb
input:
793 1009036033
output:
301770320
result:
ok 1 number(s): "301770320"
Test #6:
score: 0
Accepted
time: 1679ms
memory: 10212kb
input:
759 1005587659
output:
846376219
result:
ok 1 number(s): "846376219"
Test #7:
score: 0
Accepted
time: 1774ms
memory: 8968kb
input:
773 1007855479
output:
1398019
result:
ok 1 number(s): "1398019"
Test #8:
score: 0
Accepted
time: 1626ms
memory: 10488kb
input:
751 1006730639
output:
321287237
result:
ok 1 number(s): "321287237"
Test #9:
score: 0
Accepted
time: 1803ms
memory: 9868kb
input:
778 1007760653
output:
430322899
result:
ok 1 number(s): "430322899"
Test #10:
score: 0
Accepted
time: 1948ms
memory: 10860kb
input:
798 1007543827
output:
688720826
result:
ok 1 number(s): "688720826"
Test #11:
score: 0
Accepted
time: 1966ms
memory: 10828kb
input:
796 1004841413
output:
258829347
result:
ok 1 number(s): "258829347"
Test #12:
score: 0
Accepted
time: 1787ms
memory: 10736kb
input:
775 1005185189
output:
744278608
result:
ok 1 number(s): "744278608"
Test #13:
score: 0
Accepted
time: 1966ms
memory: 10100kb
input:
800 1006012831
output:
508549367
result:
ok 1 number(s): "508549367"
Test #14:
score: -100
Wrong Answer
time: 0ms
memory: 3568kb
input:
1 1001338769
output:
1 0
result:
wrong answer Output contains longer sequence [length = 2], but answer contains 1 elements