Codeforces-837D:Round Subset(DP)

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
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 are 3 subsets of 2 numbers. [50, 4] has product 200 with roundness 2[4, 20] — product 80roundness 1[50, 20] — product 1000roundness 3.

In the second example subset [15, 16, 25] has product 6000roundness 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
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<


你可能感兴趣的:(DP)