Codeforces-837D:Round Subset(DP)
来源:互联网 发布:刘雯怎么培养气质知乎 编辑:程序博客网 时间:2024/06/07 13:31
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 250 4 20
3
5 315 16 3 25 9
3
3 39 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.
思路:因为末尾0的个数来源于2和5的个数。所以用d[i][j][k]表示前i个数中取j个数且因子5的个数为k时,因子2的个数的最大值。ans=max(d[i][j][k],k)。
#include<bits/stdc++.h>using namespace std;int d[2][210][6000],n,m;int five(__int64 x){ int sum=0; while(x%5==0)sum++,x/=5; return sum;}int two(__int64 x){ int sum=0; while(x%2==0)sum++,x/=2; return sum;}int main(){ __int64 x; int ans=0,pre=0,now=1; memset(d,-1,sizeof d); d[0][0][0]=0; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%I64d",&x); int f=five(x),t=two(x); for(int j=0;j<=i&&j<=m;j++) { for(int k=0;k<=26*i;k++) { if(j+1<=i&&j+1<=m&&d[pre][j][k]!=-1)d[now][j+1][k+f]=max(d[now][j+1][k+f],d[pre][j][k]+t); d[now][j][k]=max(d[now][j][k],d[pre][j][k]); ans=max(ans,min(d[now][m][k],k)); } } memset(d[pre],-1,sizeof d[pre]);//二维滚动数组 pre^=1; now^=1; } cout<<ans<<endl; return 0;}
- Codeforces-837D Round Subset(dp)
- Codeforces-837D:Round Subset(DP)
- 【dp】codeforces 837D Round Subset
- codeforces 837D Round Subset (dp)
- Codeforces 837 D Round Subset
- Codeforces 837D Round Subset :随便来个DP
- Codeforces 837D Round Subset【思维+Dp+滚动数组】
- 837D. Round Subset
- Educational Codeforces Round 26 D Round Subset
- Educational Codeforces Round 26 D. Round Subset 滚动数组优化DP
- Educational Codeforces Round 12 D. Simple Subset
- Educational Codeforces Round 12 D. Simple Subset
- cf Educational Codeforces Round 26 D. Round Subset
- 【DP】Codeforces837D. Round Subset
- Codeforces Beta Round #51 D. Beautiful numbers (数位dp)
- Codeforces Round #263 (Div. 2)D(树形DP)
- Codeforces Round #264 (Div. 2)D(图论+dp)
- Codeforces Round #266 (Div. 2)D(DP)
- python学习(二十)
- react-native实现百度地图的实时导航
- VC、Linux、vxWorks读写物理扇区小结
- HTML代码中文在网页中乱码问题
- Java_冒泡、插入、快速、选择排序
- Codeforces-837D:Round Subset(DP)
- 决策树(Decision Tree)Demo
- C语言——宏定义及保留n位小数
- TNS-12547: TNS:lost contact解决办法
- lua.c:80:31: fatal error: readline/readline.h: No such file or directory
- php语言的cmpp协议应用
- 【算法——Python实现】有权图求单源最短路径Dijkstra算法
- 原生JS实现H5页面侧滑删除的效果
- 利用vnc在ubuntu14.04搭建服务器方法步骤简述——服务器端