Codeforces 837D Round Subset【思维+Dp+滚动数组】
来源:互联网 发布:网络嗅探器5.5 编辑:程序博客网 时间:2024/06/06 16:13
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 exactlyk numbers so that the roundness of the product of the selected numbers will be maximum possible.
The first line contains two integer numbers n andk (1 ≤ n ≤ 200, 1 ≤ k ≤ n).
The second line contains n space-separated integer numbersa1, 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 of2 numbers. [50, 4] has product200 with roundness2, [4, 20] — product80, roundness1, [50, 20] — product1000, roundness3.
In the second example subset [15, 16, 25] has product6000, roundness3.
In the third example all subsets has product with roundness 0.
题目大意:
给你N个数,可以从中任意取出K个数,使得其K个数相乘最末尾的0的个数最多,问最多0的个数。
思路:
很显然,如果我们可以选的数中,没有2的倍数的数,也没有5的倍数的数的话,无论怎样相乘得到的结果都一定不会出现末尾的0.
如果我们可以选的数中,有2的倍数的数,但是没有5的倍数的数的话,无论怎样相乘得到的结果都一定不会出现"新"的末尾的0,那么我们考虑问题的关键点,就在于相乘的这K个数中,有多少个2,又有多少个5..
那么我们处理出num_two【i】,表示第i个数中包含多少个2(while(num%2==0)num_two[i]++),同理再预处理出num_fIve【i】;
那么我们考虑最优的去Dp,设定dp【i】【j】【k】表示我们进行Dp到第i个数,选了j个数,2的个数为k个的话,能够获得的5的个数的最大个数。
那么不难写出其状态转移方程(因为内存开不出那么大,所以我们滚动一下数组):
那么ans=max(ans,min(k,dp【1】【j】【k】));
Ac之后发现,其实我们如果去换一个角度 ,用5去Dp2的个数的话,会更优,我这样做需要开内存为Dp【2】【205】【12800+】;而换了角度就可以压下去第三维的内存和时间。
Ac代码:
#include<stdio.h>#include<iostream>#include<string.h>using namespace std;#define ll __int64ll a[250];int num_two[250];int num_five[250];int dp[2][205][12850];int Get_two(ll num){ int sum=0; while(num%2==0)num/=2,sum++; return sum;}int Get_five(ll num){ int sum=0; while(num%5==0)num/=5,sum++; return sum;}int main(){ int n,m; while(~scanf("%d%d",&n,&m)) { memset(dp,-1,sizeof(dp)); for(int i=1;i<=n;i++)scanf("%I64d",&a[i]); for(int i=1;i<=n;i++) { num_two[i]=Get_two(a[i]); num_five[i]=Get_five(a[i]); } int output=0; dp[0][0][0]=0; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { for(int k=12805;k>=0;k--) { dp[1][j][k]=max(dp[1][j][k],dp[0][j][k]); if(k>=num_two[i]&&dp[0][j-1][k-num_two[i]]!=-1) dp[1][j][k]=max(dp[1][j][k],dp[0][j-1][k-num_two[i]]+num_five[i]); output=max(output,min(k,dp[1][j][k])); } } for(int j=1;j<=m;j++) { for(int k=12805;k>=0;k--) { dp[0][j][k]=dp[1][j][k]; } } } printf("%d\n",output); }}
- Codeforces 837D Round Subset【思维+Dp+滚动数组】
- Educational Codeforces Round 26 D. Round Subset 滚动数组优化DP
- Codeforces-837D Round Subset(dp)
- 【dp】codeforces 837D Round Subset
- codeforces 837D Round Subset (dp)
- Codeforces-837D:Round Subset(DP)
- Codeforces 837 D Round Subset
- Codeforces 837D Round Subset :随便来个DP
- Codeforces Round #273 (Div. 2)(D)dp,滚动数组
- 837D. Round Subset
- Educational Codeforces Round 26 D Round Subset
- [思维] codeforces 665D. Simple Subset
- 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 Round #387(Div. 2)D. Winter Is Coming【思维+dp】
- Codeforces Round #426 (Div. 2) C. The Meaningless Game 思维 D. The Bakery dp
- POJ2761[Feed the dogs ] 不带修改主席树
- Linux操作系统原理与应用(陈莉君)——学习笔记
- GNU工具-gperf详解(完美哈希函数生成器)
- JavaScript运算符(操作符)详解(1)----基本运算符
- Android 四大组件之 Service (上)
- Codeforces 837D Round Subset【思维+Dp+滚动数组】
- JS实现继承的几种方式
- GNU C中嵌入汇编添加一个自定义uboot命令
- opencv常见函数
- Java8 Lambda语法 示例
- serializer嵌套序列化
- Counting Divisors (2017 多校4)“素 数 筛”
- 数据结构之二叉树
- uva1600-Patrol Robot(bfs)