算法札记:ACM中将高精度算法融于题目的模板

题号:AcWIng1069 凸多边形的划分(不重要)

代码:

优点:高精度处理类似工程学的"模板",适应各种环境

cpp 复制代码
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
using LL=long long;
const int N=55,M=35,INF=1e9+5;
int n;
int w[N];
LL f[N][N][M];
void add(LL a[],LL b[]){
    static LL c[M];
    memset(c,0,sizeof c);
    for (int i=0,t=0;i<M;i++){
        t+=a[i]+b[i];
        c[i]=t%10;
        t/=10;
    }
    memcpy(a,c,sizeof(c));
}
void mul(LL a[],LL b){
    static LL c[M];
    memset(c,0,sizeof(c));
    LL t=0;
    for (int i=0;i<M;i++){
        t+=a[i]*b;
        c[i]=t%10;
        t/=10;
    }
    memcpy(a,c,sizeof(c));
}
int cmp(LL a[],LL b[]){
    for (int i=M-1;i>=0;i--){
        if (a[i]>b[i]){
            return 1;
        }else if (a[i]<b[i]){
            return -1;
        }
    }
    return 0;
}
void print(LL a[]){
    int k=M-1;
    while (k && !a[k]){
        k--;
    }
    while (k>=0){
        cout<<a[k--];
    }
    cout<<endl;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin>>n;
    for (int i=1;i<=n;i++){
        cin>>w[i];
    }
    LL temp[M];
    for (int len=3;len<=n;len++){
        for (int l=1;l+len-1<=n;l++){
            int r=l+len-1;
            f[l][r][M-1]=1;
            for (int k=l+1;k<r;k++){
                memset(temp,0,sizeof(temp));
                temp[0]=w[l];
                mul(temp,w[k]);
                mul(temp,w[r]);
                add(temp,f[l][k]);
                add(temp,f[k][r]);
                if (cmp(f[l][r],temp)>0){
                    memcpy(f[l][r],temp,sizeof(temp));
                }
            }
        }
    }
    print(f[1][n]);
    return 0;
}
相关推荐
小七在进步18 分钟前
数据结构:快速排序
数据结构·算法·排序算法
闻缺陷则喜何志丹21 分钟前
【计算几何 第十一章】凸包:混合物
数学·算法·计算几何·凸包·混合物·凸组合
土司大王23 分钟前
LeetCode hot100——二叉树的最大深度
算法·leetcode·职场和发展
高亦真30 分钟前
今天是学习嵌入式的第31天
linux·学习·算法
让学习成为一种生活方式40 分钟前
黄花蒿LHC基因家族的串联重复驱动扩张及其UV-B胁迫适应性--BMC Plant Biology
人工智能·算法·机器学习
ECT-OS-JiuHuaShan1 小时前
共轭互逆链路论,彻底打击庸俗辩证法和不可知论
数据库·人工智能·算法·机器学习·数学建模
薛定e的猫咪1 小时前
【大模型量化】使用 llama.cpp 完成量化、本地推理与服务化部署
人工智能·深度学习·算法·llama
薛定e的猫咪1 小时前
【源码解读版】ContraBAR:用 CPC 对比学习替代变分推断做贝叶斯元 RL
人工智能·深度学习·学习·算法·机器学习
绿算技术2 小时前
大模型本地化的经济账:DeepSeek V4 Flash 部署实测
人工智能·科技·算法·spark