博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF 327E(Axis Walking-状态压缩Dp-lowbit的使用)
阅读量:4318 次
发布时间:2019-06-06

本文共 3104 字,大约阅读时间需要 10 分钟。

 

E. Axis Walking
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Iahub wants to meet his girlfriend Iahubina. They both live in Ox axis (the horizontal axis). Iahub lives at point 0 and Iahubina at point d.

Iahub has n positive integers a1a2, ..., an. The sum of those numbers is d. Suppose p1p2, ..., pn is a permutation of {1, 2, ..., n}. Then, let b1 = ap1b2 = ap2 and so on. The array b is called a "route". There are n! different routes, one for each permutation p.

Iahub's travel schedule is: he walks b1 steps on Ox axis, then he makes a break in point b1. Then, he walks b2 more steps on Ox axis and makes a break in point b1 + b2. Similarly, at j-th (1 ≤ jn) time he walks bj more steps on Ox axis and makes a break in point b1 + b2 + ... + bj.

Iahub is very superstitious and has k integers which give him bad luck. He calls a route "good" if he never makes a break in a point corresponding to one of those k numbers. For his own curiosity, answer how many good routes he can make, modulo 1000000007(109 + 7).

Input

The first line contains an integer n (1 ≤ n ≤ 24). The following line contains n integers: a1, a2, ..., an (1 ≤ ai ≤ 109).

The third line contains integer k (0 ≤ k ≤ 2). The fourth line contains k positive integers, representing the numbers that give Iahub bad luck. Each of these numbers does not exceed 109.

Output

Output a single integer — the answer of Iahub's dilemma modulo 1000000007 (109 + 7).

Sample test(s)
input
32 3 525 7
output
1
input
32 2 221 3
output
6
Note

In the first case consider six possible orderings:

 

  • [2, 3, 5]. Iahub will stop at position 2, 5 and 10. Among them, 5 is bad luck for him.
  • [2, 5, 3]. Iahub will stop at position 2, 7 and 10. Among them, 7 is bad luck for him.
  • [3, 2, 5]. He will stop at the unlucky 5.
  • [3, 5, 2]. This is a valid ordering.
  • [5, 2, 3]. He got unlucky twice (5 and 7).
  • [5, 3, 2]. Iahub would reject, as it sends him to position 5.

 

In the second case, note that it is possible that two different ways have the identical set of stopping. In fact, all six possible ways have the same stops: [2, 4, 6], so there's no bad luck for Iahub.

状态压缩DP

 

考试时由于24*2^24 复杂度太高没写

结果答案居然就是这样

不过枚举时要直接用lowbit(i),返回min(2^k) (i)2  第k位为1

大家直接看答案就行了

忽然发现几乎没做过状压DP的题

 

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Rep(i,n) for(int i=0;i
=0;i--)#define MEM(a) memset(a,0,sizeof(a))#define MEMI(a) memset(a,127,sizeof(a))#define MEMi(a) memset(a,128,sizeof(a))#define INF (2139062143)#define F (1000000007)#define MAXN (1<<24)#define MAXItem (24+10)typedef long long ll;int n,k;ll f[MAXN]={0},g[MAXN]={0},A[MAXN]={0},a[MAXItem]={0},b[MAXItem]={0};int main(){// freopen("CF327E.in","r",stdin); scanf("%d",&n); For(i,n) scanf("%d",&a[i]),A[1<
>k; For(i,k) scanf("%d",&b[i]);Fork(i,k+1,2) b[i]=-1; //除了x=-1,x^-1!=0 Rep(i,1<

 

 

转载于:https://www.cnblogs.com/jiangu66/p/3174464.html

你可能感兴趣的文章
刚接触Vuex
查看>>
四种加载React数据的技术对比(Meteor 转)
查看>>
Airthmetic_Approching
查看>>
操作文本文件
查看>>
公司项目的几个问题
查看>>
解决win7下打开Excel2007,报“向程序发送命令时出现问题”的错误
查看>>
Velocity快速入门教程
查看>>
关于集合常见的问题
查看>>
车牌正则表达式
查看>>
Win form碎知识点
查看>>
避免使用不必要的浮动
查看>>
第一节:ASP.NET开发环境配置
查看>>
sqlserver database常用命令
查看>>
rsync远程同步的基本配置与使用
查看>>
第二天作业
查看>>
访问属性和访问实例变量的区别
查看>>
Spring MVC 异常处理 - SimpleMappingExceptionResolver
查看>>
props 父组件给子组件传递参数
查看>>
【loj6038】「雅礼集训 2017 Day5」远行 树的直径+并查集+LCT
查看>>
十二种获取Spring的上下文环境ApplicationContext的方法
查看>>