답안 #19495

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
19495 2016-02-24T14:14:32 Z kaTkaHr 악수 (kriii4_D) C++14
100 / 100
2942 ms 34544 KB
#include <stdio.h>
#include<vector>
#include <algorithm>
#include <map>

using namespace std;

typedef long long ll;

const int MX = 45, MM = 1000000007;

template<typename T>
T pw(T A, ll B){
  T R = 1;
  while(B){
    if( B&1 ) R = R * A;
    A = A * A; B /= 2;
  }
  return R;
}

ll rv(ll A){ 
  ll R = 1, B = MM-2;
  while(B){
    if( B&1 ) R = R * A % MM;
    A = A * A % MM; B /= 2;
  }
  return R;
}
/*
struct frac{
  ll A, B;
		frac(ll A):A(A), B(1){}
  frac(ll a, ll b){
    A = (a%MM+MM) % MM;
    B = (b%MM+MM) % MM;
  }
  frac(){A = 0, B = 1;}
  frac operator+ (const frac &l)const{
    return frac((A * l.B + B * l.A) % MM, B * l.B % MM);
  }
  frac operator*(const frac &l)const{
    return frac(A*l.A % MM, B*l.B % MM);
  }
  frac operator/(const frac &l)const{
    return frac(A*l.B % MM, B*l.A % MM);
  }
  frac operator- (const frac &l)const{
    return frac((A*l.B - B*l.A%MM + MM) % MM, B*l.B % MM);
  }
  ll v(){ return A * rv(B) % MM; }
};// */
//*
struct frac{
  ll A;
		frac(ll A):A((A%MM+MM)%MM){}
  frac(ll a, ll b){
			a = (a%MM+MM)%MM;
			b = (b%MM+MM)%MM;
			A = rv(b) * a % MM;
  }
  frac(){A = 0;}
  frac operator+ (const frac &l)const{
			return l.A + A >= MM? l.A + A - MM: l.A + A;
  }
  frac operator*(const frac &l)const{
			return l.A * A % MM;
  }
  frac operator/(const frac &l)const{
			return A * rv(l.A) % MM;
  }
  frac operator- (const frac &l)const{
			return A >= l.A? A-l.A: A-l.A + MM;
  }
  ll v(){ return A; }
};// */

frac D[MX][MX*MX], E[MX][MX*MX], C[MX*MX][MX*MX];

int main()
{
	int N, T;
	scanf("%d", &N); T = N*(N-1) / 2;

	C[0][0] = 1;
	for(int i = 1; i <= T || i <= N; i++){
		C[i][0] = 1;
		for(int j = 1; j <= i; j++){
			C[i][j] = C[i-1][j-1] + C[i-1][j];
		}
	}

	D[1][0] = E[1][0] = 1;
	for(int i = 2; i <= N; i++){
		for(int x = i-1; x <= i*(i-1)/2; x++){
			for(int k = 1; k <= i-1; k++){
				frac t = 0;
				for(int l = 0; l <= x-1; l++){
					t = t + D[k][l] * D[i-k][x-l-1] * C[x-1][l];
				}
				E[i][x] = E[i][x] + t * C[i-1][k-1] * frac(k*(i-k),1);
			}
			D[i][x] = D[i][x-1] * frac(i*(i-1)/2 - x + 1, 1) + E[i][x];
		}
	}
	frac ans = 0, mul = 1;
	frac tot = 0;
	for(int i = 0; i <= T; i++){
		tot = tot + E[N][i] * mul;
		ans = ans + E[N][i] * i * mul;
		mul = mul * frac(1, T-i);
	}
	printf("%lld\n", ans.v());
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 34544 KB Output is correct
2 Correct 0 ms 34544 KB Output is correct
3 Correct 8 ms 34544 KB Output is correct
4 Correct 4 ms 34544 KB Output is correct
5 Correct 0 ms 34544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 34544 KB Output is correct
2 Correct 0 ms 34544 KB Output is correct
3 Correct 4 ms 34544 KB Output is correct
4 Correct 0 ms 34544 KB Output is correct
5 Correct 4 ms 34544 KB Output is correct
6 Correct 5 ms 34544 KB Output is correct
7 Correct 0 ms 34544 KB Output is correct
8 Correct 3 ms 34544 KB Output is correct
9 Correct 6 ms 34544 KB Output is correct
10 Correct 10 ms 34544 KB Output is correct
11 Correct 16 ms 34544 KB Output is correct
12 Correct 18 ms 34544 KB Output is correct
13 Correct 25 ms 34544 KB Output is correct
14 Correct 42 ms 34544 KB Output is correct
15 Correct 46 ms 34544 KB Output is correct
16 Correct 65 ms 34544 KB Output is correct
17 Correct 89 ms 34544 KB Output is correct
18 Correct 106 ms 34544 KB Output is correct
19 Correct 139 ms 34544 KB Output is correct
20 Correct 176 ms 34544 KB Output is correct
21 Correct 227 ms 34544 KB Output is correct
22 Correct 278 ms 34544 KB Output is correct
23 Correct 349 ms 34544 KB Output is correct
24 Correct 427 ms 34544 KB Output is correct
25 Correct 523 ms 34544 KB Output is correct
26 Correct 637 ms 34544 KB Output is correct
27 Correct 778 ms 34544 KB Output is correct
28 Correct 927 ms 34544 KB Output is correct
29 Correct 1113 ms 34544 KB Output is correct
30 Correct 1323 ms 34544 KB Output is correct
31 Correct 1564 ms 34544 KB Output is correct
32 Correct 1845 ms 34544 KB Output is correct
33 Correct 2166 ms 34544 KB Output is correct
34 Correct 2531 ms 34544 KB Output is correct
35 Correct 2942 ms 34544 KB Output is correct