#include struct int128 { long long hig; long long low; }; int n,m; long long p=1e18; int128 ans,f[85][85][85],a[85][85]; int128 max(int128 a,int128 b) { if(a.hig>b.hig) return a; if(a.hig<b.hig) return b; if(a.low>b.low) return a; if(a.low<b.low) return b; return a; } int128 operator + (int128 a,int128 b) { int128 k; k.low=0,k.hig=0; k.low=a.low+b.low; k.hig=k.low/p+a.hig+b.hig; k.low%=p; return k; } int128 operator * (int128 a,int b) { int128 k; k.low=0,k.hig=0; k.low=a.lowb; k.hig+=k.low/p+ba.hig; k.low%=p; return k; } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%lld",&a[i][j].low); for(int i=1;i<=n;i++) for(int len=0;len<m;len++) for(int l=1;l+len<=m;l++) 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; for(int i=1;i<=n;i++) ans=ans+f[1][m][i]; if(ans.hig==0) printf("%lld",ans.low); else printf("%lld%018lld\n",ans.hig,ans.low); }//转换成Rust内容:use std::io::{self, BufRead};

#[derive(Debug, Clone, Copy)] struct Int128 { hig: i64, low: i64, }

fn max(a: Int128, b: Int128) -> Int128 { if a.hig > b.hig { a } else if a.hig < b.hig { b } else if a.low > b.low { a } else if a.low < b.low { b } else { a } }

fn add(a: Int128, b: Int128) -> Int128 { let mut k = Int128 { low: 0, hig: 0 }; k.low = a.low + b.low; k.hig = k.low / 1000000000000000000 + a.hig + b.hig; k.low %= 1000000000000000000; k }

fn multiply(a: Int128, b: i64) -> Int128 { let mut k = Int128 { low: 0, hig: 0 }; k.low = a.low * b; k.hig += k.low / 1000000000000000000 + b * a.hig; k.low %= 1000000000000000000; k }

fn main() { let stdin = io::stdin(); let mut lines = stdin.lock().lines(); let nm: Vec = lines .next() .unwrap() .unwrap() .split_whitespace() .map(|s| s.parse().unwrap()) .collect(); let n = nm[0]; let m = nm[1];

let mut a: Vec<Vec<Int128>> = vec![vec![Int128 { low: 0, hig: 0 }]; m + 1];
for _ in 1..=n {
    let row: Vec<i64> = lines
        .next()
        .unwrap()
        .unwrap()
        .split_whitespace()
        .map(|s| s.parse().unwrap())
        .collect();
    for j in 1..=m {
        a[j].push(Int128 {
            low: row[j - 1],
            hig: 0,
        });
    }
}

let mut f: Vec<Vec<Vec<Int128>>> = 
    vec![vec![vec![Int128 { low: 0, hig: 0 }; n + 1]; m + 1]; m + 1];

for i in 1..=n {
    for len in 0..m {
        for l in 1..=m - len {
            f[l][l + len][i] = max(
                add(f[l + 1][l + len][i], multiply(a[l][i], 2)),
                add(f[l][l + len - 1][i], multiply(a[l + len][i], 2)),
            );
        }
    }
}

let mut ans = Int128 { low: 0, hig: 0 };
for i in 1..=n {
    ans = add(ans, f[1][m][i]);
}

if ans.hig == 0 {
    println!("{}", ans.low);
} else {
    println!("{:018}{:018}", ans.hig, ans.low);
}
128位整数最大子矩阵和:C++ 实现与 Rust 转换

原文地址: https://www.cveoy.top/t/topic/qrNr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录