算法札记: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;
}
相关推荐
HEJOO99 小时前
Linux 设备驱动系列——使用全局工作队列
java·linux·算法
Code_Solitude9 小时前
c语言:递归
java·c语言·算法
edtoplort10 小时前
OpenAI紧急叫停GPT-6.1 Astra训练,奥尔特曼为何踩刹车
人工智能·gpt·算法
蒜香可达鸭10 小时前
让视频生成流水线跑不崩:一个事件溯源的多 Agent 可靠执行框架
算法
小羊没烦恼!11 小时前
系统内部模块(子系统)之间的耦合以及模块(子系统)划分
java·开发语言·前端·数据库·算法·c#
Nebula_g11 小时前
JavaSE拓展:Arrays和Collections工具类
java·开发语言·算法·排序算法·工具类·javase·技术栈
菜鸟~noob23311 小时前
【电子战】 第22篇:误差椭圆与置信区间【含matlab代码】
开发语言·算法·matlab
weixin_3077791311 小时前
C++代码实现MATLAB中的normalize函数功能
开发语言·c++·算法·matlab
不会就选b11 小时前
算法日常・每日刷题--<动态规划>11
算法
(Charon)11 小时前
【C++面试】手写String类:同时实现拷贝构造与移动构造
c++·算法