UVA-10590 正整数拆分

来源:互联网 发布:c# 数据库接口开发 编辑:程序博客网 时间:2024/06/14 08:35

Boxes of Chocolates Again

Little Pippy has got again a big bunch of boxes of chocolates on her 7-th birthday. Her parents are
anxious about the health of her teeth, so they have allowed her to take only a limited number of
chocolates; lets call this number N. They know that Pippy always shares her belongings with her
friends, so they have fixed a sufficiently large number to make sure that all are happy. The chocolates
are packed in several types of boxes. Each type of box contains a certain number of chocolates which
is written above the box. Boxes of different types contain different numbers of chocolates. If a box
contains k chocolate(s) we will call it type-k box. Now Pippy should take exactly N chocolates without
tearing apart any box. Your job is to determine in how many ways Pippy can do this. You may assume
that there are infinitely many boxes of each type from type-1 to type-N.
For example, lets assume that Pippy has been asked to take 3 chocolates. She can take only one
type-3 box or she can take one type-2 box and one type-1 box or she can take 3 type-1 boxes.
Input
There will be several lines as input each containing a candidate N as described above. N can be any
nonnegative number less than or equal to 5000. It may look like that N is very big for a little 7 year
old girl; but remember she has lots of friends. And, who knows, may be you are one of her friends!
Output
For each N, print an integer on a single line indicating the number of ways Pippy can take N chocolates.
Sample Input
3
4
5
Sample Output
3
5
7

题目大意:正整数拆分。
解题思路:动规
dp[i][j] 表示将i拆成若干个数字,最大的数字不超过j
dp[i][j]=dp[i][j1]+dp[ij][j]
看到j,j1可以优化空间
注意点:开始用高精度模板,T了,看题解说加法要优化,但是我不会改Orz。。。。
于是改用Java。。。以后要理解一下高精度模板的实质,这样才能优化!!!

AC代码:

import java.math.*;import java.util.*;public class Main {    static int MAXN=5050;    static BigInteger dp[]=new BigInteger[MAXN];    public static void init()    {        for(int i=0;i<MAXN;i++)            dp[i]=BigInteger.ZERO;        dp[0]=BigInteger.ONE;        for(int j=1;j<MAXN;j++)        {            for(int i=j;i<MAXN;i++)            {                dp[i]=dp[i].add(dp[i-j]);            }        }    }    public static void main(String[] args)    {        init();        int n;        Scanner in=new Scanner(System.in);        while(in.hasNext())        {            n=in.nextInt();            System.out.println(dp[n]);        }        in.close();    }}

T代码:

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;typedef long long LL;struct BigInt{    const static int mod=10000;    const static int DLEN=4;    int a[600],len;    BigInt()    {        memset(a,0,sizeof(a));        len=1;    }    BigInt(int v)    {        memset(a,0,sizeof(a));        len=0;        do        {            a[len++]=v%mod;            v/=mod;        }while(v);    }    BigInt(const char s[])    {        memset(a,0,sizeof(a));        int L=strlen(s);        len=L/DLEN;        if(L%DLEN) len++;        int index=0;        for(int i=L-1;i>=0;i-=DLEN)        {            int t=0;            int k=i-DLEN+1;            if(k<0) k=0;            for(int j=k;j<=i;j++)                t=t*10+s[j]-'0';            a[index++]=t;        }    }    BigInt operator +(const BigInt &b) const    {        BigInt res;        res.len=max(len,b.len);        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;        for(int i=0;i<len;i++)        {            int up=0;            for(int j=0;j<b.len;j++)            {                int tmp=a[i]*b.a[j]+res.a[i+j]+up;                res.a[i+j]=tmp%mod;                up=tmp/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--;        return res;    }    void output()    {        printf("%d",a[len-1]);        for(int i=len-2;i>=0;i--)            printf("%04d",a[i]);        printf("\n");    }};BigInt dp[5050];int main(){    int n;    dp[0].a[0]=1;    dp[0].len=1;    for(int j=1;j<=5010;j++)    {        for(int i=j;i<=5010;i++)        {            dp[i]=dp[i]+dp[i-j];        }    }    while(scanf("%d",&n)!=EOF)    {        dp[n].output();    }    return 0;}
原创粉丝点击