codeforces837D Round Subset(数学/背包DP)

题目链接:codeforces 837D

题目思路:

要求结果的 0 0 0 最多,不难想到这个数的因子 2 2 2 5 5 5 的个数尽可能多。故将每个数分解,然后就是一个比较简单的背包问题。

参考代码:

#include <iostream>
#include <cstring>
using namespace std;
typedef long long ll;
const int N = 220;
int cnt2[N], cnt5[N];
int dp[N][5010]; // 前i个数,有j个5(也可以计2的个数,不过空间大点)能得到的最多的2的个数
void get2(ll num, int pos) {
    while (num % 2 == 0) {
        cnt2[pos]++;
        num /= 2;
    }
}
void get5(ll num, int pos) {
    while (num % 5 == 0) {
        cnt5[pos]++;
        num /= 5;
    }
}
int main() {
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        ll num; cin >> num;
        get2(num, i);
        get5(num, i);
    }
    memset(dp, -0x3f, sizeof(dp));
    int ans = 0;
    dp[0][0] = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = k; j >= 1; j--) {
            for (int l = cnt5[i]; l <= 5000; l++) {
                dp[j][l] = max(dp[j][l], dp[j-1][l-cnt5[i]] + cnt2[i]);
            }
        }
    }
    for (int i = 1; i <= 5000; i++) {
        ans = max(ans, min(i, dp[k][i])); // 取最多的2和5的状态
    }
    cout << ans << endl;
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值