QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#834124#9561. 树数叔术ningago100 ✓78ms37736kbC++145.1kb2024-12-27 11:32:472024-12-27 11:32:47

Judging History

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

  • [2024-12-27 11:32:47]
  • 评测
  • 测评结果:100
  • 用时:78ms
  • 内存:37736kb
  • [2024-12-27 11:32:47]
  • 提交

answer

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <map>
#include <cmath>
#include <cctype>
#include <set>

namespace uvu
{
#define LOCAL ____________DONT_DEFINE_ME____________
#define ll long long
#define inf 0x3f3f3f3f
// #define int long long
// #define inf 0x3f3f3f3f3f3f3f3fll
#define infll 0x3f3f3f3f3f3f3f3fll
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define gline debug("now is #%d\n", __LINE__)
#define pii std::pair <int, int>
#define mkp std::make_pair
#define fi first
#define se second
char _ST_;
const int BUFSIZE = (1 << 20);
char ibuf[BUFSIZE], *iS = ibuf, *iT = ibuf;
char obuf[BUFSIZE], *oS = obuf, *oT = obuf + BUFSIZE;
char getc()
{
#ifdef LOCAL
	return getchar();
#else
	if(iS == iT) iT = (iS = ibuf) + fread(ibuf, 1, BUFSIZE, stdin);
	return iS == iT ? EOF : *iS++;
#endif
#define getchar ERR
}

void Flush() { fwrite(obuf, 1, oS - obuf, stdout); oS = obuf; }
struct Flusher { ~Flusher(){ Flush(); } }iamflusher;

void putc(char c)
{
#ifdef LOCAL
	putchar(c);
#else
	*oS++ = c;
	if(oS == oT) Flush();
#endif
#define putchar ERR
}

template <typename T = int> T read()
{
	T x = 0, f = 1; char c = getc();
	for(; !isdigit(c); c = getc()) if(c == '-') f = -1;
	for(;  isdigit(c); c = getc()) x = (x << 3) + (x << 1) + (c ^ 48);
	return x * f;
}

template <typename T> void print(T x, char c)
{
static int sta[BUFSIZE], top;
	top = 0;
	if(x < 0) putc('-'), x = -x;
	if(!x) sta[top = 1] = 0;
	for(; x; x /= 10) sta[++top] = x % 10;
	for(; top; ) putc(sta[top--] ^ 48);
	if(c) putc(c);
}

int readstr(char *s, int base)
{
	int idx = base - 1; char c = getc();
	for(; !(isdigit(c) || isalpha(c) || c == '#' || c == '.'); c = getc());
	for(;   isdigit(c) || isalpha(c) || c == '#' || c == '.' ; c = getc()) s[++idx] = c;
	return idx - base + 1;
}

void printf(const char *s) { for(; *s; s++) putc(*s); }
template <typename T, typename ... Args>
void printf(const char *s, T x, Args ... rest)
{
	for(; *s; s++)
	{
		if(*s != '%') { putc(*s); continue; }
		s++; if(*s == 'd') print(x, 0);
		else if(*s == 'c') putc(x);
		printf(s + 1, rest ...);
		return;
	}
}

template <typename T> void ckmax(T &x, T y) { x = x > y ? x : y; }
template <typename T> void ckmin(T &x, T y) { x = x < y ? x : y; }
// #define mod 998244353
// #define mod 1000000007
int mod;
int sm(int x) { return x >= mod ? x - mod : x; }
void plus_(int &x, int y) { x = sm(x + y); }
void mul_(int &x, int y) { x = 1ll * x * y % mod; }
int ksm(int a, int b) { int res = 1; for(; b; b >>= 1, mul_(a, a)) if(b & 1) mul_(res, a); return res; }

#define N 160
int n, m, ans;
int a[N], cnt[N];
int dp[N][N][N];
// i mex j
// mex' j'

void calc()
{
	if(a[1] != 0) return;
	for(int i = 0; i <= m; i++) cnt[i] = 0;
	int mex = 0, res = 1;
	for(int i = 1; i <= n; i++) 
	{
		if(a[i] < mex) return;
		cnt[a[i]]++;
		int las = i == 1 ? -1 : mex;
		while(cnt[mex]) mex++;
		if(mex != las) mul_(res, i);
	}
	if(mex != m + 1) return;
	// printf("a : "); for(int i = 1; i <= n; i++) print(a[i], ' '); printf("res = %d\n", res);
	plus_(ans, res);
}

void dfs(int s)
{
	if(s > n) { calc(); return; }
	for(int i = 0; i <= m; i++) a[s] = i, dfs(s + 1);
}

int f[N][N], C[N][N], tmp[N][N][N], invf[N], fac[N];

void solve()
{
	// memset(h, idx = -1, sizeof(h));
	n = read(), m = read(), mod = read();
	for(int i = fac[0] = 1; i <= n; i++) fac[i] = 1ll * fac[i - 1] * i % mod;
	invf[n] = ksm(fac[n], mod - 2); for(int i = n - 1; ~i; i--) invf[i] = 1ll * invf[i + 1] * (i + 1) % mod;
	for(int i = 0; i <= N - 10; i++) for(int j = C[i][0] = C[i][i] = 1; j < i; j++) C[i][j] = sm(C[i - 1][j - 1] + C[i - 1][j]);
	if(m >= n) { print(0, '\n'); return; }
	// dfs(1);
	// printf("ans = %d\n", ans);
	ans = 0;
	f[0][0] = 1;
	for(int i = 1; i <= m; i++) for(int j = 1; j <= n; j++)
		for(int k = 1; k <= j; k++) plus_(f[i][j], 1ll * f[i - 1][j - k] * C[j][k] % mod);
	dp[1][1][0] = 1;
	for(int i = 1; i < n; i++)
	{
		for(int j = i; j >= 0; j--)
		{ 
			for(int mex = 1; mex <= m; mex++) //if(dp[i][mex][j])
			{
				plus_(dp[i + 1][mex][j + 1], dp[i][mex][j]);
				plus_(tmp[i][mex + 1][j], 1ll * dp[i][mex][j] * (i + 1) % mod);
				if(!tmp[i][mex][j]) continue;
				// printf("tmp[%d][%d][%d] = %d\n", i, mex, j, tmp[i][mex][j]);
				for(int k = 1; k <= j; k++)
					plus_(tmp[i][mex + 1][j - k], 1ll * tmp[i][mex][j] * C[j][k] % mod);
				// for(int nmex = mex + 1; nmex <= m + 1; nmex++) for(int k = 0; k <= j; k++)
				// 	plus_(dp[i + 1][nmex][j - k], 1ll * dp[i][mex][j] * (i + 1) % mod * f[nmex - mex - 1][k] % mod * C[j][k] % mod);
			}
			for(int mex = 1; mex <= m + 1; mex++) plus_(dp[i + 1][mex][j], tmp[i][mex][j]);
		}
	}
	ans = dp[n][m + 1][0];
	// printf("ans = %d\n", ans);
	for(int i = 1; i < n; i++) mul_(ans, i);
	print(ans, '\n');
}

void init()
{

}

char _ED_;

void mian()
{
	debug("%.3f MB\n", abs(&_ST_ - &_ED_) / 1024.0 / 1024);
	init();
	for(int T = 1; T; solve(), T--);
}

#ifdef int
	#undef int
#endif
}

