[TC14860]SquadConstructor2

题目大意:

有\(n(n<2^m,m\le8)\)个互不相等的数\(v_i\)。从中选取\(k(k\le8)\)个数\(b_i\),求\(\sum_{i=0}^m(\sum_{j=1}^k[b_j\wedge 2^i=2^i])^2\)的最大值。

思路:

一个显然的动态规划\(f[i][s]\)表示选择了\(i\)个数,是否能使得状态为\(s\)。其中状态\(s\)表示\(0\sim m-1\)中每一个二进制位出现次数。显然可以做到\(\mathcal O(n\cdot k\cdot9^m)\)。使用bitset优化,做到\(\mathcal O(\frac{n\cdot k\cdot9^m}\omega)\)。

源代码:

#include<vector>
#include<bitset>
constexpr int M=8,N=1<<M,K=9;
constexpr int pwr[]={1,9,81,729,6561,59049,531441,4782969,43046721};
class SquadConstructor2 {
private:
int b[N];
std::bitset<pwr[M]> f[K];
int sqr(const int &x) const {
return x*x;
}
public:
int teamget(const int &m,const int &k,std::vector<int> v) {
const int n=v.size();
for(register int i=0;i<n;i++) {
for(register int j=0;j<m;j++) {
if(v[i]&(1<<j)) b[i]+=pwr[j];
}
}
f[0][0]=1;
for(register int i=0;i<n;i++) {
for(register int j=k;j;j--) {
f[j]|=f[j-1]<<b[i];
}
}
int ans=0;
for(register int s=0;s<pwr[m];s++) {
if(!f[k][s]) continue;
int tmp=0;
for(register int i=0;i<m;i++) {
tmp+=sqr(s/pwr[i]%9);
}
ans=std::max(ans,tmp);
}
return ans;
}
};

[TC14860]SquadConstructor2的更多相关文章

随机推荐

  1. IOException while loading persisted sessions:

    严重: IOException while loading persisted sessions: java.io.EOFException java.io.EOFException at java. ...

  2. JavaWeb使用Session防止表单重复提交

    在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没有响应,那么用户可能会以为是自己没有提交表单,就会再点击提交按钮重复提交表单,我们在开发中必须防止表单重复提交. 1.什么是表单 ...

  3. LCD实验学习笔记(七):NAND FLASH

    s3c2440 CPU内置NAND FLASH控制器.相关寄存大器起始地址为0x4e000000. 通过设置NFCONF寄存器,设置NAND FLASH 时序. 通过设置NFCONT寄存器,使能NAN ...

  4. 下载 LFS所需要的源码包的脚本程序及检验方法

    下载 LFS所需要的源码包的脚本程序及检验方法 http://blog.csdn.net/yygydjkthh/article/details/45315143

  5. C#中执行批处理文件(.bat),执行数据库相关操作

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. hadoop-Rpc使用实例

    代码:https://github.com/xufeng79x/hadoop-common-rpc-demo 1. 简介 hadoop中使用rpc机制来进行分布式进程间的通信,被封装进了hadoop- ...

  7. 理解rest架构

    越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这种"互联网软件"采用客户端/服务器模式,建立在分布式体系上,通过互联网通信,具有高延时(high latency).高 ...

  8. SQL 列 转换成 查询出来的 行

    查询  每个学生 的  (姓名,语文,数学,英语,成绩)为列 表结构如下: student: 学生表 grade 成绩表 : 查询出如下效果: SQL如下: select s.name,a.* fro ...

  9. leetcode 之Single Number(13)

    看见这题我的第一反应是用哈希来做,不过更简洁的做法是用异或来处理,只要是偶数个都为0(0和任意数异或仍为数本身). int singleNumber(int A[], int n) { ; ; i & ...

  10. 提高C#编程水平的50个要诀

    一篇旧时的文章,看后觉得还可以,特别贴出来. 提高C#编程水平的50个要点: .总是用属性 (Property) 来代替可访问的数据成员 .在 readonly 和 const 之间,优先使用 rea ...