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 with roundness 2, [4, 20] — product 80, roundness 1,[50, 20] — product 1000, roundness 3.
In the second example subset [15, 16, 25] has product 6000, roundness 3.
In the third example all subsets has product with roundness 0.
每输入一个数,就把分解为x个2,和y个5,则就转化为背包问题
1 #include <iostream> 2 #include <stdio.h> 3 using namespace std; 4 const int INF = 0x3f3f3f3f; 5 int dp[205][8010]; 6 int tdp[205][8010]; 7 int in[205][2]; 8 int main() { 9 int N, K, i, j, k; 10 scanf("%d %d", &N, &K); 11 for (i = 1; i <= N; i++) { 12 ll t; 13 scanf("%lld", &t); 14 while (t % 2 == 0) { 15 in[i][0]++; 16 t /= 2; 17 } 18 while (t % 5 == 0) { 19 in[i][1]++; 20 t /= 5; 21 } 22 } 23 24 for (i = 0; i <= K; i++) 25 for (j = 0; j <= 8000; j++) dp[i][j] = -INF; 26 dp[0][0] = 0; 27 for (i = 1; i <= N; i++) { 28 for (j = 0; j <= K; j++) 29 for (k = 0; k <= 8000; k++) 30 tdp[j][k] = dp[j][k]; 31 for (j = 0; j < K; j++) { 32 for (k = 0; k <= 8000; k++) { 33 int j2 = j + 1, k2 = k + in[i][1]; 34 if (k2 <= 8000) tdp[j2][k2] = max(tdp[j2][k2], dp[j][k] + in[i][0]); 35 } 36 } 37 for (j = 0; j <= K; j++) for (k = 0; k <= 8000; k++) dp[j][k] = tdp[j][k]; 38 } 39 40 int ans = 0; 41 for (i = 0; i <= K; i++) for (j = 0; j <= 8000; j++) ans = max(ans, min(j, dp[i][j])); 42 return !printf("%d ", ans); 43 }