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

对每个人分别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. 分享知识-快乐自己:全面解析 java注解实战指南

    请你在看这篇文章时,不要感到枯燥,从头到尾一行行看,代码一行行读,你一定会有所收获的. 问: 为什么学习注解? 学习注解有什么好处? 学完能做什么? 答: 1):能够读懂别人的代码,特别是框架相关的代 ...

  2. python-常用内置函数与装饰器

    1.常用的python函数 abs             求绝对值 all               判断迭代器中所有的数据是否为真或者可迭代数据为空,返回真,否则返回假 any          ...

  3. elasticsearch ——id字段说明,内部是_uid

    _id field Each document indexed is associated with a _type (see the section called “Mapping Typesedi ...

  4. python基础-大杂烩

    random()随机函数 import random print(random.choice('abcdefghij')) #随机取这些字母 print(random.choice(['apple', ...

  5. selenium 经常用到的API

    一.webdriver 属性及方法: 1.获取当前页面的 url driver.current_url 2 .获取窗口相关信息 get_window_position() 返回窗口x,y坐标 get_ ...

  6. [acm]HDOJ 2059 龟兔赛跑

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=2059 起点和终点,共n+2个点,n+2个状态,简单DP即可. //11512698 2014-08- ...

  7. HihoCoder1651 : 小球染色([Offer收割]编程练习赛38)(DP的优化)

    描述 小Ho面前有N个小球排成了一排.每个小球可以被染成M种颜色之一. 为了增强视觉效果,小Ho希望不存在连续K个或者K个以上的小球颜色一样. 你能帮小Ho计算出一共有多少种不同的染色方法么? 例如N ...

  8. ACM学习历程—NPU1086 随机数 2015年陕西省程序设计竞赛网络预赛(正式赛)C题 (计数排序 || set容器)

    Description 开学了,ACM队的边老板想在学校中请一些妹子一起做一项问卷调查,调查妹子们对ACM的了解情况,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤100), ...

  9. 【Lintcode】076.Longest Increasing Subsequence

    题目: Given a sequence of integers, find the longest increasing subsequence (LIS). You code should ret ...

  10. Python3解leetcode Maximum Subarray

    问题描述: Given an integer array nums, find the contiguous subarray (containing at least one number) whi ...