• Codeforces 837D 动态规划


    Codeforces 837D 动态规划

    传送门:https://codeforces.com/contest/837/problem/D

    题意:

    给你n个数,问你从这n个数中取出k个数,这k个数的乘积的末尾最多有多少个0

    题解:

    要想让乘积的末尾有0,实际上就是2的倍数和5的倍数相乘才能得到贡献,所以每个数对答案的贡献实际上就是这个数中包含的2的个数还有这个数中包含的5的数对答案的贡献

    设定dp状态为

    (dp[i][j]表示从n个数中选出i个数,其中有j个5的个数,最多有多少个2)

    边界 dp[0][0]=0, else dp[i][j]=-INF

    转移:dp[i][j]=max(dp[i][j],dp[i-1][j-cnt5[i]]+cnt2[i])

    代码:

    #include <set>
    #include <map>
    #include <cmath>
    #include <cstdio>
    #include <string>
    #include <vector>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    typedef long long LL;
    typedef pair<int, int> pii;
    typedef unsigned long long uLL;
    #define ls rt<<1
    #define rs rt<<1|1
    #define lson l,mid,rt<<1
    #define rson mid+1,r,rt<<1|1
    #define bug printf("*********
    ")
    #define FIN freopen("input.txt","r",stdin);
    #define FON freopen("output.txt","w+",stdout);
    #define IO ios::sync_with_stdio(false),cin.tie(0)
    #define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]
    "
    #define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]
    "
    #define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]
    "
    const int maxn = 3e5 + 5;
    const int INF = 0x3f3f3f3f;
    const int mod = 1e9 + 7;
    LL quick_pow(LL x, LL y) {
        LL ans = 1;
        while(y) {
            if(y & 1) {
                ans = ans * x % mod;
            } x = x * x % mod;
            y >>= 1;
        } return ans;
    }
    struct node {
        int cnt2;
        int cnt5;
    } a[maxn];
    int dp[205][205 * 64];
    int main() {
    #ifndef ONLINE_JUDGE
        FIN
    #endif
        int n, k;
        scanf("%d%d", &n, &k);
        for(int i = 1; i <= n; i++) {
            LL x;
            scanf("%lld", &x);
            while(x % 2 == 0) {
                a[i].cnt2++;
                x /= 2;
            }
            while(x % 5 == 0) {
                a[i].cnt5++;
                x /= 5;
            }
        }
        LL sum = 0;
        memset(dp, -INF, sizeof(dp));
        dp[0][0]=0;
        for(int i = 1; i <= n; i++) {
            sum += a[i].cnt5;
                    // debug1(a[i].cnt2);
                // debug1(sum);
            for(int j = min(k, i); j >= 1; j--) {
                for(int k = sum; k >= a[i].cnt5; k--) {
                    // debug2(k,a[i].cnt5);
                    dp[j][k] = max(dp[j][k], dp[j - 1][k - a[i].cnt5] + a[i].cnt2);
                    // debug3(j,k,dp[j][k]);
                }
            }
        }
        LL ans = 0;
        for(int i = 1; i <= sum; i++) {
            ans = max(ans, 1LL * min(i, dp[k][i]));
        }
        printf("%lld
    ", ans);
        return 0;
    }
    
    每一个不曾刷题的日子 都是对生命的辜负 从弱小到强大,需要一段时间的沉淀,就是现在了 ~buerdepepeqi
  • 相关阅读:
    [Bash] Shortcut
    [Bash] Rerun Bash Commands with History Expansions (!! & !$)
    [Bash] Create and Copy Multiple Files with Brace Expansions in Bash
    [Bash] Add Executable Files to $PATH with Bash
    [Typescript] Create Type From Any Given Data in TypeScript
    [Typescript] Ignore Null or Undefined Values with TypeScript Non-Null Assertion Operator
    [Bash] Chain Commands with Pipes and Redirect Output in Bash
    [Bash] Use Conditional Statements in Bash
    [Bash] Understand Exit Statuses in Bash
    监听内容变化 TextWatcher @功能 [MD]
  • 原文地址:https://www.cnblogs.com/buerdepepeqi/p/11230452.html
Copyright © 2020-2023  润新知