题意

将正整数N拆分成若干个正整数之和,问有多少种不重复的拆分方案。

\(n \leq 5000\)

分析

用f(i,j)表示将i拆成若干个数字,最大的那个数字(即最后一个数)不超过j的方案数。
转移有两种情况,第一种是最大的数字不取j,第二种是取j
\[f(i,j)=f(i,j-1)+f(i-j,j)\]
时间复杂度\(O(N^2)\)

然后就可以做了吗?稍微分析一下发现需要高精度,那么空间复杂度是\(O(W \cdot N^2)\)的,W=100。
显然不行。
考虑压位高精,每个整数存8位,W=10,算了一下空间是953MB

考虑更换枚举顺序,外层先枚举j,发现满足正确性。
所以空间就可以降一维。

代码

#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<ctime>
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<complex>
#pragma GCC optimize ("O0")
using namespace std;
template<class T> inline T read(T&x)
{
    T data=0;
    int w=1;
    char ch=getchar();
    while(!isdigit(ch))
    {
        if(ch=='-')
            w=-1;
        ch=getchar();
    }
    while(isdigit(ch))
        data=10*data+ch-'0',ch=getchar();
    return x=data*w;
}
typedef long long ll;
const int INF=0x7fffffff;

const int MAXN=5e3+7;
const int pow10[10]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};

struct Big
{
    int a[10],len; // edit 1

    void set(int x)
    {
        memset(a,0,sizeof(a));
        len=1;
        a[0]=x;
    }

    Big(int x=0)
    {
        set(x);
    }

    int&operator[](int x)
    {
        return a[x];
    }

    friend Big operator+(Big&x,Big&y)
    {
        Big z;
        z.len=max(x.len,y.len);
        for(int i=0;i<z.len;++i)
        {
            z[i]+=x[i]+y[i];
            if(z[i]>=1e9)
            {
                z[i+1]+=z[i]/int(1e9);
                z[i]%=int(1e9);
            }
        }
        if(z[z.len])
            ++z.len;
        return z;
    }

    void out()
    {
        for(int i=len-1;i>=0;--i)
        {
            if(i!=len-1)
                for(int j=8;j>=1;--j) // edit 2
                    if(a[i]<=pow10[j]-1)
                        printf("0");
            printf("%d",a[i]);
        }
    }
};

Big f[MAXN]; // edit 4

int main()
{
//  freopen("UVA10590.in","r",stdin);
//  freopen("UVA10590.out","w",stdout);
    f[0].set(1);
    for(int i=1;i<=5e3;++i) // edit 3
        for(int j=i;j<=5e3;++j)
        {
            f[j]=f[j]+f[j-i];
        }
    int n;
    while(~scanf("%d",&n))
    {
        f[n].out();
        puts("");
    }
//  fclose(stdin);
//  fclose(stdout);
    return 0;
}

Hint

注意压位高精输出的时候要补全前导0.

UVA10590 Boxes of Chocolates Again的更多相关文章

  1. Uva 10590 Boxes of Chocolates Again

    题面戳这里 dp的姿势有两种(都保证了拆分的有序): \(f_{i,j}\)表示拆分中最大数为\(j\),和为\(i\)的方案数.转移\[f_{i,j} = \sum_{k = 1}^j f_{i-j ...

  2. ACM - 动态规划专题 题目整理

    CodeForces 429B  Working out 预处理出从四个顶点到某个位置的最大权值,再枚举相遇点,相遇的时候只有两种情况,取最优解即可. #include<iostream> ...

  3. Fedora 24 Gnome Boxes 无法ping通网络

    安装Fedora 24在试用虚拟机时发现无法ping通外网. 我傻傻地以为是软件问题. 问题描述: 尝试ping程序来测试网络连通性: (我之前也是ping百度,后来在为了少打字百度了一些比较短的域名 ...

  4. Problem B Boxes in a Line

     省赛B题....手写链表..其实很简单的.... 比赛时太急了,各种手残....没搞出来....要不然就有金了...注:对相邻的元素需要特判..... Problem B Boxes in a Li ...

  5. Codeforces Round #229 (Div. 2) C. Inna and Candy Boxes 树状数组s

    C. Inna and Candy Boxes   Inna loves sweets very much. She has n closed present boxes lines up in a ...

  6. boxes

    boxes [英][bɒksɪz][美][bɑ:ksɪz] n.盒( box的名词复数 ); 一盒; 电视; 小亭; v.把…装入盒[箱,匣]中( box的第三人称单数 ); 拳击;   以上结果来自 ...

  7. Brute Force - B. Candy Boxes ( Codeforces Round #278 (Div. 2)

    B. Candy Boxes Problem's Link:   http://codeforces.com/contest/488/problem/B Mean: T题目意思很简单,不解释. ana ...

  8. UVa 103 - Stacking Boxes(dp求解)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  9. [CareerCup] 9.10 Stack Boxes 垒箱子问题

    9.10 You have a stack of n boxes, with widths w., heights hir and depths drThe boxes cannot be rotat ...

随机推荐

  1. 10X Genomics vs. PacBio

    10X Genomics已经广泛应用于单细胞测序.组装领域,现在也是火的不行. 10X Genomics原理 通过将来自相同DNA片段(10-100kb)的reads加上相同的barcode,然后在i ...

  2. pip 源 替换国内源

    网上收集来的pip源地址: 阿里云 http://mirrors.aliyun.com/pypi/simple/中国科技大学 https://pypi.mirrors.ustc.edu.cn/simp ...

  3. android--------Android Studio常见问题以及解决方式

    gradle build的时候出现的问题: Error:Execution failed for task ':app:packageDebug'. Duplicate files copied in ...

  4. python运维之使用python进行批量管理主机

    1. python运维之paramiko 2. FABRIC 一个与多台服务器远程交互的PYTHON库和工具 3. SSH连接与自动化部署工具paramiko与Fabric 4. Python批量管理 ...

  5. OC ARC之循环引用问题(代码分析)

    // // main.m // 03-arc-循环引用 // // Created by apple on 13-8-11. // Copyright (c) 2013年 itcast. All ri ...

  6. 063——VUE中vue-router之重定向redirct的使用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. learning uboot how to set ddr parameter in qca4531 cpu

    DDR工作频率  在600MHZ. include/configs/board953x.h #define CFG_PLL_FREQ            CFG_PLL_650_600_200 #d ...

  8. .net面试题精选

    1.面向对象的三个特征?面向对象的五个基本设计原则? 2.接口与抽象类的区别? 3.谈谈设计模式? 4.线程与进程的区别?开发多线程程序应该考虑哪些问题? 5.简述 .NET中的Task? 6.简述. ...

  9. 3.strcpy使用注意(3)

    void test3(char * str1) { if(str1==NULL) { return; } char string[10]; if(strlen(str1)<=10) { strc ...

  10. avalon 路由问题

    1, 直接使用avalon的 amd加载器, 可以不需要 require.js 2, 配置baseUrl 路径, 这个一定要在 js所在的目录,  而不是jsp所在的目录,  如果js 和jsp分开 ...