【Codeforces 949D】Shake It! 【动态规划】

 1 By Asm.Def, contest: Codeforces Round #431 (Div. 1), problem: (D) Shake It!, Accepted, #
2
3 #include <bits/stdc++.h>
4 using namespace std;
5 const int maxn = 52, mod = 1000000007;
6 typedef long long LL;
7 int N, M, F[maxn][maxn], G[maxn][maxn], inv[maxn];
8
9 void init()
10 {
11     scanf("%d%d", &N, &M);
12     inv[1] = 1;
13     for(int i = 2;i < maxn;++i)
14         inv[i] = LL(mod-mod/i) * inv[mod%i] % mod;
15 }
16 void work()
17 {
18     F[0][1] = 1;
19     for(int i = 1;i <= N;++i) for(int j = 1;j < maxn;++j)
20     {
21         for(int a = 0;a < i;++a)
22         {
23             G[i][j] = (G[i][j] + (LL) F[a][j] * F[i-1-a][j]) % mod;
24             for(int b = j+1;b <= i+1 && b < maxn;++b)
25             {
26                 G[i][j] = (G[i][j] + (LL) F[a][b] * F[i-1-a][j]) % mod;
27                 G[i][j] = (G[i][j] + (LL) F[a][j] * F[i-1-a][b]) % mod;
28             }
29         }
30         //get G[i][j]
31         for(int x = N-1;x >= 0;--x) for(int y = 1;y < maxn;++y) if(F[x][y])
32         {
33             int C = 1;
34             for(int t = 1;x+t*i <= N && y+t*j < maxn;++t)
35             {
36                 C = (LL) C * (G[i][j]-1+t) % mod * inv[t] % mod;
37                 F[x+t*i][y+t*j] = (F[x+t*i][y+t*j] + (LL) F[x][y] * C) % mod;
38             }
39         }
40     }
41     printf("%d\n", F[N][M]);
42 }
43 int main()
44 {
45     init();
46     work();
47     return 0;
48 }

posted @ 2017-09-24 01:28  Asm.Definer  阅读(630)  评论(0编辑  收藏  举报