QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#389949#8047. DFS Order 45abAC ✓87ms6244kbC++201.8kb2024-04-14 21:32:292024-04-14 21:32:29

Judging History

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

  • [2024-04-14 21:32:29]
  • 评测
  • 测评结果:AC
  • 用时:87ms
  • 内存:6244kb
  • [2024-04-14 21:32:29]
  • 提交

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;
			if (j != jl)
				f[i][j + 1] = (f[i][j + 1] - (sm + f[i - 1][j]) % 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,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4 114514199

output:

2

result:

ok 1 number(s): "2"

Test #2:

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

input:

10 998244353

output:

11033

result:

ok 1 number(s): "11033"

Test #3:

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

input:

100 1000000007

output:

270904395

result:

ok 1 number(s): "270904395"

Test #4:

score: 0
Accepted
time: 72ms
memory: 5992kb

input:

756 1001338769

output:

901942543

result:

ok 1 number(s): "901942543"

Test #5:

score: 0
Accepted
time: 79ms
memory: 6220kb

input:

793 1009036033

output:

301770320

result:

ok 1 number(s): "301770320"

Test #6:

score: 0
Accepted
time: 69ms
memory: 5992kb

input:

759 1005587659

output:

846376219

result:

ok 1 number(s): "846376219"

Test #7:

score: 0
Accepted
time: 70ms
memory: 6092kb

input:

773 1007855479

output:

1398019

result:

ok 1 number(s): "1398019"

Test #8:

score: 0
Accepted
time: 64ms
memory: 6032kb

input:

751 1006730639

output:

321287237

result:

ok 1 number(s): "321287237"

Test #9:

score: 0
Accepted
time: 75ms
memory: 6184kb

input:

778 1007760653

output:

430322899

result:

ok 1 number(s): "430322899"

Test #10:

score: 0
Accepted
time: 87ms
memory: 6244kb

input:

798 1007543827

output:

688720826

result:

ok 1 number(s): "688720826"

Test #11:

score: 0
Accepted
time: 85ms
memory: 6108kb

input:

796 1004841413

output:

258829347

result:

ok 1 number(s): "258829347"

Test #12:

score: 0
Accepted
time: 66ms
memory: 6044kb

input:

775 1005185189

output:

744278608

result:

ok 1 number(s): "744278608"

Test #13:

score: 0
Accepted
time: 82ms
memory: 6116kb

input:

800 1006012831

output:

508549367

result:

ok 1 number(s): "508549367"

Test #14:

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

input:

1 1001338769

output:

1

result:

ok 1 number(s): "1"

Test #15:

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

input:

2 1001338769

output:

1

result:

ok 1 number(s): "1"

Test #16:

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

input:

9 1009036033

output:

1780

result:

ok 1 number(s): "1780"

Test #17:

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

input:

14 1001338769

output:

43297358

result:

ok 1 number(s): "43297358"

Extra Test:

score: 0
Extra Test Passed