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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| #include "iostream" #include "algorithm" #include "cstring" #include "cstdio" #include "cmath" #include "vector" #include "map" #include "set" #include "queue" using namespace std; #define MAXN 1006 #define int long long #define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin() , (x).end() #define mem( a ) memset( a , 0 , sizeof a ) typedef long long ll; int P; int n , m; int A[MAXN] , inv[MAXN];
int dp[MAXN][MAXN] , t[2][MAXN]; int L[50][MAXN] , R[50][MAXN] , dis[50][MAXN] , us[MAXN]; int ans[MAXN][MAXN]; void solve( int dep , int S ) { int d = 0; rep( i , 1 , n ) if( !us[i] ) L[dep][i] = L[dep][i - 1] + 1; per( i , n , 1 ) if( !us[i] ) R[dep][i] = R[dep][i + 1] + 1; rep( i , 1 , n ) dis[dep][i] = min( L[dep][i] , R[dep][i] ) , d = max( d , dis[dep][i] ); if( d == 1 ) { int t = 0; rep( i , 1 , n ) if( !us[i] ) ++ t; rep( i , S + 1 , n ) rep( j , 1 , n ) if( !us[j] ) ans[i][j] = inv[t]; return; } int e = 0 , o = 0; rep( i , 1 , n ) if( d == dis[dep][i] ) { us[i] = 1; if( dis[dep][i + 1] == d ) ++ e , ++ i; else ++ o; } rep( i , 0 , n ) rep( j , 0 , n ) dp[i][j] = 0; dp[0][0] = 1; rep( i , 0 , e ) rep( j , 0 , o ) { if( i < e ) { dp[i + 1][j] = ( dp[i + 1][j] + dp[i][j] * 1ll * inv[o - j + ( e - i ) * 2] % P * ( e - i << 1 ) % P ) % P; t[0][S + i + j + 1] = ( t[0][S + i + j + 1] + dp[i][j] * 1ll * inv[o - j + ( e - i ) * 2] % P * ( e - i << 1 ) % P ) % P; } if( j < o ) { dp[i][j + 1] = ( dp[i][j + 1] + dp[i][j] * 1ll * inv[o - j + ( e - i ) * 2] % P * ( o - j ) % P ) % P; t[1][S + i + j + 1] = ( t[1][S + i + j + 1] + dp[i][j] * 1ll * inv[o - j + ( e - i ) * 2] % P * ( o - j ) % P ) % P; } } rep( i , S + 1 , S + e + o ) rep( j , 1 , n ) if( dis[dep][j] == d ) { if( dis[dep][j + 1] == d ) ans[i][j] = t[0][i] * 1ll * inv[e << 1] % P , ans[i][j + 1] = t[0][i] * 1ll * inv[e << 1] , ++ j; else ans[i][j] = t[1][i] * 1ll * inv[o] % P; } solve( dep + 1 , S + e + o ); int t = 0; rep( i , 1 , n - 1 ) if( dis[dep][i] == d && dis[dep][i + 1] == d ) { rep( j , S + 1 + e + o , n ) { rep( l , 0 , d - 1 ) { t = ( ans[j][i - l] + ans[j][i + l + 1] ) % P * 1ll * inv[2] % P; ans[j][i - l] = ans[j][i + l + 1] = t; } } } }
int Pow( int x , int a ) { int ret = 1; while( a ) { if( a & 1 ) ret = 1ll * ret * x % P; x = 1ll * x * x % P , a >>= 1; } return ret; }
void solve() {
cin >> n >> P; rep( i , 1 , n ) inv[i] = Pow( i , P - 2 ); solve( 1 , 0 ); rep( i , 1 , n ) { rep( j , 1 , n ) printf("%d ",ans[i][j]); puts(""); } }
signed main() {
solve(); }
|