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
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.
#include<algorithm> #include<iostream> #include<string> #include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0}; #include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;} #include<vector> #include<cmath> #include<stack> #include<string.h> #include<stdlib.h> #include<cstdio> #define maxn 220 #define UB (maxn*64) #define ll __int64 #define INF 10000000 using namespace std; const int inf =0x3f3f3f3f; int n,k; ll seq[maxn]; int c2[maxn],c5[maxn]; ll dp[maxn][UB]; /* 题目大意:给定n个数要求选恰好k个数,, 要求求出其乘积尾零最多的个数。 刚开始没看到k的限制,, 潇洒的写出了二维dp(可压缩),, 尾零取决于2和5因子的个数的最小值,, 所以可把每个数拆成2和5的个数的二元组,, 对于这个二元组序列采用背包做法。。
*/ int main() { //scanf("%d%d",&n,&k); cin>>n>>k; for(int i=0;i<n;i++) { cin>>seq[i]; ///scanf("%I64d",&seq[i]); while(seq[i]%2==0) { seq[i]/=2; c2[i]++; } while(seq[i]%5==0) { seq[i]/=5; c5[i]++; } } memset(dp,-inf,sizeof(dp)); dp[0][0]=0; for(int i=0;i<n;i++) { for(int j=k;j>=1;j--) for(int p=c2[i];p<UB;p++) { ///ll tp=dp[j-1][p-c2[i]]; // if(tp==-1) continue; dp[j][p] = max( dp[j][p] , dp[j-1][p-c2[i]]+c5[i] ); } /* cout<<i<<"次"<<endl; for(int j=1;j<=k;j++) { for(int p=0;p<=UB;p++) cout<<dp[j][p]<<" "; cout<<endl; }*/ } ll ans=0; for(ll i=0;i<UB;i++) { ///if(dp[k][i]==-1) continue; ans=max(ans,min(dp[k][i],i)); } cout<<ans<<endl; //printf("%I64d\n",ans); return 0; }