【CodeForces

来源:互联网 发布:激活windows后重启红屏 编辑:程序博客网 时间:2024/06/04 00:23

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 250 4 20
Output
3
Input
5 315 16 3 25 9
Output
3
Input
3 39 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 longusing 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;}


原创粉丝点击