POJ——3181Dollar Dayz

题目链接

题意:输入n~1000,和k~100,问将n用1到k这k个数字进行拆分,有多少种拆分方法。例如:n=5,k=3 则有n=3+2,n=3+1+1,n=2+1+1+1,n=2+2+1,n=1+1+1+1+1这5种拆分方法。

题解:这是一个完全背包,思考最后一个数字用什么填就能发现。但是发现这个最终结果大于longlong,可以试下边界数据,但是只是比longlong大一点,所以用2个longlong来表示。

重点:测试边界数据,使用两个longlong

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <ctype.h>
#include <limits.h>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
#define CLR(a) memset(a, 0, sizeof(a))
#define REP(i, a, b) for(int i = a;i < b;i++)
#define REP_D(i, a, b) for(int i = a;i <= b;i++)

typedef long long ll;

using namespace std;

const int maxn = 1e3 + 100;
const int MIN = -1e8;
const ll key = 10000000000000000ll;
int k, n;
ll dp[maxn][2];//0代表低16位的数,1代表高位

void getDp()
{
    CLR(dp);
    dp[0][0] = 1;
    REP_D(i, 1, k)
    {
        REP_D(j, i, n)
        {
            ll tmp = 0;//分成两个
            ll tmp_2 = 0;
            tmp = dp[j][0] + dp[j - i][0];
            tmp_2 += tmp/key;
            tmp %= key;
            dp[j][0] = tmp;
            tmp_2 += (dp[j][1] + dp[j - i][1]);
            dp[j][1] = tmp_2;
        }
    }
    if(dp[n][1] != 0)
        printf("%lld%016lld\n", dp[n][1], dp[n][0]);//%016lld的输出方法
    else
    {
        printf("%lld\n", dp[n][0]);
    }
}

int main()
{
   // freopen("5Ein.txt", "r", stdin);
    //freopen("5Eout.txt", "w", stdout);
    while(scanf("%d%d", &n, &k) != EOF)
    {
        getDp();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值