QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#389945#8047. DFS Order 45abAC ✓177ms6224kbC++201.9kb2024-04-14 21:28:222024-04-14 21:28:22

Judging History

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

  • [2024-04-14 21:28:22]
  • 评测
  • 测评结果:AC
  • 用时:177ms
  • 内存:6224kb
  • [2024-04-14 21:28:22]
  • 提交

answer

/* name: 8047
 * author: 5ab
 * created at: 2024-04-14
 */
#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define ssz(x) (int((x).size()))

auto chmax = [](auto& x, auto y) { if (x < y) x = y; };
auto chmin = [](auto& x, auto y) { if (y < x) x = y; };

using ll = long long;
const int N = 800;

int f[N + 1][N + 1], fac[N + 1], ifac[N + 1], inv[N + 1];

ll qpow(ll bs, ll ix, int mod)
{
	ll mul = bs, ret = 1;
	while (ix)
	{
		if (ix & 1)
			ret = (ret * mul) % mod;
		mul = (mul * mul) % mod;
		ix >>= 1;
	}
	return ret;
}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int n, mod;
	
	cin >> n >> mod;
	ll mmod = 1ll * mod * mod;
	
	fac[0] = 1;
	for (int i = 0; i < n; i++)
		fac[i + 1] = 1ll * fac[i] * (i + 1) % mod;
	ifac[n] = qpow(fac[n], mod - 2, mod);
	for (int i = n; i > 0; i--)
	{
		ifac[i - 1] = 1ll * ifac[i] * i % mod;
		inv[i] = 1ll * ifac[i] * fac[i - 1] % mod;
	}
	
	f[0][1] = f[1][0] = f[1][1] = 1;
	for (int i = 2; i <= n; i++)
	{
		int jl = min(i, n - i + 1);
		for (int j = 1; j <= min(i - 1, n - i + 1); j++)
			f[i][j - 1] = 1ll * f[i - 1][j] * inv[i] % mod;
		for (int j = 1; j <= jl; j++)
		{
			ll sm = 0;
			for (int k = 2; k < i; k++)
			{
				sm += 1ll * f[k - 1][1] * f[i - k][j];
				if (sm >= mmod) sm -= mmod;
			}
			f[i][j] = (f[i][j] + sm % mod * inv[i]) % mod;
		}
		for (int j = 1; j < jl; j++)
		{
			ll sm = 0;
			for (int k = 1; k < i; k++)
			{
				sm += 1ll * f[k - 1][1] * f[i - k][j];
				if (sm >= mmod) sm -= mmod;
			}
			f[i][j + 1] = (f[i][j + 1] - sm % mod * inv[i] % mod + mod) % mod;
		}
		
		f[i][1] = (f[i][1] + f[i][0]) % mod;
		// cerr << i << " ";
		// for (int j = 0; j <= i; j++)
		// 	cerr << 1ll * f[i][j] * fac[i] % mod << " \n"[j == i];
	}
	cout << 1ll * f[n][0] * fac[n] % mod << endl;
	
	return 0;
}
// started coding at: 04-14 19:59:40

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

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3600kb

input:

4 114514199

output:

2

result:

ok 1 number(s): "2"

Test #2:

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

input:

10 998244353

output:

11033

result:

ok 1 number(s): "11033"

Test #3:

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

input:

100 1000000007

output:

270904395

result:

ok 1 number(s): "270904395"

Test #4:

score: 0
Accepted
time: 141ms
memory: 5968kb

input:

756 1001338769

output:

901942543

result:

ok 1 number(s): "901942543"

Test #5:

score: 0
Accepted
time: 161ms
memory: 6196kb

input:

793 1009036033

output:

301770320

result:

ok 1 number(s): "301770320"

Test #6:

score: 0
Accepted
time: 145ms
memory: 6028kb

input:

759 1005587659

output:

846376219

result:

ok 1 number(s): "846376219"

Test #7:

score: 0
Accepted
time: 143ms
memory: 6060kb

input:

773 1007855479

output:

1398019

result:

ok 1 number(s): "1398019"

Test #8:

score: 0
Accepted
time: 150ms
memory: 5948kb

input:

751 1006730639

output:

321287237

result:

ok 1 number(s): "321287237"

Test #9:

score: 0
Accepted
time: 137ms
memory: 6104kb

input:

778 1007760653

output:

430322899

result:

ok 1 number(s): "430322899"

Test #10:

score: 0
Accepted
time: 177ms
memory: 6208kb

input:

798 1007543827

output:

688720826

result:

ok 1 number(s): "688720826"

Test #11:

score: 0
Accepted
time: 163ms
memory: 6208kb

input:

796 1004841413

output:

258829347

result:

ok 1 number(s): "258829347"

Test #12:

score: 0
Accepted
time: 154ms
memory: 6088kb

input:

775 1005185189

output:

744278608

result:

ok 1 number(s): "744278608"

Test #13:

score: 0
Accepted
time: 174ms
memory: 6224kb

input:

800 1006012831

output:

508549367

result:

ok 1 number(s): "508549367"

Test #14:

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

input:

1 1001338769

output:

1

result:

ok 1 number(s): "1"

Test #15:

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

input:

2 1001338769

output:

1

result:

ok 1 number(s): "1"

Test #16:

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

input:

9 1009036033

output:

1780

result:

ok 1 number(s): "1780"

Test #17:

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

input:

14 1001338769

output:

43297358

result:

ok 1 number(s): "43297358"

Extra Test:

score: 0
Extra Test Passed