Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P1005 [NOIP2007 提高组] 矩阵取数游戏

P1005 [NOIP2007 提高组] 矩阵取数游戏

显然每一行是独立的,对于每一行进行记忆化搜素即可。

高精常数爆炸,__int128 快的飞起。

查看代码
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
#include <cstdio>
#include <algorithm>
using namespace std;
template <class Type>
void read(Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
c == '-' && (flag = true);
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 3) + (x << 1) + c - '0';
flag && (x = ~x + 1);
}
template <class Type>
void write(Type x)
{
x < 0 && (putchar('-'), x = ~x + 1);
x > 9 && (write(x / 10), 0);
putchar(x % 10 + '0');
}
const int N = 90;
int n, m;
__int128 w[N], f[N][N];
__int128 dfs(int x, int y)
{
if (x + y >= n)
return 0;
if (f[x][y])
return f[x][y];
return f[x][y] = max(dfs(x + 1, y) + w[x + 1] * ((__int128)1 << x + y + 1), dfs(x, y + 1) + w[n - y] * ((__int128)1 << x + y + 1));
}
int main()
{
scanf("%d%d", &m, &n);
__int128 res = 0;
while (m--)
{
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n - i; j++)
f[i][j] = 0;
for (int i = 1; i <= n; i++)
read(w[i]);
res += dfs(0, 0);
}
write(res);
return 0;
}