The DP (i, j) indicates the number of scenarios selected for the J person from the I~n, and the state transition considers how many people I number and then calculates from the I+1 scheme count. Time complexity O (tn^2)
---------------------------------------------------------------------
#include <cstdio>#include <algorithm>#include <cstring>using namespace std;typedef long Long ll;#define C (i) (Cnt[i]-cnt[i + 1]) const int MAXN = 309;const int MAXT = one;int DP[MAXN][MAXN], N, CNT[MAXN];int V[MAXN][MAXN], COMB[MAXT][MAXN][MAXN];int n[maxt], m[maxt], P[maxt], Q[MAXT][MAXN], T, _t;inline void upd (int &x, int t) {if ((x + = t) >= p[_t])x-= p[_t];}void Init () {For (_t = 0; _t < T; _t++) {comb[_t][0][0] = 1;for (int i = 1; I <= n[_t]; i++) {comb[_t][i][0] = 1;For (int j = 1; J <= I; j + +) {Comb[_t][i][j] = comb[_t][i-1][j-1] + comb[_t][i-1][j];if (Comb[_t][i][j] >= p[_t])Comb[_t][i][j]-= p[_t];}}}}void Read () {scanf ("%d%d%d", n + _t, M + _t, P + _t);for (int i = 0, p; i < m[_t]; i++)scanf ("%d%d", &p, q[_t] + i);}inline int C (int n, int m) {if (n < m)return 0;return comb[_t][n][m];}void Work () {N = n[_t];memset (CNT, 0, sizeof CNT);for (int i = 0; i < m[_t]; i++)cnt[q[_t][i]]++;for (int i = N; i; i--)if ((cnt[i] + = cnt[i + 1]) > n-i + 1) {puts ("NO");return;}memset (DP, 0, sizeof DP);if (Cnt[n]) {dp[n][0] = 0;dp[n][1] = 1;} else {dp[n][0] = 1;dp[n][1] = n-cnt[1];}for (int i = N; i.) {For (int j = n-i + 1; j >= Cnt[i]; j--)for (int k = c (i); K <= J; k++)upd (Dp[i][j], LL (Dp[i + 1][j-k]) * C (n-j + k-cnt[1] + cnt[i + 1], k-c (i))% p[_t]);}printf ("YES%d\n", Dp[1][n]);}int main () {scanf ("%d", &t);For (_t = 0; _t < T; _t++) Read ();Init ();For (_t = 0; _t < T; _t++) work ();return 0;}
---------------------------------------------------------------------
2302: [Haoi2011]problem C time limit: Sec Memory Limit: + MB
Submit: 407 Solved: 221
[Submit] [Status] [Discuss]
Description
GiveNthe individual arranges the seat, first gives each person a1~nthe number, set the firstIthe number of the individual isAI(the number of different people can be the same), and then from the first person, we take a seat, firstIafter the individual comes, try to sitAI, ifAIis occupied, tryai+1,ai+1if they're occupied, try.ai+2, ..., if you keep trying toNNo one can, the arrangement is not legal. However there aremthe number of the individual has been determined(They might have bribed your boss....), you can only arrange the number of the remaining people and ask how many legal arrangements you have. Because the answer can be large, just output it by dividing itMafter the remainder can be.
Input
first line an integer T that represents the number of data groups
for each set of data, the first row has three integers representing N , m , M
ifmNot for0, the next line hasmfor integers,P1,Q1,P2,Q2,..., pm,QM, where sectionIon integerPi,Qirepresents the firstPithe number of the individual must beQi
Output
for each set of data output line, if there is a solution output YES , followed by an integer representing the number of scenarios MoD M , note that YES there is only one space between the sum, or the output NO
Sample Input2
4 3 10
1 2 2 1 3 1
10 3 8882
7 9 2 9 5 10
Sample Output
YES 4
NO
HINT
100% of the data meet: 1≤T≤10,1≤N≤300,0≤M≤N,2≤M≤109,1≤PI, qi≤n and ensure that the pi is different.
Source
Bzoj 2302: [Haoi2011]problem C (DP)