【LOJ】#6433. 「PKUSC2018」最大前缀和
题解
神仙的状压啊QAQ
设一个\(f[S]\)表示数字的集合为\(S\)时\(sum[S]\)为前缀最大值的方案数
\(g[S]\)表示数字集合为\(S\)时所有前缀和都小于等于0的方案数
答案就是\(sum_{S} sum[S] * f[S] * g[2^{N} - 1 - S]\)
求\(f\)每次相当于往前面插入一个数,如果\(sum[S] > 0\)就更新
\(f[S \^ (1 << i - 1)] += f[S] (sum[S] > 0)\)
求\(g\)只要每次看看更新的集合总和是不是合法就行了
代码
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define pdi pair<db,int>
#define mp make_pair
#define pb push_back
#define enter putchar('\n')
#define space putchar(' ')
#define eps 1e-8
#define mo 974711
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
res = 0;char c = getchar();T f = 1;
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
const int MOD = 998244353;
int N;
int A[25],sum[(1 << 20) + 5],pos[(1 << 20) + 5],f[(1 << 20) + 5],g[(1 << 20) + 5],ans;
int inc(int a,int b) {
return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
return 1LL * a * b % MOD;
}
int lowbit(int x) {
return x & (-x);
}
void update(int &x,int y) {
x = inc(x,y);
}
void Solve() {
read(N);
for(int i = 1 ; i <= N ; ++i) read(A[i]);
for(int i = 0 ; i < N ; ++i) pos[1 << i] = i + 1;
for(int S = 1 ; S < (1 << N) ; ++S) {
sum[S] = sum[S ^ lowbit(S)] + A[pos[lowbit(S)]];
}
g[0] = 1;
for(int i = 1 ; i <= N ; ++i) {
f[1 << i - 1] = 1;
}
for(int S = 1 ; S < (1 << N) ; ++S) {
if(sum[S] > 0) {
for(int i = 1 ; i <= N ; ++i) {
if(!(S >> (i - 1) & 1)) {
update(f[S ^ (1 << i - 1)],f[S]);
}
}
}
else {
for(int i = 1 ; i <= N ; ++i) {
if(S >> (i - 1) & 1) {
update(g[S],g[S ^ (1 << i - 1)]);
}
}
}
}
for(int S = 1 ; S < (1 << N) ; ++S) {
int t = mul(f[S],g[(1 << N) - 1 - S]);
t = mul(t,inc(MOD,sum[S]));
update(ans,t);
}
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
【LOJ】#6433. 「PKUSC2018」最大前缀和的更多相关文章
- LOJ 6433 「PKUSC2018」最大前缀和——状压DP
题目:https://loj.ac/problem/6433 想到一个方案中没有被选的后缀满足 “该后缀的任一前缀和 <=0 ”. 于是令 dp[ S ] 表示选了点集 S ,满足任一前缀和 & ...
- Loj#6433「PKUSC2018」最大前缀和(状态压缩DP)
题面 Loj 题解 先转化题意,其实这题在乘了\(n!\)以后就变成了全排列中的最大前缀和的和(有点拗口).\(n\leq20\),考虑状压\(DP\) 考虑一个最大前缀和\(\sum\limits_ ...
- Loj 6433. 「PKUSC2018」最大前缀和 (状压dp)
题面 Loj 题解 感觉挺难的啊- 状压\(dp\) 首先,有一个性质 对于一个序列的最大前缀和\(\sum_{i=1}^{p} A[i]\) 显然对于每个\(\sum_{i=p+1}^{x}A[i] ...
- LOJ#6433. 「PKUSC2018」最大前缀和 状压dp
原文链接https://www.cnblogs.com/zhouzhendong/p/LOJ6433.html 题解 枚举一个集合 S ,表示最大前缀和中包含的元素集为 S ,然后求出有多少个排列是这 ...
- loj 6433 「PKUSC2018」最大前缀和 题解【DP】【枚举】【二进制】【排列组合】
这是个什么集合DP啊- 想过枚举断点但是不会处理接下来的问题了- 我好菜啊 题目描述 小 C 是一个算法竞赛爱好者,有一天小 C 遇到了一个非常难的问题:求一个序列的最大子段和. 但是小 C 并不会做 ...
- loj#6433. 「PKUSC2018」最大前缀和(状压dp)
传送门 今天\(PKUWC\)试机的题 看着边上的大佬们一个个\(A\)穿咱还是不会-- 我们考虑枚举最大前缀和,如果一个前缀\(1\)到\(p\)是最大前缀和,那么\(p\)后面的所有前缀和都要小于 ...
- [LOJ #6433]「PKUSC2018」最大前缀和
题目大意:给你一个$n(n\leqslant20)$项的数列$A$,设重排后的数列为$A'$,令$pre_p=\sum\limits_{i=1}^pA'_i$,求$max\{pre_i\}$的期望,乘 ...
- LOJ #6436. 「PKUSC2018」神仙的游戏(字符串+NTT)
题面 LOJ #6436. 「PKUSC2018」神仙的游戏 题解 参考 yyb 的口中的长郡最强选手 租酥雨大佬的博客 ... 一开始以为 通配符匹配 就是类似于 BZOJ 4259: 残缺的字符串 ...
- LOJ #6435. 「PKUSC2018」星际穿越(倍增)
题面 LOJ#6435. 「PKUSC2018」星际穿越 题解 参考了 这位大佬的博客 这道题好恶心啊qwq~~ 首先一定要认真阅读题目 !! 注意 \(l_i<r_i<x_i\) 这个条 ...
随机推荐
- 数位DP学习笔记
数位DP学习笔记 什么是数位DP? 数位DP比较经典的题目是在数字Li和Ri之间求有多少个满足X性质的数,显然对于所有的题目都可以这样得到一些暴力的分数 我们称之为朴素算法: for(int i=l_ ...
- luogu1083 [NOIp2012]借教室 (二分答案+差分)
先二分一个答案x,然后通过差分来看有没有不满足的 #include<bits/stdc++.h> #define pa pair<int,int> #define lowb(x ...
- maven的三种工程pom、jar、war的区别
转: maven的三种工程pom.jar.war的区别 2018年09月03日 10:02:53 houjx3 阅读数:2918更多 个人分类: maven 1.pom工程:用在父级工程或聚合工程 ...
- Eclipse 报 "The builder launch configuration could not be found" 错误
Eclipse 报 "The builder launch configuration could not be found" 错误的解决办法 标签: eclipseEclipse ...
- ASP.NET Web API queryString访问的一点总结
自从开始使用ASP.NET Web API,各种路由的蛋疼问题一直困扰着我,相信大家也都一样. Web API的路由配置与ASP.MVC类似,在App_Start文件夹下,有一个WebApiConfi ...
- Tornado实现多线程、多进程HTTP服务
背景 线上有一个相关百科的服务,返回一个query中提及的百科词条.该服务是用python实现的,以前通过thrift接口访问,现要将其改为通过HTTP访问.之前没有搭建HTTPServer的经验,因 ...
- div中添加滚动条
<div style="position:absolute; height:400px; overflow:auto"></div>div 设置滚动条显示: ...
- CodeForces 1059B
Description Student Andrey has been skipping physical education lessons for the whole term, and now ...
- Servlet笔记11--补充
Servlet线程安全问题: 代码示例: package com.bjpowernode.javaweb.servlet; import java.io.IOException; import jav ...
- Python学习笔记之逻辑回归
# -*- coding: utf-8 -*- """ Created on Wed Apr 22 17:39:19 2015 @author: 90Zeng " ...