Codeforces 837D - Round Subset DP

先算出每个数的pop1(twonum),pop(fivenum)然后DP ans[i][j]表示选i个数有j个2时最多有多少个5

转移方程是

        for(int j=k;j>=1;j--)
        {
        for(int w=pop1;w<5000;w++)
        {
        ans[j][w]=max(ans[j][w],ans[j-1][w-pop1]+pop);
        }
        }

AC程序:

#include <bits/stdc++.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#include<queue>
#define EPS 1.0e-9
#define PI acos(-1.0)
#define INF 30000000
#define MOD 1000000007
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define pb push_back
#define pai pair<int,int>
//using ll = long long;
//using ull= unsigned long long;
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que;
typedef long long ll;
typedef unsigned long long ull;
const int maxn=1005;
struct num
{
  int two;
  int five;
}number[205];
ll ans[205][5001];
int main()
{
        mem(ans,-1);
        ans[0][0]=0;
    int n;
    int k;
    cin >> n >> k;
    int now;
    int pop,pop1;
    num temp;
    for(int i=1;i<=n;i++)
        {
        pop=pop1=0;
        cin >> now;
        while(now&&now%5==0)
        {
        pop++;
        now/=5;
        }
        while(now&&now%2==0)
        {
        pop1++;
        now/=2;
        }
        for(int j=k;j>=1;j--)
        {
        for(int w=pop1;w<5000;w++)
        {
        ans[j][w]=max(ans[j][w],ans[j-1][w-pop1]+pop);
        }
        }
        }
        ll anser=0;
        for(ll i=1;i<=5000;i++)
        anser=max(anser,min(i,ans[k][i]));
        cout<<anser<<endl;
    return 0;
}
时间: 2024-11-05 09:57:10

Codeforces 837D - Round Subset DP的相关文章

Codeforces 837D Round Subset(背包)

题目链接  Round Subset 题意  在n个数中选择k个数,求这k个数乘积末尾0个数的最大值. 首先我们预处理出每个数5的因子个数c[i]和2的因子个数d[i] 然后就可以背包了. 设f[i][j]为选i个数,5的因子总和为j时,2的因子总和的最大值. 则状态转移方程为 $f[i][j] = max(f[i - 1][j - c[k]] + d[k])$ 注意边界条件 时间复杂度$O(5200nk)$ #include <bits/stdc++.h> using namespace s

Codeforces 837D 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. Input

Educational Codeforces Round 26 D. Round Subset(dp)

题目链接:Educational Codeforces Round 26 D. Round Subset 题意: 给你n个数,让你选其中的k个数,使得这k个数的乘积的末尾的0的个数最大. 题解: 显然,末尾乘积0的个数和因子2和因子5的个数有关. 然后考虑dp[i][j]表示选i个数,当前因子5的个数为j时,能得到因子2最多的为多少. 那么对于每个数,记录一下因子2和5的个数,做一些01背包就行了. 1 #include<bits/stdc++.h> 2 #define mst(a,b) me

Educational Codeforces Round 26 D. Round Subset 动态规划

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

暴力/DP Codeforces Beta Round #22 (Div. 2 Only) B. Bargaining Table

题目传送门 1 /* 2 题意:求最大矩形(全0)的面积 3 暴力/dp:每对一个0查看它左下的最大矩形面积,更新ans 4 注意:是字符串,没用空格,好事多磨,WA了多少次才发现:( 5 详细解释:http://www.cnblogs.com/cszlg/p/3217478.html 6 */ 7 #include <cstdio> 8 #include <algorithm> 9 #include <cstring> 10 #include <cmath>

Codeforces Beta Round #85 (Div. 1 Only) C (状态压缩或是数学?)

C. Petya and Spiders Little Petya loves training spiders. Petya has a board n × m in size. Each cell of the board initially has a spider sitting on it. After one second Petya chooses a certain action for each spider, and all of them humbly perform it

【动态规划】Round Subset

CF837D. 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 ma

D - Round Subset codeforces837d

D - Round Subset 思路:背包: 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define ll long long ll n,m,dp[250][10050],tmp,p2,p5,pos,sum,ans; int main() { //freopen("data.txt&

Codeforces Beta Round#2

Codeforces Beta Round#2 http://codeforces.com/contest/2 A 模拟题 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 5 map<string,ll>mp; 6 struct sair{ 7 string str; 8 int id; 9 ll num; 10 }a[1005]; 11 12 bool cmp(sair a,sa