Codeforces 837D Round Subset :随便来个DP

来源:互联网 发布:免费申请域名建立网站 编辑:程序博客网 时间:2024/06/05 16:38

弱智题。。。辣眼睛。。不要看。。。orz

题意:给出n(<=200)个数字,每个数字<=1e18。现在取其中k(<=n)个相乘,求其末尾0个数的最大值。

题解:显然可以把数字分成2^x和5^y剩下的部分都没有用。对于每个数字记下x和y。考虑一个数字最大是10^18,最小的因子是2,所以x最大值是18/lg2 <54所以总共200个数字2的个数小于10800。用dp[ i ][ j ]表示选择了 j 个数字,总共有 i 个2,5的最大数量。然后在枚举min( i , dp[ i ][ k ] )取最大的一个就可以了

Code:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int n, m, s;int f[10050][250];int main() {memset(f, 0x80, sizeof(f));f[0][0]=0;scanf("%d%d", &n, &m);for(int i=1; i<=n; ++i) {long long p; int x=0, y=0; scanf("%I64d", &p);for(; !(p%5); p/=5) ++x;for(; !(p%2); p/=2) ++y;s+=x;for(int k=min(i, m); k>=1; --k)for(int j=s; j>=x; --j)f[j][k]=max(f[j][k], f[j-x][k-1]+y);}int ans=0;for(int i=1; i<=s; ++i) ans=max(ans, min(i, f[i][m]));printf("%d\n", ans);return 0;}