题意

将正整数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. python-GUI,生成ssn

    第一次做这个, 样子有点丑,主要是实现功能,做测试的时候,经常要用到身份证号.手机号.姓名等,这里先生成ssn,后续研究怎么做成客户端 代码: from tkinter import * from u ...

  2. 012 - jstat命令查看jvm的GC情况 | jvm

    jstat命令可以查看堆内存各部分的使用量,以及加载类的数量. 命令的格式如下: jstat -<option> [-t] [-h<lines>] <vmid> [ ...

  3. 20170405xlVBA快速录入

    Dim Rng As Range Dim Arr As Variant Dim LastCell As Range Dim FindText As String Dim ItemCount As Lo ...

  4. 『Numpy』高级函数_np.nditer()&ufunc运算

    1.np.nditer():numpy迭代器 默认情况下,nditer将视待迭代遍历的数组为只读对象(read-only),为了在遍历数组的同时,实现对数组元素值得修改,必须指定op_flags=[' ...

  5. hdu5253 MST

    连接的管道 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  6. 使用a标签实现软件下载及下载量统计

    通常最简单的软件下载就是采用如下方式: <a id="welcomeMiddleBtn" href="${basePath}/files/client/instal ...

  7. [LeetCode] 41. First Missing Positive ☆☆☆☆☆(第一个丢失的正数)

    Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...

  8. currentSession

    public  class  HibernateUtil  {  public static final ThreadLocal session =new ThreadLocal();  public ...

  9. Tarjan 算法求强联通分量

    转载自:http://blog.csdn.net/xinghongduo/article/details/6195337 还是没懂Tarjan算法的原理.但是感觉.讲的很有道理. 说到以Tarjan命 ...

  10. 使用shake.js让你博客支持摇一摇

    大家好,又到了随机文章的时间,请使用手机打开演示站点,然后像摇妹子一样摇晃手机,你会发现非常牛逼的事情,炫酷吧.该功能已经集成在Oconnor1.8中.本文主要讲解这货的原理. 首先需要下载shake ...