题意

将正整数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. 实训10a--用数据值填充下拉列表

    1.新建mvc4项目,选择基本模板. (1)点击“开始->所有程序->Microsoft Visual Studio 2012->Visual Studio 2012”菜单,打开Vi ...

  2. Confluence 6 为站点禁用匿名用户访问

    希望为你的站点禁用匿名用户的访问,取消选择 可以使用(can use)前面的选择框,然后选择 保存所有(Save All).这时候,用户应该禁止访问你的站点直达这些用户登录你的 Confluence ...

  3. 卸载 PrestaShop 1.7

    PrestaShop 的卸载非常简单: 在你的 Web 服务器上删除所有 PrestaShop 的文件和目录.你可以使用 FTP 客户端,你也可以使用 SSH 工具. 使用数据库工具删除数据库中所有以 ...

  4. 『科学计算』科学绘图库matplotlib练习

    思想:万物皆对象 作业 第一题: import numpy as np import matplotlib.pyplot as plt x = [1, 2, 3, 1] y = [1, 3, 0, 1 ...

  5. Homebrew cask

    在MacOS中用Homebrew安装软件很方便, 但用 brew install 安装的软件并不会在Launchpad中创建快捷方式, 这很不方便. 所以, 可以用 brew cask install ...

  6. dp练习(1)——马走日字

    3328: 马走日字 时间限制: 1 Sec  内存限制: 128 MB提交: 35  解决: 5[提交][状态][讨论版] 题目描述 一次外出旅游,你路上遇到了一个骑着马的强盗,你很害怕,你需要找一 ...

  7. 教你一步一步用 Node.js 制作慕课网视频爬虫

    转自:http://www.jianshu.com/p/d7631fc695af 开始 这个教程十分适合初学 Node.js 的初学者看(因为我也是一只初学的菜鸟~) 在这里,我就默认大家都已经在自己 ...

  8. 【LeetCode】Unique Binary Search Trees II 异构二叉查找树II

    本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4048209.html 原题: Given n, generate all struc ...

  9. Asterisk——part 1

    Asterisk Russell Bryant Asterisk1 is an open source telephony applications platform distributed unde ...

  10. 简话Angular 02 Angular控制器-作用域嵌套

    一句话: 就是孩子可以啃老,老子不能动孩子一根毛! * 子控制器有父控制器里变量的所有权限,可以读取,也可以修改. * 父控制器不能读,也不能修改孩子的变量 1. html代码 <div ng- ...