【CodeForces - 837D Round Subset】 DP

D - Round Subset


Let's call the roundness of the number the number of zeros to which it ends.

You have an array of n numbers. You need to choose a subset of exactly k numbers so that the roundness of the product of the selected numbers will be maximum possible.

Input

The first line contains two integer numbers n and k (1 ≤ n ≤ 200, 1 ≤ k ≤ n).

The second line contains n space-separated integer numbers a1, a2, ..., an (1 ≤ ai ≤ 1018).

Output

Print maximal roundness of product of the chosen subset of length k.

Example
Input
3 2
50 4 20
Output
3
Input
5 3
15 16 3 25 9
Output
3
Input
3 3
9 77 13
Output
0
Note

In the first example there are 3 subsets of 2 numbers. [50, 4] has product 200 withroundness 2[4, 20] — product 80roundness 1[50, 20] — product 1000roundness3.

In the second example subset [15, 16, 25] has product 6000roundness 3.

In the third example all subsets has product with roundness 0.


题意:给出n个数,让你从中选出m个数,使得这m个数乘积有最大的后缀0。


分析:因为要求后缀0的个数,我们知道只有2和5的乘积才会使后缀产生0,所以我们要把数分为2^x和5^y的形式。用dp[i][j]数组来记录选择j个数中有i个因子5时因子2的数量。状态转移方程:dp[i][j] = max(dp[i][j], dp[i-x][j-1] + y)。最后的答案把dp[i][j]数组扫一遍找到最小的匹配数min(dp[i][j], i)即是答案。


代码如下:

#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long

using namespace std;
const int MX = 1e5 + 5;
const int mod = 1e9 + 7;
const int INF = 2e9 + 5;

LL a[205];
int dp[MX][205];

int main(){
    int n, m, s = 0;
    scanf("%d%d", &n, &m);
    memset(dp, -INF, sizeof(dp));
    dp[0][0] = 0;
    for(int i = 1; i <= n; i++){
        LL p;
        int x = 0, y = 0;
        scanf("%I64d", &p);
        while(p%5 == 0){
            p /= 5;
            x++;
        }
        while(p%2 == 0){
            p /= 2;
            y++;
        }
        s += x;
        for(int k = min(i, m); k >= 1; k--){
            for(int j = s; j >= x; j--){
                dp[j][k] = max(dp[j][k], dp[j-x][k-1] + y);
            }
        }
    }
    int ans = 0;
    for(int i = 1; i <= s; i++){
        ans = max(ans, min(dp[i][m], i));
    }
    printf("%d\n", ans);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值