码迷,mamicode.com
首页 > 其他好文 > 详细

POJ3181

时间:2014-08-09 15:58:28      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:des   os   io   strong   for   ar   div   line   

Dollar Dayz
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3923   Accepted: 1530

Description

Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of tools on sale. During his first visit, the tools are selling variously for $1, $2, and $3. Farmer John has exactly $5 to spend. He can buy 5 tools at $1 each or 1 tool at $3 and an additional 1 tool at $2. Of course, there are other combinations for a total of 5 different ways FJ can spend all his money on tools. Here they are:

        1 @ US$3 + 1 @ US$2

        1 @ US$3 + 2 @ US$1

        1 @ US$2 + 3 @ US$1

        2 @ US$2 + 1 @ US$1

        5 @ US$1
Write a program than will compute the number of ways FJ can spend N dollars (1 <= N <= 1000) at The Cow Store for tools on sale with a cost of $1..$K (1 <= K <= 100).

Input

A single line with two space-separated integers: N and K.

Output

A single line with a single integer that is the number of unique ways FJ can spend his money.

Sample Input

5 3

Sample Output

5

Source

 

就是整数划分的模板题,只是输出的结果比较打,高精度用数组储存。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const __int64 MD=10000000000;
struct node
{
    __int64 g[5];
    node friend operator +(node a,node b)
    {
        node c;
        int i;
        for(i=0;i<5;i++)
        {
            c.g[i]=a.g[i]+b.g[i];
        }
        for(i=0;i<5;i++)
        {
            if(c.g[i]>=MD)
            {
                c.g[i+1]+=c.g[i]/MD;
                c.g[i]%=MD;
            }
        }
        return c;
    }
    void friend shuchu(node a)
    {
        int i;
        for(i=4;i>=0&&a.g[i]==0;i--);
        printf("%I64d",a.g[i]);
        for(i--;i>=0;i--)
        {
            printf("%010I64d",a.g[i]);
        }
        printf("\n");
    }
};
node dp[1001][101];
int main()
{

    int n,k,i,j,x;
    for(i=0;i<1001;i++)
        for(j=0;j<101;j++)
            for(x=0;x<5;x++)
                dp[i][j].g[x]=0;
    for(i=1;i<1001;i++)
        dp[i][1].g[0]=1;
    for(i=1;i<101;i++)
        dp[1][i].g[0]=1;
        dp[0][0].g[0]=1;
    for(i=2;i<1001;i++)
    {
        for(j=2;j<101;j++)
        {
            if(i==j)
                dp[i][j]=dp[i][i-1]+dp[0][0];
            else if(i<j)
                dp[i][j]=dp[i][i];
            else
                dp[i][j]=dp[i][j-1]+dp[i-j][j];
        }
    }
     //cout<<dp[5][3].g[0]<<endl;
    while(scanf("%d%d",&n,&k)!=EOF)
    {
       shuchu(dp[n][k]);
    }
    return 0;
}


 

POJ3181,布布扣,bubuko.com

POJ3181

标签:des   os   io   strong   for   ar   div   line   

原文地址:http://blog.csdn.net/fljssj/article/details/38456523

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!