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代替模

代码如下:

  1. #pragma GCC optimize(3)
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<iostream>
  5. #include<algorithm>
  6. #define mod 1000000007
  7. using namespace std;
  8. long long n,dp[<<],sum[<<],kkk[],k;
  9. inline int lowbit(int x)
  10. {
  11. return x&-x;
  12. }
  13. int main()
  14. {
  15. scanf("%lld",&n);
  16. int tmp;
  17. for(int i=;i<n;i++)
  18. {
  19. scanf("%d",&tmp);
  20. sum[<<i]=tmp;
  21. }
  22. scanf("%lld",&k);
  23. for(int i=;i<k;i++)
  24. {
  25. scanf("%lld",&kkk[i]);
  26. }
  27. dp[]=;
  28. for(int i=;i<(<<n);i++)
  29. {
  30. sum[i]=sum[i^lowbit(i)]+sum[lowbit(i)];
  31. if(sum[i]==kkk[]||sum[i]==kkk[])
  32. {
  33. continue;
  34. }
  35. for(int j=i;j;j-=lowbit(j))
  36. {
  37. dp[i]=dp[i]+dp[i^lowbit(j)];
  38. if(dp[i]>=mod) dp[i]-=mod;
  39. }
  40. }
  41. printf("%lld\n",dp[(<<n)-]);
  42. }

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. OD 实验(十三) - 对一个程序的逆向

    程序: 运行程序 点击 Start,它就会进行对系统的扫描 点击 About -> Enter Registration Code 随便输入一下内容,点击 OK,会弹出该弹窗 用 PEiD 看一 ...

  2. ubuntu下安装stm32开发环境

    在windowns下开发stm32刚开始学最烦的就是创建工程模板,都不知道为什么要那样设置,而且步骤繁多.现在我告诉大家一个好消息,在linux下配置stm32开发环境包括创建工程,使用JLink仿真 ...

  3. 记一则css3计算

    .Head{ background-image: url("../../Img/PersonalCenter/banner.png"); background-repeat: no ...

  4. maven surefire plugin介绍

    示例 <!-- 测试运行器,生成测试报告 --> <plugin> <groupId>org.apache.maven.plugins</groupId> ...

  5. mysql彻底删除

    yum remove mysql mysql-server mysql-libs compat-mysql51rm -rf /var/lib/mysqlrm /etc/my.cnf查看是否还有mysq ...

  6. ffmpeg源码分析二:main函数和transcode函数 (转2)

    原帖地址:http://blog.csdn.net/austinblog/article/details/24804455 首先从main函数看起,关键解释部分已加注释,该函数在ffmpeg.c文件中 ...

  7. jaegeropentracing的Java-client

    关于jaegeropentracing的Java-client做记录如下: 1.依赖jar包 <!-- 以下jar包是jaegeropentracing依赖的日志jar -->slf4j- ...

  8. 漂亮的表格样式–>使用CSS样式表控制表格样式

    依照WEB2.0风格,设计了几个表格样式,希望大家喜欢.WEB2.0提倡使用div开布局,但不是要完全放弃使用表格,表格在数据展现方面还是不错的选择.现在介绍使用CSS样式表来控制.美化表格的方法. ...

  9. keepalived和zookeeper对比

    https://blog.csdn.net/vtopqx/article/details/79066703keepalived与zookeeper都可以用来实现高可用,高可用一般跟负载均衡会一起考虑, ...

  10. Apache Hive (四)Hive的连接3种连接方式

    转自:https://www.cnblogs.com/qingyunzong/p/8715925.html 一.CLI连接 进入到 bin 目录下,直接输入命令: [hadoop@hadoop3 ~] ...