title
BZOJ 5219
Description
In Byteland a total of n cities, sequentially numbered from 1 to n, plans to build n (n-1) / 2 unidirectional path therebetween, for any two different points i and j, between which and only one-way road, the direction of either i to j, j to either i. In other words, this is a tournament of n points. Living in several cities Byteasar No. 1 city, he hoped that from No. 1 city, not repeatedly visit a number of cities along the one-way road, making access as much as possible.
Please write a program that helps Byteasar calculate how many road construction a way that the longest simple path from point 1 through point of departure exactly k, because the answer may be large, please modulo output of P
Input
The first line contains two positive integers n, P, represents the number of points and modulus. 2≤P≤1e9, N <= 2000
Output
N output lines, the longest simple path i-th row outputted from a starting point through exactly the number of i-mode competition FIG P.
Sample Input
2 233
Sample Output
1
1
Source
analysis
Tournaments have a good nature: there must be a Hamiltonian path.
Therefore, our starting point \ (1 \) constant on this path, and this path we can link into two blocks: \ (A \) and \ (B \) ( \ (1 \ in B \) ). Of course, there \ (A \) absence.
We can establish \ (f [i] [j ] \) represents \ (I \) points, the length of the longest path \ (J \) number of tournament.
First, we can know \ (F [I] [. 1] = 2 ^ {\ FRAC {(2-n-) (. 1-n-)} {2}} \) , because we only need to actually all \ ( 1 \) side of the dot point \ (1 \) dots can be.
Then for the other \ (j <i \) case, \ (F [I] [J] = F [J] [J] \ Times \ Binom {I-. 1} {J-. 1} \ Times 2 ^ {\ {FRAC (ij of) (. 1-ij of)} {2}} \) , \ (F [J] [J] \) considered \ (J \) connection point side, \ (\ Binom {I- 1} {j-1} \ ) considering the type of the selected point, corresponding to Imperial \ (1 \) dot from the remaining \ (i-1 \) point which is selected from \ (j-1 \) point composition \ (B \) , the remaining composition \ (a \) of a set of points connected to the interior side of chaos.
When \ (i = j \) when this formula does not work, but we know, contain \ (I \) race is the total number of points in FIG \ (2 ^ {\ frac { i (i-1) } {2}} \) a, so that \ (f [i] [i ] = 2 ^ {\ frac {i (i-1)} {2}} - \ sum_ {j = 1} ^ {i-1 F} [I] [J] \) .
Can be (O (N ^ 2) \ ) \ pretreatment number of combinations, \ (O (N ^ 2) the DP \) .
Reference: LPA20020220 .
code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2050;
char buf[1<<15],*fs,*ft;
inline char getc() { return (ft==fs&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),ft==fs))?0:*fs++; }
template<typename T>inline void read(T &x)
{
x=0;
T f=1, ch=getchar();
while (!isdigit(ch) && ch^'-') ch=getchar();
if (ch=='-') f=-1, ch=getchar();
while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48), ch=getchar();
x*=f;
}
template<typename T>inline void write(T x)
{
if (!x) { putchar('0'); return ; }
if (x<0) putchar('-'), x=-x;
T num=0, ch[20];
while (x) ch[++num]=x%10+48, x/=10;
while (num) putchar(ch[num--]);
}
ll power[maxn*maxn],C[maxn][maxn],f[maxn][maxn],mod;
int main()
{
int n;read(n);read(mod);
int e=n*(n-1)>>1;power[0]=1;
for (int i=1; i<=e; ++i) power[i]=(power[i-1]<<1)%mod;
for (int i=0; i<=n; ++i) C[i][0]=1;
for (int i=1; i<=n; ++i)
for (int j=1; j<=i; ++j) C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;
for (int i=1; i<=n; ++i)
{
f[i][1]=power[(i-1)*(i-2)>>1];
f[i][i]=power[i*(i-1)>>1];
for (int j=2; j<i; ++j) f[i][j]=f[j][j]*C[i-1][j-1]%mod*power[(i-j-1)*(i-j)>>1]%mod;
for (int j=1; j<i; ++j) (f[i][i]-=f[i][j])%=mod;
(f[i][i]+=mod)%=mod;
}
for (int i=1; i<=n; ++i) write(f[n][i]),puts("");
return 0;
}