首页 > 代码库 > Educational Codeforces Round 26 D. Round Subset 动态规划
Educational Codeforces Round 26 D. Round Subset 动态规划
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.
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).
Print maximal roundness of product of the chosen subset of length k.
3 2
50 4 20
3
5 3
15 16 3 25 9
3
3 3
9 77 13
0
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个数,求使得k个数相乘最多有多少个后缀0。
一个神奇的动态规划。dp[i][j]表示取i个数,在有j个5的情况下最多有多少个2
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 #include <cmath> 6 #define ll long long 7 using namespace std; 8 struct Nod { 9 ll a; 10 int x,y; 11 Nod(){ 12 a = x = y = 0; 13 } 14 }nod[220]; 15 void fun(Nod *p) { 16 ll x = p->a; 17 while(x%2 == 0){ 18 p->x++; 19 x/=2; 20 } 21 x = p->a; 22 while(x%5 == 0){ 23 p->y++; 24 x/=5; 25 } 26 } 27 int dp[210][18*210]; 28 bool vis[220]; 29 int main() { 30 int n, k,MAXY=0; 31 cin >> n >> k; 32 for(int i = 1; i <= n; i ++) 33 cin >> nod[i].a, fun(&nod[i]),MAXY += nod[i].y; 34 memset(dp, -1, sizeof(dp)); 35 // for(int i = 1; i <= n; i ++)printf("%d %d\n",nod[i].x,nod[i].y); 36 // printf("%d\n",MAXY); 37 dp[0][0] = 0; 38 for(int i = 1; i <= n; i ++) { 39 for(int j = k; j >= 1; j --) { 40 for(int k = MAXY; k >= 0; k --) { 41 if(k-nod[i].y < 0) continue; 42 if(dp[j-1][k-nod[i].y] == -1) continue; 43 dp[j][k] = max(dp[j][k], dp[j-1][k-nod[i].y]+nod[i].x); 44 } 45 } 46 } 47 int ans = 0; 48 for(int i = 1; i <= MAXY; i ++) { 49 ans = max(ans, min(i,dp[k][i])); 50 } 51 printf("%d\n",ans); 52 return 0; 53 }
Educational Codeforces Round 26 D. Round Subset 动态规划