P1005 [NOIP2007 提高组] 矩阵取数游戏C++代码

本文介绍了一种解决NOIP2007提高组矩阵取数游戏问题的C++代码实现,通过状态转移方程进行优化,确保不超过128位的计算,并提供了详细的代码注释。

让我来一波50行超长代码


首先推出状态转移方程

f[l][l+len][i]

表示左端点为l,右端点为l+len,第i行取数所能获得的最大值

很容易得出

f[l][l+len][i]=max(2*f[l+1][l+len][i]+2*a[i][l],2*f[l][l+len-1][i]+2*a[i][l+len]);

进一步推得

f[l][l+len][i]=max(f[l+1][l+len][i]+a[i][l],f[l][l+len-1][i]+a[i][l+len])*2

如此化简方便重载运算符时减少运算量


目测发现最大数为80x1000x(2^81-1)

显然不会超过128位,于是我选择手写int128

用a.hig储存a的高位,a.low储存a的低位。

高位显然不会溢出(因为只有2^81-1),所以将低位的1e18空出,防止两数相加时溢出

虽然这样一搞变成了int123 =_=

输出时要格外注意:

  • 当高位为零时直接输出低位;
  • 当高位不为零时直接输出高位,低位位数不足18位要补零
  • 当初就是因为低位没补零,找了八百个小时的bag

代码(有注释)如下:

#include<cstdio>
struct int128
{
    long long hig;
    long long low;
};//定义int128
int n,m;
long long p=1e18;//作mod用
int128 ans,f[85][85][85],a[85][85];
int128 max(int128 a,int128 b)
{
	if(a.hi
### 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、付费专栏及课程。

余额充值