Codeforces #564div2 E1(记忆化搜索)
虽然不是正解但毕竟自己做出来了还是记下来吧~
对每个人分别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(记忆化搜索)的更多相关文章
- CodeForces - 607B (记忆化搜索)
传送门: http://codeforces.com/problemset/problem/607/B Genos recently installed the game Zuma on his ph ...
- CodeForces 173C Spiral Maximum 记忆化搜索 滚动数组优化
Spiral Maximum 题目连接: http://codeforces.com/problemset/problem/173/C Description Let's consider a k × ...
- Codeforces Gym 100231G Voracious Steve 记忆化搜索
Voracious Steve 题目连接: http://codeforces.com/gym/100231/attachments Description 有两个人在玩一个游戏 有一个盆子里面有n个 ...
- Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索
D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...
- Educational Codeforces Round 1 E. Chocolate Bar 记忆化搜索
E. Chocolate Bar Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/prob ...
- CodeForces 398B 概率DP 记忆化搜索
题目:http://codeforces.com/contest/398/problem/B 有点似曾相识的感觉,记忆中上次那个跟这个相似的 我是用了 暴力搜索过掉的,今天这个肯定不行了,dp方程想了 ...
- Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索
A. Berzerk 题目连接: http://codeforces.com/contest/786/problem/A Description Rick and Morty are playing ...
- 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 ...
- Codeforces 148D Bag of mice:概率dp 记忆化搜索
题目链接:http://codeforces.com/problemset/problem/148/D 题意: 一个袋子中有w只白老鼠,b只黑老鼠. 公主和龙轮流从袋子里随机抓一只老鼠出来,不放回,公 ...
- codeforces 284 D. Cow Program(记忆化搜索)
题目链接:http://codeforces.com/contest/284/problem/D 题意:给出n个数,奇数次操作x,y都加上a[x],偶数次操作y加上a[x],x减去a[x],走出了范围 ...
随机推荐
- python的easygui
1.利用msgbox(单词messagebox的缩写)给出一个提示信息: import easygui as g reply=g.msgbox('This is a basic message box ...
- 简单的可兼容所有浏览器的操作html元素的javascript框架
1.根据id名称取元素 $id(idName) 2.根据class定义取元素 $class(className)返回所有class被定义成className的元素数组,或者$Eclass(clas ...
- 时尚创意VI矢量设计模板
时尚创意VI矢量设计模板 创意VI VI设计 企业VI 时尚背景 信封设计 封面设计 杯子 桌旗 帽子 EPS矢量素材下载 http://www.huiyi8.com/vi/
- legend2---开发日志15(功能需求明确,设计好类和结构的好处是)
legend2---开发日志15(功能需求明确,设计好类和结构的好处是) 一.总结 一句话总结: 极快简化编程,节约大量时间 1.多个类型的物品,比如商店和寻宝的丹药,装备,特性书,英雄石等等 应该怎 ...
- swoole+nginx反向代理
nginx配置: server { listen 80; server_name www.swoole.com; root /data/wwwroot/www.swoole.com; location ...
- hdu-5784 How Many Triangles(计算几何+极角排序)
题目链接: How Many Triangles Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- OpenCV——花环生成函数
// define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...
- HihoCoder1641 : 热门号码([Offer收割]编程练习赛37)(模拟)
描述 1 2 3 ABC DEF 4 5 6 GHI JKL MNO 7 8 9 PQRS TUV WXYZ * 0 # 我们知道电话拨号盘上数字会有若干字母对应,例如2对应ABC,7对应PQRS. ...
- MySQL活动期间制定月份注册用户下单情况_20161029
在10.29到10.31号期间 10月新注册的用户订单金额满600元赠与优惠券 #3天内订单满600元且10月注册的用户订单明细 SELECT a.城市,a.用户ID,b.用户名称,DATE(b.注册 ...
- 几个网络模型的示例代码(BlockingModel、OverlappedModel、WSAEventSelect、CompletionRoutine)..c++
作者的blog:猪)的网络编程世界 几个网络模型的示例代码代码包括了下面几个模型的示例:BlockingModel(阻塞模式).OverlappedModel(基于事件的重叠I/O).WSAEvent ...