【题目链接】 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(高维前缀和)的更多相关文章

  1. SPOJTLE - Time Limit Exceeded(高维前缀和)

    题意 题目链接 题目的意思是给一个数组C,长度为n,每个数字的范围是2^m,然后要求构造一个数组a,满足 1.a[i] % C[i] !=0 ; 2.a[i] < 2^m ; 3.a[i] &a ...

  2. SPOJ.TLE - Time Limit Exceeded(DP 高维前缀和)

    题目链接 \(Description\) 给定长为\(n\)的数组\(c_i\)和\(m\),求长为\(n\)的序列\(a_i\)个数,满足:\(c_i\not\mid a_i,\quad a_i\& ...

  3. TLE - Time Limit Exceeded

    TLE - Time Limit Exceeded no tags  Given integers N (1 ≤ N ≤ 50) and M (1 ≤ M ≤ 15), compute the num ...

  4. java.lang.OutOfMemoryError:GC overhead limit exceeded填坑心得

    我遇到这样的问题,本地部署时抛出异常java.lang.OutOfMemoryError:GC overhead limit exceeded导致服务起不来,查看日志发现加载了太多资源到内存,本地的性 ...

  5. Spark java.lang.outofmemoryerror gc overhead limit exceeded 与 spark OOM:java heap space 解决方法

    引用自:http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece7631046893b4c4380146d96864968d4e414c42246 ...

  6. Unable to execute dex: GC overhead limit exceeded

    Android打包时下面的错误: Unable to execute dex: GC overhead limit exceeded GC overhead limit exceeded 解决的方法: ...

  7. [转]java.lang.OutOfMemoryError:GC overhead limit exceeded

    我遇到这样的问题,本地部署时抛出异常java.lang.OutOfMemoryError:GC overhead limit exceeded导致服务起不来,查看日志发现加载了太多资源到内存,本地的性 ...

  8. android Eclipse执行项目提示错误: unable to execute dex: GC orerhead limit exceeded

    Eclipse执行项目提示错误: unable to execute dex: GC orerhead limit exceeded 解决方法: 找到Eclipse安装目录的文件,\eclipse\e ...

  9. android studio Error:java.lang.OutOfMemoryError: GC overhead limit exceeded

    android studio Error:java.lang.OutOfMemoryError: GC overhead limit exceeded 在app下的build.gradle中找到and ...

随机推荐

  1. 运输层和TCP/IP协议

    0. 基本要点 运输层是为相互通信的应用进程提供逻辑通信. 端口和套接字的意义 什么是无连接UDP 什么是面向连接的TCP 在不可靠的网络上实现可靠传输的工作原理,停止等待协议和ARQ协议 TCP的滑 ...

  2. android 图片透明

    在ImageButton中载入图片后,图片周围会存在一圈白边,会影响到美观,其实解决这个问题有两种方法 一种方法是将ImageButton的背景改为所需要的图片.如:android:backgroun ...

  3. [New learn]AutoLayout调查基于code

    代码https://github.com/xufeng79x/TestAutolayout-code2 0.插在前面 必须关闭view的自动缩放掩码,自动缩放掩码是autolayout出现之前系统管理 ...

  4. django “如何”系列8:如何为模型提供初始化数据

    当你第一次配置一个app的时候,有时候使用硬编码的数据去预填充你的数据库是非常有用的.这里有几个你可以让django自动创建这些数据的方法:你可以提供固定格式的初始化数据或者提供通过SQL初始化数据. ...

  5. 883H - Palindromic Cut(思维+STL)

    题目链接:http://codeforces.com/problemset/problem/883/H 题目大意:给一段长度为n的字符串s,想让你把s切成几段长度相同的回文串,可以改变s中字符的排列, ...

  6. LeetCode解题报告—— Regular Expression Matching

    Given an input string (s) and a pattern (p), implement regular expression matching with support for  ...

  7. Majority Element——算法课上的一道题(经典)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  8. [vim]使用中问题

    bug1: vim文档中文注释为乱码 step1: vim /var/lib/locales/supported.d/local 在其中添加下面的中文字符集 zh_CN.GBK GBK zh_CN.G ...

  9. Mysql用户管理(远程连接、授权)

    1.新建用户 登录MYSQL: @>mysql -u root -p @>密码 创建用户: mysql> insert into mysql.user(Host,User,Passw ...

  10. python webpy 框架环境架设

    前几年使用过 webpy做个些小东西,今天有个东西从拾webpy.但是基本上都忘记了,还是那句古话“好记性不如烂笔头”.这里把相应的步骤梳理下. 前提: 操作系统 windows 一.webpy 方面 ...