Codeforces837D - Round Subset 【DP】
2017-08-25 13:16
316 查看
D. Round Subset
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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
output
input
output
input
output
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.
【题意】
给出n个数,让你从中选出k个数,使得它们乘积的后缀0最多,输出这个值。
【思路】
显然0是由2*5得来的,所以先算出一个数分解质因子后2和5的个数。
然后用dp[i][j]表示选择了i个数,且这i个数质因子中5的个数和为j时,质因子中2的个数和的最大值。状态转移方程为:
dp[j][l]=max(dp[j][l],dp[j-1][l-n5]+n2);
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define N 205
int n, k, dp
[6000];
int two
, five
;
void solve()
{
memset(two, 0, sizeof(two));
memset(five, 0, sizeof(five));
memset(dp, 0xbf, sizeof(dp));
int tf=0;
for(int i=1; i<=n; i++)
{
LL x, tmp;cin>>x;tmp=x;
while(x%2==0)
{
two[i]++;
x/=2;
}
x=tmp;
while(x%5==0)
{
five[i]++;
x/=5;
tf++;
}
}
dp[0][0] = 0;
int ans = 0;
for(int i=1; i<=n; i++)
for(int j = min(k, i); j >= 1; j--)
for(int k = tf; k >= five[i]; k--)
dp[j][k] = max(dp[j][k], dp[j-1][k-five[i]]+two[i]);
for(int i = 1; i <= tf; i++)
ans = max(ans, min(dp[k][i], i));
cout << ans << endl;
}
int main()
{
while(cin>>n>>k)
{
solve();
}
return 0;
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 315 16 3 25 9
output
3
input
3 39 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.
【题意】
给出n个数,让你从中选出k个数,使得它们乘积的后缀0最多,输出这个值。
【思路】
显然0是由2*5得来的,所以先算出一个数分解质因子后2和5的个数。
然后用dp[i][j]表示选择了i个数,且这i个数质因子中5的个数和为j时,质因子中2的个数和的最大值。状态转移方程为:
dp[j][l]=max(dp[j][l],dp[j-1][l-n5]+n2);
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define N 205
int n, k, dp
[6000];
int two
, five
;
void solve()
{
memset(two, 0, sizeof(two));
memset(five, 0, sizeof(five));
memset(dp, 0xbf, sizeof(dp));
int tf=0;
for(int i=1; i<=n; i++)
{
LL x, tmp;cin>>x;tmp=x;
while(x%2==0)
{
two[i]++;
x/=2;
}
x=tmp;
while(x%5==0)
{
five[i]++;
x/=5;
tf++;
}
}
dp[0][0] = 0;
int ans = 0;
for(int i=1; i<=n; i++)
for(int j = min(k, i); j >= 1; j--)
for(int k = tf; k >= five[i]; k--)
dp[j][k] = max(dp[j][k], dp[j-1][k-five[i]]+two[i]);
for(int i = 1; i <= tf; i++)
ans = max(ans, min(dp[k][i], i));
cout << ans << endl;
}
int main()
{
while(cin>>n>>k)
{
solve();
}
return 0;
}
相关文章推荐
- Educational Codeforces Round 26 D. Round Subset(dp)
- Codeforces 837D - Round Subset 【DP】
- Codeforces 837D - Round Subset 【DP】
- Educational Codeforces Round 26 D. Round Subset 滚动数组优化DP
- Educational Codeforces Round 26-D-Round Subset(背包DP)
- Codeforces-837D:Round Subset(DP)
- 【HDU5188 BestCoder Round 33C】【贪心排序+DP】zhx and contest 考试不被怀疑作弊条件下达到至少m分的最少时间
- Codeforces Beta Round #50 C. First Digit Law(概率dp,好题)
- Codeforces Round #468 (Div. 2): F. Teodor is not a liar!(DP)
- hdu 6092 Rikka with Subset 背包dp
- 【Codeforces Beta Round 2B】【贪心 DP】The least round way 从(1,1)走到(n,n)乘积尾数0尽可能少
- Codeforces Round #260 (Div. 2) A , B , C 标记,找规律 , dp
- Codeforces Round #290 (Div. 2) D. Fox And Jumping dp
- Codeforces Round #274 (Div. 2) E:Riding in a Lift DP + 前缀优化
- Codeforces Round #FF (Div. 2/C)/Codeforces446A_DZY Loves Sequences(DP)
- 8VC Venture Cup 2016 - Elimination Round F. Group Projects dp
- Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] C. Weakness and Poorness 三分 dp
- Educational Codeforces Round 8 D. Magic Numbers 数位DP
- Codeforces Round #277.5(Div. 2) F. Special Matrices【思维+Dp】好题~好题~
- Codeforces Round #274 Div.1 C Riding in a Lift --DP