Codeforces 327E Axis Walking (狀壓dp lowbit優化)

_TCgogogo_發表於2015-07-31

E. 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 and Iahubina at point d.
Iahub has n positive integers a1, a2, ..., an. The sum of those numbers is d. Suppose p1, p2, ..., pn is a permutation of {1, 2, ..., n}. Then, let b1 = ap1, b2 = 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 ≤ j ≤ n) 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

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.


題目連結:http://codeforces.com/contest/327/problem/E


題目大意:給一個序列,可以任意重排,但是字首和不能出現給定數字中的數,問有幾種排列方式


題目分析:n小於等於24,畢竟是cf,跑的快,狀壓搞一下,sum陣列記錄狀態,即有哪些數字被選了(1表示被選),這樣就可以簡單的列舉全排列的狀態了,用dp記下數,注意這裡還需要用lowbit優化來降低複雜度


#include <cstdio>
#include <cstring>
#include <algorithm>
#define lowbit(x) (x & (-x))
#define ll long long
using namespace std;
int const MOD = 1e9 + 7;
int const MAX = (1 << 24) + 1;
ll sum[MAX], dp[MAX];
int a[MAX], no[2];

int main()
{
    int n, k;
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
        sum[1 << i] = a[i];
    }
    scanf("%d", &k);
    for(int i = 0; i < k; i++)
        scanf("%d", &no[i]);
    dp[0] = 1;
    for(int i = 1; i < (1 << n); i++)
    {
        sum[i] = sum[i & ~lowbit(i)] + sum[lowbit(i)];
        if(sum[i] == no[0] || sum[i] == no[1])
            continue; 
        for(int j = i; j != 0; j -= lowbit(j))
        {
            dp[i] += dp[i & ~lowbit(j)];
            if(dp[i] > MOD)
                dp[i] -= MOD;
        }
    }
    printf("%I64d\n", dp[(1 << n) - 1]);
}


相關文章