P1005 矩阵取数游戏 (高精度)

https://www.luogu.org/problem/P1005

尽管这道题不算很难,但不过我还是差点没有想到。

首先这道题很容易看出来,每一排互不相干,每一排算自己的。

因为涉及到先后,而且只能取两个端点的。

因此很容易想到区间dp,有一个小的区间去推一个大的区间。

因为正着推,是错误了,自己yy吧。

而到着推,很容易想到地推方程的。dp[i][j]=max(dp[i+1][j]+a[i]*]2^{m-(j-i)},dp[i][j-1]+a[j]*2^{m-(j-i)})

这个方程也不是很难的。

这道题一看数据范围就知道会用到高精度。有板子还好说,我又没板子,有写不来,又不会python

所以就只有写java了。。。。。

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	static BigInteger[][] dp = new BigInteger[88][88];
	static BigInteger ans = BigInteger.ZERO;
	static long[] a = new long[88];
	static BigInteger[] p2 = new BigInteger[88];
	static int n, m;

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		n = cin.nextInt();
		m = cin.nextInt();
		p2[0] = BigInteger.ONE;
		for (int i = 1; i <= m; i++)
			p2[i] = p2[i - 1].multiply(BigInteger.valueOf(2));
		while (n-- > 0) {
			for (int i = 1; i <= m; i++)
				a[i] = cin.nextLong();
			for (int i = 1; i <= m; i++) {
				dp[i][i] = p2[m].multiply(BigInteger.valueOf(a[i]));
			}
			for (int l = 2; l <= m; l++) {
				for (int i = 1; i <= m - l + 1; i++) {
					int j = i + l - 1;
					BigInteger t1 = dp[i + 1][j].add(p2[m - l + 1].multiply(BigInteger.valueOf(a[i])));
					BigInteger t2 = dp[i][j - 1].add(p2[m - l + 1].multiply(BigInteger.valueOf(a[j])));
					dp[i][j] = t1.max(t2);
				}
			}
			ans = ans.add(dp[1][m]);
		}
		System.out.println(ans);
	}
}
发布了130 篇原创文章 · 获赞 80 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/KXL5180/article/details/99690011