SPOJTLE - Time Limit Exceeded(高维前缀和)
题意
题目的意思是给一个数组C,长度为n,每个数字的范围是2^m,然后要求构造一个数组a,满足
1、a[i] % C[i] !=0 ;
2、a[i] < 2^m ;
3、a[i] & a[i+1] = 0;
Sol
直接dp的话就是先枚举补集的子集,这样的复杂度是\(3^n\)的
然后补集的子集可以用高位前缀和优化一下
时间复杂度:\(O(2^n * n)\)
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN = 1e5 + 10, mod = 1000000000;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, M, a[MAXN], c[MAXN], f[51][65537], sum[51][65537];
int add(int &x, int y) {
x = (x + y >= mod ? x + y - mod : x + y);
}
int solve() {
N = read(); M = read(); int Lim = (1 << M) - 1;
memset(f, 0, sizeof(f));
memset(sum, 0, sizeof(sum));
f[0][0] = 1;
for(int i = 0; i <= Lim; i++) sum[0][i] = 1;
for(int i = 1; i <= N; i++) {
c[i] = read();
for(int sta = 1; sta <= Lim; sta ++) {
if(!(sta % c[i])) continue;
int s = (~sta) & Lim;
sum[i][sta] = f[i][sta] = sum[i - 1][s];
}
for(int j = 0; j < M; j++)//必须先枚举这个
for(int sta = 0; sta <= Lim; sta++)
if(sta & (1 << j)) add(sum[i][sta], sum[i][sta ^ (1 << j)]);
}
int ans = 0;
for(int i = 0; i <= Lim; i++) add(ans, f[N][i]);
return ans;
}
int main() {
int T = read();
while(T--) printf("%d\n", solve());
return 0;
}
SPOJTLE - Time Limit Exceeded(高维前缀和)的更多相关文章
- SPOJ.TLE - Time Limit Exceeded(DP 高维前缀和)
题目链接 \(Description\) 给定长为\(n\)的数组\(c_i\)和\(m\),求长为\(n\)的序列\(a_i\)个数,满足:\(c_i\not\mid a_i,\quad a_i\& ...
- SPOJ Time Limit Exceeded(高维前缀和)
[题目链接] http://www.spoj.com/problems/TLE/en/ [题目大意] 给出n个数字c,求非负整数序列a,满足a<2^m 并且有a[i]&a[i+1]=0, ...
- TLE - Time Limit Exceeded
TLE - Time Limit Exceeded no tags Given integers N (1 ≤ N ≤ 50) and M (1 ≤ M ≤ 15), compute the num ...
- java.lang.OutOfMemoryError:GC overhead limit exceeded填坑心得
我遇到这样的问题,本地部署时抛出异常java.lang.OutOfMemoryError:GC overhead limit exceeded导致服务起不来,查看日志发现加载了太多资源到内存,本地的性 ...
- Spark java.lang.outofmemoryerror gc overhead limit exceeded 与 spark OOM:java heap space 解决方法
引用自:http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece7631046893b4c4380146d96864968d4e414c42246 ...
- Unable to execute dex: GC overhead limit exceeded
Android打包时下面的错误: Unable to execute dex: GC overhead limit exceeded GC overhead limit exceeded 解决的方法: ...
- [转]java.lang.OutOfMemoryError:GC overhead limit exceeded
我遇到这样的问题,本地部署时抛出异常java.lang.OutOfMemoryError:GC overhead limit exceeded导致服务起不来,查看日志发现加载了太多资源到内存,本地的性 ...
- android Eclipse执行项目提示错误: unable to execute dex: GC orerhead limit exceeded
Eclipse执行项目提示错误: unable to execute dex: GC orerhead limit exceeded 解决方法: 找到Eclipse安装目录的文件,\eclipse\e ...
- android studio Error:java.lang.OutOfMemoryError: GC overhead limit exceeded
android studio Error:java.lang.OutOfMemoryError: GC overhead limit exceeded 在app下的build.gradle中找到and ...
随机推荐
- ajax异步请求302
我们知道,只有请求成功ajax才会进行回调处理,具体状态码为 status >= 200 && status < 300 || status === 304; 这一点通过查 ...
- java中集合的组成及特点
1:集合 Collection(单列集合) List(有序,可重复) ArrayList 底层数据结构是数组,查询快,增删慢 线程不安全,效率高 Vector 底层数据结构是数组,查询快,增删慢 线程 ...
- iframe传递参数问题
在页面中嵌入了iframe,如果需要传递参数到iframe中 1.通过将参数嵌入到url中,在iframe中使用${param.xxx}可以获取 2.通过将参数存入到session中,在iframe中 ...
- Intellij IDEA14 搜索框及控制台乱码解决
搜索ctrl+F及ctrl+H的搜索框.调试的时候控制台.导入module都显示为为中文乱码 如下: 解决方案: File->Setting->IDE Settings->Appea ...
- Node.js Error: listen EADDRNOTAVAIL
1 前言 nodejs部署在云服务器,外网用域名加端口访问不进来,但在服务器本地用127.0.0.1加端口可以访问,并且端口已经放开,然后只能排查配置.此文章仅作为记录使用. 如果端口和另一个的端口一 ...
- 如何利用github打造个人博客专属域名(文字版本)
1. 前言 此篇文章仅限于记录,不适合作为教程使用. 2. 步骤 2.1 先决条件 有github账号,有个人域名(可在万网购买),电脑本地安装有git环境 2.2 在github新建仓库.例如我的g ...
- Android开发之多Fragment切换优化
问题分析 一直在简书里看别人的技术贴,今天我也来写点自己的心得!最近在写一个项目用到大量的Fragment后的总结! 我想刚刚接触安卓的同学或许会这么写: FragmentManager fragme ...
- Android 自定义View二(深入了解自定义属性attrs.xml)
1.为什么要自定义属性 要使用属性,首先这个属性应该存在,所以如果我们要使用自己的属性,必须要先把他定义出来才能使用.但我们平时在写布局文件的时候好像没有自己定义属性,但我们照样可以用很多属性,这是为 ...
- sql in语句
转自http://www.1keydata.com/cn/sql/sql-in.php 在 SQL 中,在两个情况下会用到 IN 这个指令:这一页将介绍其中之一 -- 与 WHERE 有关的那一个情况 ...
- LoadRunner性能测试入门教程
javaweb性能测试那些事 一:什么是javaweb性能测试: 二:javaweb性能测试基本流程 三:javaweb性能测试常用指标: 1:响应时间:2-5-8 原则 2:吞吐量 3:资源使用率 ...