[CF837D] Round Subset

[CF837D] Round Subset - dp

Description

给你一个长度为 (n) 的数列,要求你从中选出 (k) 个数,使得这些选出的数的积的末尾 (0) 的个数大。

Solution

(f[i][j][k]) 表示从前 i 个数中选了 j 个,已经有了 k 个 5,最多能有多少个 2

#include <bits/stdc++.h>
using namespace std;

#define int long long

const int N = 205;
const int M = 5005;

int n, k, a[N], f[N][M];

signed main()
{
    ios::sync_with_stdio(false);

    cin >> n >> k;
    for (int i = 1; i <= n; i++)
        cin >> a[i];

    memset(f, -0x3f, sizeof f);

    f[0][0] = 0;

    for (int t = 1; t <= n; t++)
    {
        int x = a[t];
        int c2 = 0, c5 = 0;
        while (x % 2 == 0)
            x /= 2, ++c2;
        while (x % 5 == 0)
            x /= 5, ++c5;
        for (int i = k; i >= 1; i--)
            for (int j = 0; j < M; j++)
                if (j + c5 < M)
                    f[i][j + c5] = max(f[i][j + c5], f[i - 1][j] + c2);
    }

    int ans = 0;
    for (int i = 0; i < M; i++)
        ans = max(ans, min(i, f[k][i]));
    cout << ans << endl;
}
原文地址:https://www.cnblogs.com/mollnn/p/14361964.html