QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#501572#8047. DFS Order 4snow_traceAC ✓1965ms10748kbC++141.7kb2024-08-02 20:54:342024-08-02 20:54:35

Judging History

你现在查看的是最新测评结果

  • [2024-08-02 20:54:35]
  • 评测
  • 测评结果:AC
  • 用时:1965ms
  • 内存:10748kb
  • [2024-08-02 20:54:34]
  • 提交

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;return 0;}
	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
*/

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3628kb

input:

4 114514199

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3576kb

input:

10 998244353

output:

11033

result:

ok 1 number(s): "11033"

Test #3:

score: 0
Accepted
time: 4ms
memory: 4068kb

input:

100 1000000007

output:

270904395

result:

ok 1 number(s): "270904395"

Test #4:

score: 0
Accepted
time: 1652ms
memory: 9840kb

input:

756 1001338769

output:

901942543

result:

ok 1 number(s): "901942543"

Test #5:

score: 0
Accepted
time: 1909ms
memory: 10668kb

input:

793 1009036033

output:

301770320

result:

ok 1 number(s): "301770320"

Test #6:

score: 0
Accepted
time: 1679ms
memory: 10600kb

input:

759 1005587659

output:

846376219

result:

ok 1 number(s): "846376219"

Test #7:

score: 0
Accepted
time: 1765ms
memory: 10460kb

input:

773 1007855479

output:

1398019

result:

ok 1 number(s): "1398019"

Test #8:

score: 0
Accepted
time: 1626ms
memory: 10628kb

input:

751 1006730639

output:

321287237

result:

ok 1 number(s): "321287237"

Test #9:

score: 0
Accepted
time: 1809ms
memory: 10496kb

input:

778 1007760653

output:

430322899

result:

ok 1 number(s): "430322899"

Test #10:

score: 0
Accepted
time: 1952ms
memory: 10748kb

input:

798 1007543827

output:

688720826

result:

ok 1 number(s): "688720826"

Test #11:

score: 0
Accepted
time: 1933ms
memory: 9832kb

input:

796 1004841413

output:

258829347

result:

ok 1 number(s): "258829347"

Test #12:

score: 0
Accepted
time: 1787ms
memory: 10192kb

input:

775 1005185189

output:

744278608

result:

ok 1 number(s): "744278608"

Test #13:

score: 0
Accepted
time: 1965ms
memory: 10052kb

input:

800 1006012831

output:

508549367

result:

ok 1 number(s): "508549367"

Test #14:

score: 0
Accepted
time: 0ms
memory: 3616kb

input:

1 1001338769

output:

1

result:

ok 1 number(s): "1"

Test #15:

score: 0
Accepted
time: 0ms
memory: 3508kb

input:

2 1001338769

output:

1

result:

ok 1 number(s): "1"

Test #16:

score: 0
Accepted
time: 1ms
memory: 5588kb

input:

9 1009036033

output:

1780

result:

ok 1 number(s): "1780"

Test #17:

score: 0
Accepted
time: 0ms
memory: 3740kb

input:

14 1001338769

output:

43297358

result:

ok 1 number(s): "43297358"

Extra Test:

score: 0
Extra Test Passed