【dp】codeforces 837D Round Subset

CN_swords發表於2017-08-09

Link:http://codeforces.com/problemset/problem/837/D

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
/*
codeforces 837D Round Subset
題意:找出一個包含k個數的子集,使k個數相乘末尾的0最多。
題解:
dp[i][j] 代表取i個有j個2的最大的5的個數
dp[i][j] = max(dp[i][j],dp[i-1][j-a[g]]+b[g]);
*/
const int N = 210;
const int INF = 0x3f3f3f3f;
int a[N],b[N];
int dp[N][N*65];
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    LL x;
    for(int i = 1; i <= n; i++)
    {
        scanf("%I64d",&x);
        int num = 0;
        while(x%2==0)
        {
            x/=2;
            num++;
        }
        a[i] = num;
        num = 0;
        while(x%5==0)
        {
            x/=5;
            num++;
        }
        b[i] = num;
//        printf("%d %d\n",a[i],b[i]);
    }
    memset(dp,-INF,sizeof(dp));
    dp[0][0] = 0;
    for(int g = 1; g <= n; g++)
    {
        for(int i = min(k,g); i >= 1; i--)
        {
            for(int j = a[g]; j < N*65; j++)
                dp[i][j] = max(dp[i][j],dp[i-1][j-a[g]]+b[g]);
        }
    }
    int ans = 0;
    for(int i = 1; i < N*65; i++)
        ans = max(ans,min(i,dp[k][i]));
    printf("%d\n",ans);
    return 0;
}


相關文章