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 ...
随机推荐
- 【Linux】Linux下统计当前文件夹下的文件个数、目录个数
统计当前文件夹下文件的个数,包括子文件夹里的 ls -lR|grep "^-"|wc -l 统计文件夹下目录的个数,包括子文件夹里的 ls -lR|grep "^d&qu ...
- Java中的BlockingQueue队列
BlockingQueue位于JDK5新增的concurrent包中,它很好地解决了多线程中,如何高效安全地“传输”数据的问题.通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极 ...
- Oracle常用sql语句(一)
# Sql的分类 # DDL (Data Definition Language):数据定义语言,用来定义数据库对象:库.表.列等: CREATE. ALTER.DROP DML(Data Manip ...
- Mac配置Java开发环境
笔者从Window上转到Mac上做开发,一切配置都要重新开始,开发环境配置介绍如下: 1. 下载JDK 从下面链接选择合适版本的安装包进行下载...笔者下载的是jdk-9.0.1 链接:http:// ...
- python魔法函数(二)之__getitem__、__len__、__iter__
魔法函数会增强python类的类型,独立存在 __getitem class Company: def __init__(self, employees): self.employees = empl ...
- Fiddler抓包3-查看get与post请求
前言 前面两篇关于Fiddler抓包的一些基本配置,配置完之后就可以抓到我们想要的数据了,接下来就是如何去分析这些数据. 本篇以博客园的请求为例,简单分析get与post数据有何不一样,以后也能分辨出 ...
- JMeter 聚合报告 90%响应时间
90%的响应时间理解 官方解释:90% Line - 90% of the samples took no more than this time. The remaining samples at ...
- python 全栈开发,Day111(客户管理之 编辑权限(二),Django表单集合Formset,ORM之limit_choices_to,构造家族结构)
昨日内容回顾 1. 权限系统的流程? 2. 权限的表有几个? 3. 技术点 中间件 session orm - 去重 - 去空 inclusion_tag filter 有序字典 settings配置 ...
- canvas图像保存
很多时候绘制完成的图片需要保存,那么我们就可以使用到Canvas API来完成这最后一步! Canvas API使用toDataURL方法把绘画的状态输出到一个data URL中然后重新装载,然后我们 ...
- Windows任务计划 & Linux crontab定时自动任务
如何在windows服务器上面创建定时任务https://blog.csdn.net/shiyong1949/article/details/52779359 Windows 10系统下如何设置计划任 ...