SPOJ Time Limit Exceeded(高维前缀和)
【题目链接】 http://www.spoj.com/problems/TLE/en/
【题目大意】
给出n个数字c,求非负整数序列a,满足a<2^m
并且有a[i]&a[i+1]=0,对于每个a[i],要保证a[i]不是c[i]的倍数,
求这样的a[i]序列的个数
【题解】
我们用dp[i]表示以i为结尾的方案数,
我们发现要满足a[i]&a[i+1]=0,则dp[i]是从上一次结果中所有满足i&j=0的地方转移过来的
i&j=0即i&(~j)=i,即i为~j的子集,那么我们每次对上一次的结果进行下标取反操作,
那么求当前dp[i],就是求出以i为子集的上一次计算出的dp值的高维前缀和。
对于c[i]这个条件,我们每轮计算后将c[i]倍数为下标的dp值置0即可。
【代码】
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int T,n,m,c[100];
const int mod=1000000000;
struct data{
int val;
data operator +(const data &rhs)const{
int t_val=val+rhs.val;
if(t_val>=mod)t_val-=mod;
if(t_val<0)t_val+=mod;
return data{t_val};
}
}dp[(1<<15)+10],res;
int main(){
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",&c[i]);
int all=1<<m;
for(int j=0;j<all;j++)dp[j].val=(j==0);
for(int i=1;i<=n;i++){
for(int j=0;j<all;j+=2)swap(dp[j],dp[j^(all-1)]);
for(int j=0;j<m;j++)for(int k=0;k<all;k++){
if(~k&(1<<j))dp[k]=dp[k]+dp[k|(1<<j)];
}for(int j=0;j<all;j+=c[i])dp[j].val=0;
}res.val=0;
for(int j=0;j<all;j++)res=res+dp[j];
printf("%d\n",res.val);
}return 0;
}
SPOJ Time Limit Exceeded(高维前缀和)的更多相关文章
- SPOJTLE - Time Limit Exceeded(高维前缀和)
题意 题目链接 题目的意思是给一个数组C,长度为n,每个数字的范围是2^m,然后要求构造一个数组a,满足 1.a[i] % C[i] !=0 ; 2.a[i] < 2^m ; 3.a[i] &a ...
- SPOJ.TLE - Time Limit Exceeded(DP 高维前缀和)
题目链接 \(Description\) 给定长为\(n\)的数组\(c_i\)和\(m\),求长为\(n\)的序列\(a_i\)个数,满足:\(c_i\not\mid a_i,\quad a_i\& ...
- 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 ...
随机推荐
- DotNETCore 学习笔记 MVC视图
Razor Syntax Reference Implicit Razor expressions <p>@DateTime.Now</p> <p>@DateTim ...
- 2017ACM暑期多校联合训练 - Team 1 1002 HDU 6034 Balala Power! (字符串处理)
题目链接 Problem Description Talented Mr.Tang has n strings consisting of only lower case characters. He ...
- @EnableEurekaClient源码分析
@EnableEurekaClient注解,从源码的角度分析是如何work的 NetFlix Eureka client Eureka client 负责与Eureka Server 配合向外提供注册 ...
- python进行机器学习(二)之特征选择
毫无疑问,解决一个问题最重要的是恰当选取特征.甚至创造特征的能力,这叫做特征选取和特征工程.对于特征选取工作,我个人认为分为两个方面: 1)利用python中已有的算法进行特征选取. 2)人为分析各个 ...
- Java线程(一)
1. java什么叫线程安全?什么叫不安全? 就是线程同步的意思,就是当一个程序对一个线程安全的方法或者语句进行访问的时候,其他的不能再对他进行操作了,必须等到这次访问结束以后才能对这个线程安全的方法 ...
- Ubuntu安装pip
首先打开终端 在终端输入:sudo apt-get install python-pip python-dev build-essential [+] 如果需要在Python3下安装pip,那么在py ...
- Django之项目搭建和配置总结(一)
安装和创建虚拟环境 参考:linux系统下Python虚拟环境的安装和使用 安装Django包 先进入虚拟环境,在联网下执行: pip install django==1.8.7 1.8.7表示dja ...
- MVC 从控制器将数据对象赋值给前端JS对象
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...
- 将MongoDB安装成为Windows服务
使用以下命令将MongoDB安装成为Windows服务.笔者的MongoDB目录为D:\Program Files\mongodb 切换到D:\Program Files\mongodb\bin> ...
- Python初学--字符串
ASCII.Unicode和UTF-8的关系 在计算机内存中,统一使用Unicode编码,当需要保存到硬盘或者需要传输的时候,就转换为UTF-8编码 记事本编辑的时候,从文件读取的UTF-8字符被转换 ...