Codeforces 837D - Round Subset 【DP】
2017-08-04 02:16
405 查看
D. Round Subset
time limit per test 2 seconds
memory limit per test 256 megabytes
of the number the number of zeros to which it ends.
You have an array ofn
numbers. You need to choose a subsetof 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 numbersn
and k
(1 ≤ n ≤ 200, 1 ≤ k ≤ n).
The second line containsn
space-separated integer numbers a1, a2, ..., an
(1 ≤ ai ≤ 1018).
Output
Print maximal roundness of product of the chosen subset of lengthk.
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 are3
subsets of 2
numbers. [50, 4]
has product 200
with roundness2,[4, 20]
— product 80,roundness1,[50, 20]
— product 1000,roundness3.
In the second example subset[15, 16, 25]
has product 6000,roundness3.
In the third example all subsets has product withroundness0.
【题意】
给出n个数,让你从中选出k个数,使得它们乘积的后缀0最多,输出这个值。
【思路】
显然0是由2*5得来的,所以先算出一个数分解质因子后2和5的个数。
然后用dp[i][j]表示选择了i个数,且这i个数质因子中2的个数和为j时,质因子中5的个数和的最大值。状态转移方程为:
dp[j][l]=max(dp[j][l],dp[j-1][l-n2]+n5);
最后扫一遍即可(注意取2的个数与5的个数其中的较小值)
#include <cstdio> #include <algorithm> using namespace std; #define mst(a,b) memset((a),(b),sizeof(a)) #define rush() int T;scanf("%d",&T);while(T--) typedef long long ll; const int maxn = 205; const int maxm = 205*64; const ll mod = 1e9+7; const int INF = 0x3f3f3f; const double eps = 1e-9; int dp[maxn][maxm]; int n,k; ll x; int main() { while(~scanf("%d%d",&n,&k)) { for(int i=0;i<=k;i++) for(int j=0;j<maxm;j++) { dp[i][j]=-INF; } dp[0][0]=0; for(int i=0;i<n;i++) { int n2=0,n5=0; scanf("%I64d",&x); while(x%2==0) x/=2,n2++; while(x%5==0) x/=5,n5++; for(int j=k;j>=1;j--) for(int l=n2;l<maxm;l++) { dp[j][l]=max(dp[j][l],dp[j-1][l-n2]+n5); } } int ans=0; for(int i=1;i<maxm;i++) { ans=max(ans,min(i,dp[k][i])); } printf("%d\n",ans); } return 0; }
相关文章推荐
- Codeforces 837D - Round Subset 【DP】
- Codeforces-837D:Round Subset(DP)
- Codeforces837D - Round Subset 【DP】
- Codeforces 837 D Round Subset
- codeforces 2B The least round way DP因子路径
- Educational Codeforces Round 26 D. Round Subset(dp)
- Educational Codeforces Round 26 D. Round Subset 滚动数组优化DP
- Educational Codeforces Round 26-D-Round Subset(背包DP)
- Codeforces 8VC Venture Cup 2016 - Elimination Round F. Group Projects 差分DP*****
- [杂题 背包DP] Codeforces 793E Tinkoff Challenge - Elimination Round E. Problem of offices
- CodeForces 71 C.Round Table Knights(数论+dp)
- codeforces 2B.The least round way(数学&dp)
- CodeForces 2B The least round way(dp+数学)
- codeforces 2B The least round way(DP+数学)
- [DP 平衡树] Codeforces 809D Round #415 (Div. 1) D. Hitchhiking in the Baltic States
- 【记忆化搜索区间dp】CodeForces 149D Coloring Brackets
- 【dp】codeforces 830-A Office Keys
- Codeforces 710E Generate a String【dp】好题!
- DP Codeforces Round #322 (Div. 2) F. Zublicanes and Mumocrates
- CodeForces - 19E Fairy(图论 + DP)