算法札记: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;
}
相关推荐
shirsl3 小时前
算法 Day 5 树 / 二叉树 + DFS
数据结构·python·算法
木子算法3 小时前
非凸、离散、还耦合:论文里的求解方法是一条四步流水线
人工智能·算法·目标跟踪
虚无的纽扣4 小时前
【力扣刷题】第二天:无重复字符的最长字串、移动零问题
算法·leetcode·排序算法
张祥6422889044 小时前
牛顿迭代法求解开普勒方程:从RTKLIB源码到数值分析
人工智能·算法·机器学习
Niuguangshuo4 小时前
论文解读:Deep Speech 2,工业级英中端到端 ASR 系统报告
算法·音视频·语音识别
钓鱼的肝4 小时前
csp-j-s总结(2)
c++·经验分享·笔记·算法·青少年编程
奇妙之二进制4 小时前
机器人导航路径规划算法入门(6)Dijkstra(迪杰斯特拉)算法深入解析
算法·导航
Niuguangshuo5 小时前
论文解读:Qwen2-Audio,阿里的通用音频语言模型
算法·音视频·语音识别
WiChP6 小时前
【V0.1B16】从零开始的2D游戏引擎开发之路
开发语言·算法·游戏引擎
番茄巴士7 小时前
手写一个 mini HashMap,彻底搞懂哈希表原理
算法