题意

将正整数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. Mysql错误: Lock wait timeout exceeded 解决办法

    一.临时解决办法: 执行mysql命令:show full processlist; 然后找出插入语句的系统id 执行mysql命令:kill id 或 首先,查看数据库的进程信息: show ful ...

  2. Android之ToolBar的使用

    Toolbar是在 Android 5.0 开始推出的一个 Material Design 风格的导航控件 ,Google 非常推荐大家使用 Toolbar 来作为Android客户端的导航栏,以此来 ...

  3. android--------volley之网络请求和图片加载

    Volley是 Google 推出的 Android 异步网络请求框架和图片加载框架. Volley的特性 封装了的异步的请求API.Volley 中大多是基于接口的设计,可配置性强. 一个优雅和稳健 ...

  4. hdu6398 计算几何

    不算严格的计算几何,就是各种分类 精度调好就能过,考虑三条边斜着放的所有情况即可 #include<bits/stdc++.h> #define LL long long #define ...

  5. python-day52--前端html、css

    一.html需掌握的: 1. img标签 属性:src alt title width height 2. a标签 属性:href target 3. ul 标签及li 标签,二者都是块级标签 ul ...

  6. bat批处理文件运行时隐藏cmd窗口

    想让bat运行时隐藏cmd窗口,最好的方法是使用vbs文件实现, 1.新建一个文本文档,改名为123.vbs,编辑内容: set ws=WScript.CreateObject("WScri ...

  7. bzoj1089

    题解: 递推 f[i]=f[i-1]^n+1 ans=f[d]-f[d-1] 代码: #include<bits/stdc++.h> using namespace std; int n, ...

  8. bzoj2209

    题解: splay打标机 往下传递 记录x,y为化简后,区间有多少(,) 代码: #include<bits/stdc++.h> ; using namespace std; ],sum[ ...

  9. Delphi IDE 版本

    产品 compiler Version version bds 2007-WIN32 VER 180 185 11 5.0 2009 VER 200 13 6.0 XE VER 220 15 8.0 ...

  10. 玩转X-CTR100 l STM32F4 l CAN通信

    我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ] X-CTR100控制器STM32F4处理器内置CAN ...