Codeforces 837D Round Set (二维背包dp)

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.

 
#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; }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值