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 = ap2and 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 knumbers. 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 nintegers: 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.

题意:给出n个数,给出至多两个数k1,k2,求这n个数每个排列中前缀和不含k1/k2的排列个数

题解:首先看着范围考虑状压dp

dp[sta]=sigma(dp[sta^1<<i]) i为sta中所有1的位置

sum[sta]==k1||sum[sta]==k2 dp[sta]=0;

然后显然直接模会非常慢, 所以用-mod代替模

代码如下:

#pragma GCC optimize(3)
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define mod 1000000007
using namespace std; long long n,dp[<<],sum[<<],kkk[],k; inline int lowbit(int x)
{
return x&-x;
} int main()
{
scanf("%lld",&n);
int tmp;
for(int i=;i<n;i++)
{
scanf("%d",&tmp);
sum[<<i]=tmp;
}
scanf("%lld",&k);
for(int i=;i<k;i++)
{
scanf("%lld",&kkk[i]);
}
dp[]=;
for(int i=;i<(<<n);i++)
{
sum[i]=sum[i^lowbit(i)]+sum[lowbit(i)];
if(sum[i]==kkk[]||sum[i]==kkk[])
{
continue;
}
for(int j=i;j;j-=lowbit(j))
{
dp[i]=dp[i]+dp[i^lowbit(j)];
if(dp[i]>=mod) dp[i]-=mod;
}
}
printf("%lld\n",dp[(<<n)-]);
}

CodeForces 327E Axis Walking(状压DP+卡常技巧)的更多相关文章

  1. Codeforces 327E Axis Walking 状压dp

    这题真的有2500分吗... 难以置信... #include<bits/stdc++.h> #define LL long long #define fi first #define s ...

  2. codeforces Diagrams & Tableaux1 (状压DP)

    http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ...

  3. Codeforces 917C - Pollywog(状压 dp+矩阵优化)

    UPD 2021.4.9:修了个 typo,为啥写题解老出现 typo 啊( Codeforces 题目传送门 & 洛谷题目传送门 这是一道 *2900 的 D1C,不过还是被我想出来了 u1 ...

  4. Codeforces 79D - Password(状压 dp+差分转化)

    Codeforces 题目传送门 & 洛谷题目传送门 一个远古场的 *2800,在现在看来大概 *2600 左右罢( 不过我写这篇题解的原因大概是因为这题教会了我一个套路罢( 首先注意到每次翻 ...

  5. Codeforces 544E Remembering Strings 状压dp

    题目链接 题意: 给定n个长度均为m的字符串 以下n行给出字符串 以下n*m的矩阵表示把相应的字母改动成其它字母的花费. 问: 对于一个字符串,若它是easy to remembering 当 它存在 ...

  6. codeforces 21D. Traveling Graph 状压dp

    题目链接 题目大意: 给一个无向图, n个点m条边, 每条边有权值, 问你从1出发, 每条边至少走一次, 最终回到点1. 所走的距离最短是多少. 如果这个图是一个欧拉回路, 即所有点的度数为偶数. 那 ...

  7. Codeforces 895C - Square Subsets 状压DP

    题意: 给了n个数,要求有几个子集使子集中元素的和为一个数的平方. 题解: 因为每个数都可以分解为质数的乘积,所有的数都小于70,所以在小于70的数中一共只有19个质数.可以使用状压DP,每一位上0表 ...

  8. Codeforces ----- Kefa and Dishes [状压dp]

    题目传送门:580D 题目大意:给你n道菜以及每道菜一个权值,k个条件,即第y道菜在第x道后马上吃有z的附加值,求从中取m道菜的最大权值 看到这道题,我们会想到去枚举,但是很显然这是会超时的,再一看数 ...

  9. CodeForces 907E Party(bfs+状压DP)

    Arseny likes to organize parties and invite people to it. However, not only friends come to his part ...

随机推荐

  1. 把价钱转化为xx.xx的形式

    把number类型转化为字符串类型let orderPayFeeStr = this.state.orderPayFee.toString();//商品价格转化为字符串if(orderPayFeeSt ...

  2. linux 监控系统剩余内存大小

    cur_free = `free -m | awk '/buffers\// {print $NF}'` chars="current memory is $cur_free." ...

  3. keil中结构体跨文件调用

    在a.h中: 定义了, struct ABC{ short a; short b; ```}; 在a.c中(#include "a.h"): 声明了, struct ABC stc ...

  4. fatal error C1010: 在查找预编译头时遇到意外的文件结尾 (转)

    错误描述:fatal error C1010: 在查找预编译头时遇到意外的文件结尾.是否忘记了向源中添加“#include "stdafx.h"”? 错误分析:     此错误发生 ...

  5. UNIX网络编程——客户/服务器心搏函数 (转)

    下面是关于回送客户和服务器程序开发一些简单的心搏函数.这些函数可以发现对端主机或到对端的通信路径的过早失效.         在给出这些函数之前我们必须提出一些警告.首先,有人会想到使用TCP的保持存 ...

  6. 塔防游戏 Day1

    1. 创建地图 如上图所示,白色方块为可以放置塔楼的区域,粉红色区域为敌人的行走区域,绿色方块处为起始点,黄色方块点为终止点. 2. 控制游戏的视野 public class ViewScript : ...

  7. java.lang.IllegalStateException: getOutputStream() has already been called for this response解决方案

    异常产生原因:web容器生成的servlet代码中有out.write(""),这个和JSP中调用的response.getOutputStream()产生冲突.即Servlet规 ...

  8. css常用属性总结第二弹:id选择器

    承接上一篇class选择器,这一篇我们来说说css的id选择器. id选择器类似于类选择器,不过也有一些重要的差别,首先,id选择器前面有一个#号----称它为棋牌号吧,class为点号,用法就和cl ...

  9. 工具类: 用于模拟HTTP请求中GET/POST方式

    package com.jarvis.base.util; import java.io.BufferedReader; import java.io.IOException; import java ...

  10. Perl 子程序(函数)

    1.Perl 子程序(函数)Perl 子程序也就是用户定义的函数.Perl 子程序即执行一个特殊任务的一段分离的代码,它可以使减少重复代码且使程序易读. Perl 子程序可以出现在程序的任何地方,语法 ...