int main()
{
	// freopen("tmp.in", "r", stdin);
	// freopen("tmp.out", "w", stdout);
	uvu::mian(); return 0;
}

詳細信息

Subtask #1:

score: 5
Accepted

Test #1:

score: 5
Accepted
time: 0ms
memory: 10008kb

input:

1 1 624295285

output:

0

result:

ok single line: '0'

Test #2:

score: 5
Accepted
time: 1ms
memory: 10068kb

input:

4 684813415 564954712

output:

0

result:

ok single line: '0'

Test #3:

score: 5
Accepted
time: 0ms
memory: 9972kb

input:

4 2 844826878

output:

24

result:

ok single line: '24'

Test #4:

score: 5
Accepted
time: 1ms
memory: 9964kb

input:

4 17724073 252218682

output:

0

result:

ok single line: '0'

Test #5:

score: 5
Accepted
time: 2ms
memory: 10044kb

input:

4 3 697681963

output:

384

result:

ok single line: '384'

Subtask #2:

score: 15
Accepted

Test #6:

score: 15
Accepted
time: 2ms
memory: 12124kb

input:

6 4 956647977

output:

238320

result:

ok single line: '238320'

Test #7:

score: 15
Accepted
time: 1ms
memory: 9880kb

input:

6 450615260 491361886

output:

0

result:

ok single line: '0'

Test #8:

score: 15
Accepted
time: 0ms
memory: 10128kb

input:

6 5 339344353

output:

933120

result:

ok single line: '933120'

Test #9:

score: 15
Accepted
time: 1ms
memory: 9976kb

input:

6 3 842228619

output:

23760

result:

ok single line: '23760'

Test #10:

score: 15
Accepted
time: 2ms
memory: 10012kb

input:

6 5 331699652

output:

933120

result:

ok single line: '933120'

Subtask #3:

score: 30
Accepted

Test #11:

score: 30
Accepted
time: 0ms
memory: 11836kb

input:

48 26 424594716

output:

362283012

result:

ok single line: '362283012'

Test #12:

score: 30
Accepted
time: 0ms
memory: 10012kb

input:

49 1000000000 738885247

output:

0

result:

ok single line: '0'

Test #13:

score: 30
Accepted
time: 4ms
memory: 12668kb

input:

48 39 688951620

output:

598399200

result:

ok single line: '598399200'

Test #14:

score: 30
Accepted
time: 1ms
memory: 9936kb

input:

50 476039414 292870080

output:

0

result:

ok single line: '0'

Test #15:

score: 30
Accepted
time: 0ms
memory: 13288kb

input:

50 48 245196368

output:

123576912

result:

ok single line: '123576912'

Subtask #4:

score: 50
Accepted

Test #16:

score: 50
Accepted
time: 0ms
memory: 9964kb

input:

150 526250070 197316869

output:

0

result:

ok single line: '0'

Test #17:

score: 50
Accepted
time: 68ms
memory: 31616kb

input:

149 116 671784452

output:

18945228

result:

ok single line: '18945228'

Test #18:

score: 50
Accepted
time: 59ms
memory: 37176kb

input:

146 144 906402626

output:

438777234

result:

ok single line: '438777234'

Test #19:

score: 50
Accepted
time: 63ms
memory: 36088kb

input:

147 143 705666477

output:

70408701

result:

ok single line: '70408701'

Test #20:

score: 50
Accepted
time: 78ms
memory: 37736kb

input:

150 147 453481175

output:

336290325

result:

ok single line: '336290325'