SCUT - 299 - Kaildls的数组划分 - dp - 高精

https://scut.online/p/299
\(dp[i][k]\) 为前 \(i\) 个数分 \(k\) 组的最大值,那么 $dp[i][k]=max_{p=1}^{i-1}{dp[p][k-1]*sum(p+1,i)} $

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

struct BigInt {
    const static int mod = 10000;
    const static int DLEN = 4;
    vector<int> a;
    int len;

    BigInt() {
        a.resize(1);
        len = 1;
    }

    BigInt(int v) {
        a.resize(2);
        len = 0;
        do {
            a[len++] = v%mod;
            v /= mod;
        } while(v);
    }

    BigInt operator +(const BigInt &b)const {
        BigInt res;
        res.len = max(len,b.len);
        res.a.resize(res.len+1);
        for(int i = 0; i <= res.len; i++)
            res.a[i] = 0;
        for(int i = 0; i < res.len; i++) {
            res.a[i] += ((i < len)?a[i]:0)+((i < b.len)?b.a[i]:0);
            res.a[i+1] += res.a[i]/mod;
            res.a[i] %= mod;

        }
        if(res.a[res.len] > 0)
            res.len++;
        return res;
    }

    BigInt operator *(const BigInt &b)const {
        BigInt res;
        res.a.resize(len + b.len);
        for(int i = 0; i < len; i++) {
            int up = 0;
            for(int j = 0; j < b.len; j++) {
                int temp = a[i]*b.a[j] + res.a[i+j] + up;
                res.a[i+j] = temp%mod;
                up = temp/mod;

            }
            if(up != 0)
                res.a[i + b.len] = up;

        }
        res.len = len + b.len;
        while(res.a[res.len - 1] == 0 &&res.len > 1)
            res.len--;
        res.a.resize(res.len);
        return res;
    }

    bool operator >(const BigInt &b)const {
        if(len>b.len)
            return true;
        else if(len==b.len) {
            int ln=len-1;
            while(a[ln]==b.a[ln]&&ln>=0)
                ln--;
            if(ln>=0&&a[ln]>b.a[ln])
                return true;
            else
                return false;
        } else
            return false;
    }

    void output() {
        printf("%d",a[len-1]);
        for(int i = len-2; i >=0 ; i--)
            printf("%04d",a[i]);
        printf("\n");
    }
};

int a[205];
BigInt dp[205][205];
//区间[i,j]分为k段取得的最大值

inline int suma(int i,int j) {
    return a[j]-a[i-1];
}

int main() {
#ifdef Yinku
    freopen("Yinku.in","r",stdin);
#endif // Yinku
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=1; i<=n; i++) {
        scanf("%d",&a[i]);
    }
    for(int i=1; i<=n; i++) {
        a[i]+=a[i-1];
    }

    for(int i=1; i<=n; i++)
        dp[i][1]=BigInt(suma(1,i));

    for(int i=2; i<=n; i++) {
        int c=min(i,k);
        for(int ki=2; ki<=c; ki++) {
            for(int p=1; p<=i-1; p++) {
                BigInt t=dp[p][ki-1]*BigInt(suma(p+1,i));
                if(t>dp[i][ki])
                    dp[i][ki]=t;
            }
        }
    }

    dp[n][k].output();
}

带FFT优化,反而更慢。这个可以理解。本身乘法是\(O(nm)\),这里m小到不行。你还搞个\(O(nlogn)\)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int MAXN=1e6+10;
inline int read() {
    char c=getchar();
    int x=0,f=1;
    while(c<'0'||c>'9') {
        if(c=='-')
            f=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9') {
        x=x*10+c-'0';
        c=getchar();
    }
    return x*f;
}

const double Pi=acos(-1.0);
struct Complex {
    double x,y;
    Complex(double xx=0,double yy=0) {
        x=xx,y=yy;
    }
    Complex operator+(const Complex& b)const {
        return Complex(x+b.x,y+b.y);
    }
    Complex operator-(const Complex& b)const {
        return Complex(x-b.x,y-b.y);
    }
    Complex operator*(const Complex& b)const {
        return Complex(x*b.x-y*b.y,x*b.y+y*b.x);
    }
} a[MAXN],b[MAXN];

int l,r[MAXN];
int limit;

void FFT(Complex *A,int type) {
    for(int i=0; i<limit; i++)
        if(i<r[i])
            swap(A[i],A[r[i]]);
    for(int mid=1; mid<limit; mid<<=1) {
        Complex Wn(cos(Pi/mid), type*sin(Pi/mid));
        for(int R=mid<<1,j=0; j<limit; j+=R) {
            Complex w(1,0);
            for(int k=0; k<mid; k++,w=w*Wn) {
                Complex x=A[j+k],y=w*A[j+mid+k];
                A[j+k]=x+y;
                A[j+mid+k]=x-y;
            }
        }
    }
}

