Bzoj2302 [HAOI2011] Problem C

Description

https://www.luogu.com.cn/problem/P2523

https://www.luogu.com.cn/problem/P1386

双倍经验

水题没做出来,警戒。

Solution

重要性质:每个人的顺序其实并没有关系。入典。

考虑先判断一下无解的情况。

感性理解一下或者根据 Hall 定理,编号 $\ge x$ 的个数都小于等于 $n-x+1$ 则一定有解。否则一定无解。

那么只需要知道每个编号分别有多少,然后组合分配一下即可。

令 $f_{i,j}$ 表示现在确定第 $i-n$ 的编号,确定的编号数为 $j$ 个 。保证有解的情况随便转移一下。

注意到模数不是质数,需要 $O(n^2)$ 递推一下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define maxn 305
#define put() putchar('\n')
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
using namespace std;
void read(int &x){
int f=1;x=0;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
x*=f;
}
namespace Debug{
Tp void _debug(char* f,Ty t){cerr<<f<<'='<<t<<endl;}
Ts void _debug(char* f,Ty x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
Tp ostream& operator<<(ostream& os,vector<Ty>& V){os<<"[";for(auto& vv:V) os<<vv<<",";os<<"]";return os;}
#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
}using namespace Debug;
#define fi first
#define se second
#define mk make_pair
int mod,n,m;
int power(int x,int y=mod-2) {
int sum=1;
while (y) {
if (y&1) sum=sum*x%mod;
x=x*x%mod;y>>=1;
}
return sum;
}
int c[maxn][maxn],f[maxn][maxn],s[maxn];
void solve(void) {
int i,j,k,x,y;
read(n);read(m);read(mod);
for (i=1;i<=n;i++) s[i]=0;
for (i=1;i<=m;i++) read(x),read(y),s[y]++;
for (i=n-1;i>=1;i--) s[i]+=s[i+1];
for (i=1;i<=n;i++) if (s[i]>n-i+1) return puts("NO"),void();
for (i=0;i<=n;i++) {
c[i][0]=1;
for (j=1;j<=i;j++) c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;
}
memset(f,0,sizeof(f));
f[n+1][0]=1;
for (i=n;i>=1;i--) {
for (j=0;j<=n-i+1-s[i];j++) {
for (k=0;k<=j;k++)
f[i][j]=(f[i][j]+f[i+1][j-k]*c[j][k])%mod;
}
}
printf("YES %lld\n",f[1][n-m]);
}
signed main(void){
int T;
read(T);
while (T--) solve();
return 0;
}