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
题目大意:正整数拆分。
解题思路:动规
看到
注意点:开始用高精度模板,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;}
- UVA-10590 正整数拆分
- 正整数拆分
- 正整数拆分问题
- 拆分为连续正整数之和
- uva 11384 正整数序列
- 实现将一个正整数随机拆分为三个正整数-- 【叶子】
- 实现将一个正整数随机拆分为三个正整数
- UVa 10976 分数拆分
- UVa 10976 分数拆分
- uva 10976 分数拆分
- 正整数n的k拆分问题
- HDOJ1028(正整数拆分-普通型母函数模板)
- 【基本算法】拆分为连续正整数之和
- UVA 10590(完全背包,整数拆分+大数加法)
- 把输入整数拆分为连续的正整数之和
- 拆分正整数组,使二者和最接近,且长度无限制
- 拆分正整数数组,使二者和最接近,且长度相等。
- poj 1032 Parliament(正整数拆分不同数使乘积最大)
- 进程保活之单进程守护
- 螺旋线
- java中变量 以及使用变量的原因
- 一个工具类的全局静态方法生成uuid
- 5.16特长生模拟题 遭遇战
- UVA-10590 正整数拆分
- 制作Centos usb 安装盘 for Mac
- GCD常用方法
- AsyncTask使用总结
- BaseDialog的封装
- 公开密钥加密技术(公钥和私钥)
- 轻量级的.NET对象查找服务和AOP开发框架Netop.Core源码解说(2)--配置
- vs编译器一些常见错误
- js中字符串常用截取函数