P1005 矩阵取数游戏 DP+__int128

本文详细解析了洛谷P1005矩阵取数游戏问题,采用动态规划策略,通过逐行计算最大得分,利用快速幂优化计算过程,最终实现高效求解。

P1005 矩阵取数游戏

链接在此https://www.luogu.org/problem/P1005

先考虑部分分60分
可以看到dp是可以稳稳地得到60分的。
而dp思路就是将每一行单独求dp
dp方程式为

			dp[i][j]=max(dp[i-1][j]+a[i]*fpow(2,n-(j-i)+1),dp[i][j]);
			dp[i][j]=max(dp[i][j+1]+a[j]*fpow(2,n-(j-i)+1),dp[i][j]);

其中fpow为快速幂(这里有坑点)。表示2的(n-i+j+1)次方
然后i表示从开头取取到了第i个
j表示从结尾取取到了第j个

i和j都是数组的序号而不是从左往右或从右往左的个数。

剩下的就是高精度。手懒用了int128

要注意的是print时注意特判0的情况
还有fpow中的变量类型也要是int128

#include<bits/stdc++.h>
#define REP(i,a,b) for(int i(a); i<=(b);++i)
#define MAXN 100010
#define ll long long
#define bll __int128
using namespace std;
void read(int &x){
	x=0; char c=getchar(); int f=1;
	for(;!isdigit(c);c=getchar()) if (c=='-') f=-f;
	for(;isdigit(c);c=getchar()) x=x*10+c-'0'; x*=f;
}
bll fpow(bll a,bll b){
	bll ans=1;
	while (b>0){
		if (b%2)  ans*=a;
		a=a*a;	b=b/2;
	}
	return ans;
}
void print(bll x){
    if(!x) return;
    if(x) print(x/10);
    putchar(x%10+'0');
}
int n,m,a[MAXN];
bll dp[100][100];
int main(){
	bll ANS=0; 
	read(m); read(n);
	REP(QQ,1,m){
		memset(dp,0,sizeof(dp)); 
		memset(a,0,sizeof(a)); 
	for(int i(1);i<=n;++i) read(a[i]);
	for(int i(n+1);i>=1;i--) dp[0][i]=dp[0][i+1]+a[i]*fpow(2,n-i+1);
	REP(i,1,n)
		for(int j=n+1; j>i;j--){
			dp[i][j]=max(dp[i-1][j]+a[i]*fpow(2,n-(j-i)+1),dp[i][j]);
			dp[i][j]=max(dp[i][j+1]+a[j]*fpow(2,n-(j-i)+1),dp[i][j]);
		}
	bll ans=0; 
		REP(i,0,n){
			ans=max(ans,dp[i][i+1]);
		}
		ANS+=ans; 
	}
	if(ANS) 
	print(ANS); else cout<<"0"<<endl; 
}
先展示下效果 https://pan.quark.cn/s/5061241daffd 在使用Apache HttpClient库发起HTTP请求的过程中,有可能遇到`HttpClient`返回`response`为`null`的现象,这通常暗示着请求未能成功执行或部分资源未能得到妥善处理。 在本文中,我们将详细研究该问题的成因以及应对策略。 我们需要掌握`HttpClient`的运作机制。 `HttpClient`是一个功能强大的Java库,用于发送HTTP请求并接收响应。 它提供了丰富的API,能够处理多种HTTP方法(例如GET、POST等),支持重试机制、连接池管理以及自定义请求头等特性。 然而,一旦`response`对象为`null`,可能涉及以下几种情形:1. **连接故障**:网络连接未成功建立或在请求期间中断。 需要检查网络配置,确保服务器地址准确且可访问。 2. **超时配置**:若请求超时,`HttpClient`可能不会返回`response`。 应检查连接和读超时设置,并根据实际需求进行适当调整。 3. **服务器故障**:服务器可能返回了错误状态码(如500内部服务器错误),`HttpClient`无法解析该响应。 建议查看服务器日志以获更多详细信息。 4. **资源管理**:在某些情况下,如果请求的响应实体未被正确关闭,可能导致连接被提前释放,进而使后续的`response`对象为`null`。 在使用`HttpClient 3.x`版本时,必须手动调用`HttpMethod.releaseConnection()`来释放连接。 而在`HttpClient 4.x`及以上版本中,推荐采用`EntityUtils.consumeQuietly(respons...
### NOIP 2007 提高组 P1005 矩阵游戏 C++ 解法 #### 动态规划思路解析 此问题的核心在于通过动态规划解决如何最大化矩阵后的总得分。定义状态 `f[i][j]` 表示当前子序列从位置 `i` 到位置 `j` 的最大得分,则转移方程可以表示为: ```cpp f[i][j] = max(f[i+1][j] + a[i] * pow(2, t), f[i][j-1] + a[j] * pow(2, t)); ``` 其中,`t` 是当前操作次,`a[i]` 和 `a[j]` 分别代表选的头尾元素。 由于涉及大运算,需采用自定义的大整类或者使用内置支持更大范围的据类型(如 `__int128`)。以下是具体实现方法[^2]。 --- #### 完整代码实现 以下是一个基于动态规划并考虑大处理的完整解决方案: ```cpp #include <bits/stdc++.h> using namespace std; struct Int128 { long long high; long long low; }; Int128 operator+(const Int128& a, const Int128& b) { Int128 res; res.low = a.low + b.low; res.high = a.high + b.high + (res.low >= 1e18 ? 1 : 0); res.low %= 1e18; return res; } Int128 operator*(const Int128& a, long long b) { Int128 res; res.low = a.low * b; res.high = a.high * b + (res.low / 1e18); res.low %= 1e18; return res; } bool operator<(const Int128& a, const Int128& b) { if (a.high != b.high) return a.high < b.high; return a.low < b.low; } Int128 max(const Int128& a, const Int128& b) { return a < b ? b : a; } void print(Int128 num) { if (num.high > 0) cout << num.high; printf("%018lld", num.low); } long long power[105]; Int128 dp[105][105]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; // Precompute powers of 2 power[0] = 1; for (int i = 1; i <= m; ++i) power[i] = power[i - 1] * 2; Int128 totalScore = {0, 0}; while (n--) { vector<long long> row(m + 1, 0); for (int i = 1; i <= m; ++i) cin >> row[i]; // Initialize DP table fill(&dp[0][0], &dp[m][m] + 1, Int128{0, 0}); for (int len = 1; len <= m; ++len) { for (int l = 1; l + len - 1 <= m; ++l) { int r = l + len - 1; if (len == 1) { dp[l][r].low = row[l] * power[m - len + 1]; } else { dp[l][r] = max( dp[l + 1][r] + Int128{(row[l] * power[m - len + 1]) / 1e18, (row[l] * power[m - len + 1]) % 1e18}, dp[l][r - 1] + Int128{(row[r] * power[m - len + 1]) / 1e18, (row[r] * power[m - len + 1]) % 1e18} ); } } } totalScore = totalScore + dp[1][m]; } print(totalScore); return 0; } ``` --- #### 关键点说明 1. **据结构设计** 使用自定义结构体 `Int128` 来存储大值,分别记录高位和低位部分以便于计算超大据的结果[^5]。 2. **幂次预处理** 预先计算好所有的 $2^i$ 幂次值存入组中以减少重复计算开销[^3]。 3. **边界条件处理** 当长度等于1时直接赋值对应单个元素乘以其权重作为初始状态;其他情况则依据前一步的状态更新当前最优解[^4]。 4. **最终结果输出** 对于超过常规类型的答案采分段打印方式展示全部有效位[^5]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值