zoukankan      html  css  js  c++  java
  • Codeforces 1172C2 Nauuo and Pictures (hard version) dp

    Nauuo and Pictures (hard version

    首先考虑简单版本的, 一个一个dp求出来, 分成三坨, 一坨当前要求照片, 一坨除了当前的喜欢的照片, 一坨除了当前的讨厌的照片。

    单次dp   50 ^ 4

    感觉hard的也挺简单的。。 

    我们先算出最后喜欢的照片的总w, 和讨厌的照片的总w, 然后每个的贡献就是在原先的w中所占的比例。

    #include<bits/stdc++.h>
    #define LL long long
    #define LD long double
    #define ull unsigned long long
    #define fi first
    #define se second
    #define mk make_pair
    #define PLL pair<LL, LL>
    #define PLI pair<LL, int>
    #define PII pair<int, int>
    #define SZ(x) ((int)x.size())
    #define ALL(x) (x).begin(), (x).end()
    #define fio ios::sync_with_stdio(false); cin.tie(0);
    
    using namespace std;
    
    const int N = 3000 + 7;
    const int inf = 0x3f3f3f3f;
    const LL INF = 0x3f3f3f3f3f3f3f3f;
    const int mod = 998244353;
    const double eps = 1e-10;
    const double PI = acos(-1);
    
    template<class T, class S> inline void add(T &a, S b) {a += b; if(a >= mod) a -= mod;}
    template<class T, class S> inline void sub(T &a, S b) {a -= b; if(a < 0) a += mod;}
    template<class T, class S> inline bool chkmax(T &a, S b) {return a < b ? a = b, true : false;}
    template<class T, class S> inline bool chkmin(T &a, S b) {return a > b ? a = b, true : false;}
    
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    
    int power(int a, int b) {
        int ans = 1;
        while(b) {
            if(b & 1) ans = 1LL * ans * a % mod;
            a = 1LL * a * a % mod; b >>= 1;
        }
        return ans;
    }
    
    int n, m, a[200007], w[200007], r[200007];
    int sumL, sumH, EL, EH;
    
    int dp[N][N];
    
    int main() {
    
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
        for(int i = 1; i <= n; i++) {
            scanf("%d", &w[i]);
            if(a[i]) sumL += w[i];
            else sumH += w[i];
        }
    
        EL = sumL;
        EH = sumH;
    
        dp[0][0] = 1;
    
        for(int i = 0; i < m; i++) {
            for(int a = 0; a <= i; a++) {
                int b = i - a;
                add(dp[i + 1][a + 1], 1LL * dp[i][a] * (sumL + a) % mod * power(sumL + sumH + a - b, mod - 2) % mod);
                add(dp[i + 1][a], 1LL * dp[i][a] * (sumH - b) % mod * power(sumL + sumH + a - b, mod - 2) % mod);
            }
        }
    
        for(int a = 0; a <= m; a++) {
            int b = m - a;
            add(EL, 1LL * a * dp[m][a] % mod);
            sub(EH, 1LL * b * dp[m][a] % mod);
        }
    
        for(int i = 1; i <= n; i++) {
            if(a[i]) {
    
                r[i] = 1LL * EL * w[i] % mod * power(sumL, mod - 2) % mod;
    
            } else {
    
                r[i] = 1LL * EH * w[i] % mod * power(sumH, mod - 2) % mod;
    
            }
        }
    
        for(int i = 1; i <= n; i++) printf("%d
    ", r[i]);
        return 0;
    }
    
    /*
    */
  • 相关阅读:
    /etc/sysctl.conf 控制内核相关配置文件
    python 并发编程 非阻塞IO模型
    python 并发编程 多路复用IO模型
    python 并发编程 异步IO模型
    python 并发编程 阻塞IO模型
    python 并发编程 基于gevent模块 协程池 实现并发的套接字通信
    python 并发编程 基于gevent模块实现并发的套接字通信
    python 并发编程 io模型 目录
    python 并发编程 socket 服务端 客户端 阻塞io行为
    python 并发编程 IO模型介绍
  • 原文地址:https://www.cnblogs.com/CJLHY/p/11116659.html
Copyright © 2011-2022 走看看