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 ...
随机推荐
- CSS3动画常用demo
1.border动画 2.闪动动画(一闪一闪亮晶晶,满天都是小星星) .blink { animation: mymove 0.8s infinite; -webkit-animation: mymo ...
- 【vim】自动补全 Ctrl+n
Vim 默认有自动补全的功能.的确这个功能是很基本的,并且可以通过插件来增强,但它也很有帮助.方法很简单. Vim 尝试通过已经输入的单词来预测单词的结尾. 比如当你在同一个文件中第二次输入 &quo ...
- 调用链系列三、基于zipkin调用链封装starter实现springmvc、dubbo、restTemplate等实现全链路跟踪
一.实现思路 1.过滤器实现思路 所有调用链数据都通过过滤器实现埋点并收集.同一条链共享一个traceId.每个节点有唯一的spanId. 2.共享传递方式 1.rpc调用:通过隐式传参.dubbo有 ...
- 默认以管理员身份运行VS2013/15/17
方法如下: 1.右击VS的快捷方式,选择[属性],打开属性对话框,再点击[高级]按钮,如下图所示: 2.再勾选[用管理员身份运行],点击[确定]即可: 然后就可以双击VS快捷方式,直接以管理员身份运行 ...
- C++:vector中的v.at(0)和v[0]的区别
设v是一个vector的对象, 如果v是非空的,则v.at(0)和v[0]是没有区别的,都是取数组中第一个值: 如果v是空的,则v.at(0)会抛出异常(exception std::out_of_r ...
- python操作mysql数据库的常用方法使用详解
python操作mysql数据库 1.环境准备: Linux 安装mysql: apt-get install mysql-server 安装python-mysql模块:apt-get instal ...
- 彻底理解this指向-----实例分析
this的指向在函数创建的时候是决定不了的,在调用的时候才能决定,谁调用的就指向谁,一定要搞清楚这个. 情况1:如果一个函数中有this,但是它没有被上一级的对象所调用,那么this指向的就是wind ...
- 使用fidder进行接口测试
官方下载地址 下载后一路next安装即可. get请求 get请求直接把需要携带的参数使用?跟在后面如:http://127.0.0.1:8000/api/get_event_list/?id=1 p ...
- MVC中页面的传值方式总结
MVC中的页面传值,通常指Controller和view之间的数据传递,经常用到的有几种方式,总结如下: 一:ViewData 获取或设置一个字典,其中包含在控制器和视图之间传递的数据.使用ViewD ...
- python全栈开发day15-递归函数、二分查找
1.昨日内容回顾 主要内置函数: map(func,iter1):返回迭代器 filter(func,iter1):返回迭代器 sorted(iter,key=,reverse=):返回列表 reve ...