forked from derekhh/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
roy-and-alpha-beta-trees.cpp
72 lines (64 loc) · 1.37 KB
/
roy-and-alpha-beta-trees.cpp
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
//roy-and-alpha-beta-trees.cpp
//Roy and alpha-beta trees
//Weekly Challenges - Week 4
//Author: derekhh
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
long long c[151], a[151];
int alpha, beta;
const int MOD = 1000000009;
void init()
{
c[0] = c[1] = 1;
for (int i = 2; i <= 150; i++)
for (int j = 0; j < i; j++)
c[i] = ((c[j] * c[i - j - 1] % MOD) + c[i]) % MOD;
}
long long f[151][151][2];
long long dp(int i, int j, int flag)
{
if (f[i][j][flag] != -1)
return f[i][j][flag];
if (i == j)
{
if (flag == 0) return f[i][j][flag] = a[i] * alpha % MOD;
else return f[i][j][flag] = (MOD - (a[i] * beta % MOD) % MOD);
}
else
{
long long sum = 0;
for (int idx = i; idx <= j; idx++)
{
if (idx > i) sum = (sum + c[j - idx] * dp(i, idx - 1, 1 - flag)) % MOD;
if (idx < j) sum = (sum + c[idx - i] * dp(idx + 1, j, 1 - flag)) % MOD;
long long val = c[idx - i];
val = (val * c[j - idx]) % MOD;
val = (val * a[idx]) % MOD;
if (flag == 0)
val = (val * alpha) % MOD;
else
val = (MOD - ((val * beta) % MOD)) % MOD;
sum = (sum + val) % MOD;
}
return f[i][j][flag] = sum % MOD;
}
}
int main()
{
init();
int t;
cin >> t;
while (t--)
{
memset(f, -1, sizeof(f));
int n;
cin >> n >> alpha >> beta;
for (int i = 0; i < n; i++)
cin >> a[i];
sort(a, a + n);
cout << dp(0, n - 1, 0) << endl;
}
return 0;
}