虽然不是正解但毕竟自己做出来了还是记下来吧~

对每个人分别dfs得到其期望,某两维的组合情况有限所以Hash一下避免了MLE。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std; const int maxn = 51, mod = 998244353;
int n, m, like[maxn], w[maxn];
int sum[2], cnt[2], val[maxn * maxn][maxn * maxn], dp[maxn][maxn << 1][5000][2];//in fact, 2500 is ok
unordered_map<long long, int> mp;
int hashcnt; int ksm(int a, int b) {
int res = 1;
for (; b; b >>= 1) {
if (b & 1) res = 1LL * res * a % mod;
a = 1LL * a * a % mod;
}
return res;
} int calc(int wi, int sum) {//wi / sum
if (val[wi][sum] != -1) return val[wi][sum];
return val[wi][sum] = 1LL * wi * ksm(sum, mod - 2) % mod;
} int Hash(int x, int y) {//[suma][sumb] is MLE, so hash it
long long t = 10000LL * x + y;
return mp.count(t) ? mp[t] : mp[t] = hashcnt++;
} int dfs(int depth, int wi, int suma, int sumb, int like) {
if (depth == m + 1) {
return wi;
}
int &x = dp[depth][wi][Hash(suma, sumb)][like];
if (x != -1) return x;
int add = like ? 1 : -1, ts = 0;
//select this
if (wi > 0) ts = (1LL * calc(wi, suma + sumb) * dfs(depth + 1, wi + add, suma + add, sumb, like) + ts) % mod;
//select one the same as it
if (suma > wi) ts = (1LL * calc(suma - wi, suma + sumb) * dfs(depth + 1, wi, suma + add, sumb, like) + ts) % mod;
//select one from opposite
if (sumb > 0) ts = (1LL * calc(sumb, suma + sumb) * dfs(depth + 1, wi, suma, sumb - add, like) + ts) % mod;
return x = ts;
} int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d", &like[i]);
cnt[like[i]]++;
}
for (int i = 0; i < n; i++) {
scanf("%d", &w[i]);
sum[like[i]] += w[i];//sum[0] sum[1]
}
memset(val, -1, sizeof val);
memset(dp, -1, sizeof dp);
for (int i = 0; i < n; i++) {
printf("%d\n", dfs(1, w[i], sum[like[i]], sum[like[i] ^ 1], like[i]));
}
}

Codeforces #564div2 E1(记忆化搜索)的更多相关文章

  1. CodeForces - 607B (记忆化搜索)

    传送门: http://codeforces.com/problemset/problem/607/B Genos recently installed the game Zuma on his ph ...

  2. CodeForces 173C Spiral Maximum 记忆化搜索 滚动数组优化

    Spiral Maximum 题目连接: http://codeforces.com/problemset/problem/173/C Description Let's consider a k × ...

  3. Codeforces Gym 100231G Voracious Steve 记忆化搜索

    Voracious Steve 题目连接: http://codeforces.com/gym/100231/attachments Description 有两个人在玩一个游戏 有一个盆子里面有n个 ...

  4. Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索

    D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...

  5. Educational Codeforces Round 1 E. Chocolate Bar 记忆化搜索

    E. Chocolate Bar Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/prob ...

  6. CodeForces 398B 概率DP 记忆化搜索

    题目:http://codeforces.com/contest/398/problem/B 有点似曾相识的感觉,记忆中上次那个跟这个相似的 我是用了 暴力搜索过掉的,今天这个肯定不行了,dp方程想了 ...

  7. Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索

    A. Berzerk 题目连接: http://codeforces.com/contest/786/problem/A Description Rick and Morty are playing ...

  8. Codeforces Round #427 (Div. 2) Problem D Palindromic characteristics (Codeforces 835D) - 记忆化搜索

    Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th nu ...

  9. Codeforces 148D Bag of mice:概率dp 记忆化搜索

    题目链接:http://codeforces.com/problemset/problem/148/D 题意: 一个袋子中有w只白老鼠,b只黑老鼠. 公主和龙轮流从袋子里随机抓一只老鼠出来,不放回,公 ...

  10. codeforces 284 D. Cow Program(记忆化搜索)

    题目链接:http://codeforces.com/contest/284/problem/D 题意:给出n个数,奇数次操作x,y都加上a[x],偶数次操作y加上a[x],x减去a[x],走出了范围 ...

随机推荐

  1. RQNOJ 514 字串距离:dp & 字符串

    题目链接:https://www.rqnoj.cn/problem/514 题意: 设有字符串X,我们称在X的头尾及中间插入任意多个空格后构成的新字符串为X的扩展串,如字符串X为”abcbcd”,则字 ...

  2. tensorflow 线性回归 iris

    线性拟合

  3. Java企业微信开发_10_未验证域名归属,JS-SDK功能受限

    1.现象: 在企业微信后台填写可信域名后,提示:未验证域名归属,JS-SDK功能受限,如下图: 点击“申请域名校验”后, 注意:域名根目录 当时一直不清楚这个域名根目录在哪里,最后让我给试出来了 2. ...

  4. listen 57

    Secondhand Smoke Exposure Doubled Asthmatic Kids' Hospital Readmissions If your child has asthma哮喘, ...

  5. find命令的基础用法以及按文件修改时间查找文件

    一般文件查找方法: find 命令学好是一件很有趣的事情,也可以帮你在查找系统文件的时候事倍功半,还可以与正则表达式结合使用,功能强大,是一个很好的查找工具.可以整体提高你的系统管理能力. 基础用法 ...

  6. 局域网扫描IP

    今天有朋友去面试,被问到一个“如何扫描局域网IP”的问题(即找出局域网中当前已使用的IP),朋友回答的不好,回来问我,我首先想到的就是使用ping命令将局域网可分配的IP地址逐个遍历一遍,能ping通 ...

  7. darknet YOLOv2安装及数据集训练

    一. YOLOv2安装使用 1. darknet YOLOv2安装 git clone https://github.com/pjreddie/darknetcd darknetmake或到网址上下载 ...

  8. [转]BX9054: 各浏览器对 document.execCommand 方法的首参数可选值范围存在差异

    作者:钱宝坤 标准参考 无. 问题描述 execCommand 方法通常用于控制可编辑的 IFRAME 内容,制作富文本编辑器. 但他现在为止还是非标准的,方法的首参数 Commmands 的可选值由 ...

  9. CF1076E:Vasya and a Tree

    浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://codeforces.com/problemset/prob ...

  10. pkg_resources----Entry Points为程序提供扩展点

    官方文档对Entry Points的介绍 Entry Points Entry points are a simple way for distributions to "advertise ...