[codeforces327E]Axis Walking

time limit per test : 3 seconds
memory limit per test : 512 megabytes

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

Iahub has n positive integers a 1 , a 2 , . . . , a n a_1, a_2, ..., an a1,a2,...,an. The sum of those numbers is d d d. Suppose p 1 , p 2 , . . . , p n p_1, p_2, ..., p_n p1,p2,...,pn is a permutation of { 1 ,   2 ,   . . . ,   n 1, 2, ..., n 1,2,...,n}. Then, let b 1 b1 b1 =  a p 1 a_{p1} ap1, b 2 b_2 b2 =  a p 2 a_{p2} ap2 and so on. The array b is called a “route”. There are n! different routes, one for each permutation p p p.

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

Iahub is very superstitious and has k k 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 k k numbers. For his own curiosity, answer how many good routes he can make, modulo 1000000007 ( 1 0 9   +   7 ) 1000000007 (10^9 + 7) 1000000007(109+7).

Input

The first line contains an integer n ( 1   ≤   n   ≤   24 ) n (1 ≤ n ≤ 24) n(1n24). The following line contains n n n integers: a 1 ,   a 2 ,   . . . ,   a n ( 1   ≤   a i   ≤   1 0 9 ) a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9) a1,a2,...,an(1ai109).

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

Output

Output a single integer — the answer of Iahub’s dilemma modulo 1000000007 ( 1 0 9   +   7 ) 1000000007 (10^9 + 7) 1000000007(109+7).

Examples
Input

3
2 3 5
2
5 7

Output

1

Input

3
2 2 2
2
1 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.

题意:
对于一个序列{ a i a_i ai},他的一个排列{ b i b_i bi}被称为幸运的,则 b i b_i bi的前缀和{ s i s_i si} ( 1 &lt; = i &lt; = n ) (1&lt;=i&lt;=n) (1<=i<=n)中有没有一个非法元素。给出数列{ a i a_i ai}和 k k k个非法元素,询问有多少个幸运的序列。
题解:
这题有点神仙,本来写的折半搜索,结果发现元素个数被卡了…后面写了个状压dp反而过了,就是卡时,要注意减少取模,否则会T。

	if(S&(1<<(i-1))){
		f[S] += f[S - (1<<(i-1))];
		sum += a[i];
	}
	if(sum is illegal){ // 如果sum是非法的,则这个状态直接记录为0
		f[S] = 0;
	}
#include<bits/stdc++.h>
#define ll long long
#define LiangJiaJun main
#define MOD 1000000007LL
using namespace std;
int n,k;
ll ans,sum,a[34],p[4];
ll f[1<<24];

int LiangJiaJun(){
    scanf("%d",&n);
    sum=0;
    for(int i=1;i<=n;i++){
        scanf("%lld",&a[i]);
        sum+=a[i];
    }
    scanf("%d",&k);
    for(int i=1;i<=k;i++)scanf("%lld",&p[i]);
    sort(p+1,p+k+1);
    while(k>0&&p[k]>sum)k--;
    f[0]=1;
    for(int S=1;S<(1<<n);S++){
        sum=0;
        for(int i=1;i<=n;i++){
            if(S&(1<<(i-1))){
                sum+=a[i];
                f[S]+=f[S-(1<<(i-1))];
            }
        }
        for(int i=1;i<=k;i++){
            if(sum==p[i])f[S]=0;
        }
        f[S]%=MOD;
    }
    printf("%lld\n",f[(1<<n)-1]);
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值