【题目链接】 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. 教你 Shiro 整合 SpringBoot,避开各种坑(山东数漫江湖)

    依赖包 <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-sprin ...

  2. nyoj 15 括号匹配(二) (经典dp)

    题目链接 描述 给你一个字符串,里面只包含"(",")","[","]"四种符号,请问你需要至少添加多少个括号才能使这些 ...

  3. js_面向对象设计和行为委托设计模式

    最近换了新工作,面试过程中有多多少少的问题没有给的出答案,为自己的技术短板而促急. javascript中万物皆对象(键:值构成的一种数据),暂且不讨论这个句话的对与错,可以想象对象在javascri ...

  4. hadoop2.4.1伪分布式环境搭建

    注意:所有的安装用普通哟用户安装,所以首先使普通用户可以以sudo执行一些命令: 0.虚拟机中前期的网络配置参考: http://www.cnblogs.com/qlqwjy/p/7783253.ht ...

  5. imx6设备树pinctrl解析【转】

    转自:http://blog.csdn.net/michaelcao1980/article/details/50730421 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近在移植linu ...

  6. ASPxgridview 编辑列初始化事件

    在初始化编辑咧的时候,给其赋值或者是disable等等.... 贴上代码 protected void master_CellEditorInitialize(object sender, ASPxG ...

  7. [linux]通过ssh远程设定各服务器时间,从而实现集群时间同步

    #!/usr/bin/env bash #all hosts should to sync time, all hosts should no password login echo other sy ...

  8. Microsoft .NET Native

    首页: https://msdn.microsoft.com/en-US/vstudio/dotnetnative

  9. ArcGIS Server配置端口

    写在前面,GIS服务器必须连通到外网,基于某些情况,可能一个机组有多态服务器,担任不同的角色,有Web服务器.数据库服务器和GIS服务器等,但是可能购买时只有一个外网IP,这样是不行的.JS脚本运行在 ...

  10. inux权限管理(1)

    1.linux系统文件普通权限 2.文件所属主的设置,组的指定 3.特殊权限 4.acl权限 5.su命令及其注意事项和sudo权限 6.权限管理的注意点 0.首先,在linux下用户账户是分角色的, ...