【CodeForces
来源:互联网 发布:激活windows后重启红屏 编辑:程序博客网 时间:2024/06/04 00:23
D - 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.
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 withroundness 2, [4, 20] — product 80, roundness 1, [50, 20] — product 1000, roundness3.
In the second example subset [15, 16, 25] has product 6000, roundness 3.
In the third example all subsets has product with roundness 0.
题意:给出n个数,让你从中选出m个数,使得这m个数乘积有最大的后缀0。
分析:因为要求后缀0的个数,我们知道只有2和5的乘积才会使后缀产生0,所以我们要把数分为2^x和5^y的形式。用dp[i][j]数组来记录选择j个数中有i个因子5时因子2的数量。状态转移方程:dp[i][j] = max(dp[i][j], dp[i-x][j-1] + y)。最后的答案把dp[i][j]数组扫一遍找到最小的匹配数min(dp[i][j], i)即是答案。
代码如下:
#include <map>#include <cmath>#include <queue>#include <stack>#include <vector>#include <cstdio>#include <string>#include <cstring>#include <iostream>#include <algorithm>#define LL long longusing namespace std;const int MX = 1e5 + 5;const int mod = 1e9 + 7;const int INF = 2e9 + 5;LL a[205];int dp[MX][205];int main(){ int n, m, s = 0; scanf("%d%d", &n, &m); memset(dp, -INF, sizeof(dp)); dp[0][0] = 0; for(int i = 1; i <= n; i++){ LL p; int x = 0, y = 0; scanf("%I64d", &p); while(p%5 == 0){ p /= 5; x++; } while(p%2 == 0){ p /= 2; y++; } s += x; for(int k = min(i, m); k >= 1; k--){ for(int j = s; j >= x; j--){ dp[j][k] = max(dp[j][k], dp[j-x][k-1] + y); } } } int ans = 0; for(int i = 1; i <= s; i++){ ans = max(ans, min(dp[i][m], i)); } printf("%d\n", ans); return 0;}
- codeforces~~~
- Codeforces
- codeforces
- Codeforces
- codeforces
- codeforces
- Codeforces
- Codeforces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- HDU 1452 Happy 2004(约数和定理)
- floyd算法 (算每两点间的距离)
- 计算几何模版
- jquery_selector01
- Unity3D Shader(17)——水的折射
- 【CodeForces
- Docker 镜像的存储机制
- NYOJ-0058-最少步数
- 猪八戒笔试:数串
- 小端格式和大端格式(Little-Endian&Big-Endian)
- 计算文件缓冲大小getCacheDir()和getExternalCacheDir()
- 《web性能权威指南》读书笔记
- HDU --- 5965 扫雷 【dp + 思维】
- 单个fragment中的SurfeceView在多个fragment当中被置于最顶层显示的问题