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.
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).
Print maximal roundness of product of the chosen subset of length k.
3 2 50 4 20
3
5 3 15 16 3 25 9
3
3 3 9 77 13
0
In the first example there are 3 subsets of 2 numbers. [50, 4] has product 200 withroundness 2, [4, 20] — product 80, roundness 1, [50, 20] — product 1000, roundness3.
In the second example subset [15, 16, 25] has product 6000, roundness 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;
}