Codeforces 837D Round Subset - 动态规划 - 数论

时间:2023-03-09 07:35:16
Codeforces 837D 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.

Examples
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 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.


  题目大意 给定一个数组,从中选出k个数(不能选同一个数),使得这k个数的乘积的末尾的零的个数最多。

  根据小学的数学知识,我们知道一个数的末尾有多个零取决于它质因数分解后2的指数和5的指数的最小值。(10 = 2 × 5)

  所以我们初步得到动态规划的状态f[i][j][k]表示,从前i个数中,选出j个数,使得它们的乘积质因数分解后2的指数为k,5的指数最大为多少。

  显然它有两种转移:选第i + 1个数,不选第i + 1个数。所以转移是显然的。

  然后您会得到MLE,所以加上黑科技bfs版 + 滚动数组动态规划,不知道能不能卡过,但是有一种很简单的优化方法。

  显然将题目中输入的一个数质因数分解后5的指数不会超过Codeforces 837D Round Subset - 动态规划 - 数论。所以我们可以把5个个数作为状态,2的个数作为状态的值,这样总状态数不会超过2003 * 25(刚刚是乘64)

  所以继续黑科技优化内存和时间就过了。

Code

 /**
* Codeforces
* Problem#837D
* Accepted
* Time: 187ms
* Memory: 10604k
*/
#include <bits/stdc++.h>
using namespace std;
typedef bool boolean;
#define smax(a, b) a = max(a, b);
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) {
ungetc(x, stdin);
return false;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
} typedef class Status {
public:
int stage;
int seced;
int c5;
}Status; int n, k;
int *c2s, *c5s;
int res = ; inline void init() {
long long x;
readInteger(n);
readInteger(k);
c2s = new int[(n + )];
c5s = new int[(n + )];
for(int i = ; i <= n; i++) {
c2s[i] = c5s[i] = ;
readInteger(x);
while(!(x & )) x >>= , c2s[i]++;
while(!(x % )) x /= , c5s[i]++;
}
} queue<Status> que;
int f[][][];
inline void dp() {
int last = ;
Status sta = (Status) {, , };
que.push(sta);
memset(f, -, sizeof(f));
f[][][] = ;
while(!que.empty()) {
Status e = que.front();
que.pop(); int lastf = f[e.stage & ][e.seced][e.c5];
Status eu = e;
eu.stage++;
if(eu.stage != last) {
last = eu.stage;
memset(f[eu.stage & ], -, sizeof(f[]));
} if(f[eu.stage & ][eu.seced][eu.c5] == - && eu.stage < n && eu.seced <= k)
que.push(eu);
else if(eu.stage == n && eu.seced == k)
smax(res, min(eu.c5, lastf));
smax(f[eu.stage & ][eu.seced][eu.c5], lastf); eu.seced++, eu.c5 += c5s[eu.stage];
if(f[eu.stage & ][eu.seced][eu.c5] == - && eu.stage < n && eu.seced <= k)
que.push(eu);
else if(eu.stage == n && eu.seced == k)
smax(res, min(eu.c5, lastf + c2s[eu.stage]));
smax(f[eu.stage & ][eu.seced][eu.c5], lastf + c2s[eu.stage]);
}
} inline void solve() {
printf("%d\n", res);
} int main() {
init();
dp();
solve();
return ;
}