void Mult(const int &n,const int &m,const vector<int> &A,const vector<int> &B) {
    for(limit=1,l=0; limit<=n+m;limit<<=1)
        l++;
    memset(a,0,sizeof(a[0])*limit);
    memset(b,0,sizeof(b[0])*limit);
    for(int i=0; i<=n; i++)
        a[i].x=A[i];
    for(int i=0; i<=m; i++)
        b[i].x=B[i];
    for(int i=0; i<limit; i++)
        r[i]=(r[i>>1]>>1)|((i&1)<<(l-1));
    FFT(a,1);
    FFT(b,1);
    for(int i=0; i<=limit; i++)
        a[i]=a[i]*b[i];
    FFT(a,-1);
    for(int i=0; i<=n+m+2; i++) {
        a[i].x=(ll)(a[i].x/limit+0.5);
    }
    return;
}

struct BigInt {
    const static int mod = 10000;
    const static int DLEN = 4;
    vector<int> a;
    int len;

    BigInt() {
        a.resize(4);
        len = 1;
    }

    BigInt(int v) {
        a.resize(4);
        len=0;
        do {
            a[len++]=v%mod;
            v/=mod;
        } while(v);
    }

    BigInt operator +(const BigInt &b)const {
        BigInt res;
        res.len=max(len,b.len);
        res.a.resize(res.len+4);
        for(int i=0; i<res.len; i++) {
            res.a[i]+=((i<len)?a[i]:0)+((i<b.len)?b.a[i]:0);
            res.a[i+1]+=res.a[i]/mod;
            res.a[i]%=mod;
        }
        if(res.a[res.len]>0)
            res.len++;
        return res;
    }

    BigInt operator *(const BigInt &b)const {
        BigInt res;
        res.a.resize(len+b.len+4);
        for(int i=0; i<len; i++) {
            int up=0;
            for(int j=0; j<b.len; j++) {
                int temp=a[i]*b.a[j]+res.a[i+j]+up;
                res.a[i+j]=temp%mod;
                up=temp/mod;
            }
            if(up != 0)
                res.a[i+b.len]=up;
        }
        res.len=len+b.len;
        while(res.len>1&&res.a[res.len-1]==0)
            res.len--;
        return res;
    }

    bool operator >(const BigInt &b)const {
        if(len>b.len)
            return true;
        else if(len==b.len) {
            int ln=len-1;
            while(a[ln]==b.a[ln]&&ln>=0)
                ln--;
            if(ln>=0&&a[ln]>b.a[ln])
                return true;
            else
                return false;
        } else
            return false;
    }

    void output() {
        printf("%d",a[len-1]);
        for(int i=len-2; i>=0; i--)
            printf("%04d",a[i]);
        printf("\n");
    }
};

BigInt BigInt_Mult_FFT(const BigInt &A,const BigInt &B) {
    Mult(A.len-1,B.len-1,A.a,B.a);
    BigInt res;
    res.len=A.len+B.len-1;
    res.a.resize(res.len+4);
    for(int i=0; i<res.len; i++)
        res.a[i]=a[i].x;
    while(res.len>1&&res.a[res.len-1]==0)
        res.len--;
    for(int i=0; i<res.len; i++) {
        res.a[i+1]+=res.a[i]/BigInt::mod;
        res.a[i]%=BigInt::mod;
    }
    if(res.a[res.len]>0)
        res.len++;
    return res;
}

int arr[205];
BigInt dp[205][205];
//区间[i,j]分为k段取得的最大值

inline int sumarr(int i,int j) {
    return arr[j]-arr[i-1];
}

int main() {
#ifdef Yinku
    freopen("Yinku.in","r",stdin);
#endif // Yinku
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=1; i<=n; i++) {
        scanf("%d",&arr[i]);
    }
    for(int i=1; i<=n; i++) {
        arr[i]+=arr[i-1];
    }

    for(int i=1; i<=n; i++) {
        dp[i][1]=BigInt(sumarr(1,i));
    }

    for(int i=2; i<=n; i++) {
        int c=min(i,k);
        for(int ki=2; ki<=c; ki++) {
            for(int p=1; p<=i-1; p++) {
                //BigInt t=dp[p][ki-1]*BigInt(sumarr(p+1,i));
                BigInt t=BigInt_Mult_FFT(dp[p][ki-1],BigInt(sumarr(p+1,i)));
                if(t>dp[i][ki]) {
                    dp[i][ki]=t;
                }
            }
        }
    }

    dp[n][k].output();
}
posted @ 2019-06-15 01:46  韵意  阅读(164)  评论(0编辑  收藏  举报