CodeForces 327E Axis Walking(状压DP+卡常技巧)
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 = 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+卡常技巧)的更多相关文章
- Codeforces 327E Axis Walking 状压dp
		这题真的有2500分吗... 难以置信... #include<bits/stdc++.h> #define LL long long #define fi first #define s ... 
- codeforces Diagrams & Tableaux1 (状压DP)
		http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ... 
- Codeforces 917C - Pollywog(状压 dp+矩阵优化)
		UPD 2021.4.9:修了个 typo,为啥写题解老出现 typo 啊( Codeforces 题目传送门 & 洛谷题目传送门 这是一道 *2900 的 D1C,不过还是被我想出来了 u1 ... 
- Codeforces 79D - Password(状压 dp+差分转化)
		Codeforces 题目传送门 & 洛谷题目传送门 一个远古场的 *2800,在现在看来大概 *2600 左右罢( 不过我写这篇题解的原因大概是因为这题教会了我一个套路罢( 首先注意到每次翻 ... 
- Codeforces 544E Remembering Strings 状压dp
		题目链接 题意: 给定n个长度均为m的字符串 以下n行给出字符串 以下n*m的矩阵表示把相应的字母改动成其它字母的花费. 问: 对于一个字符串,若它是easy to remembering 当 它存在 ... 
- codeforces 21D. Traveling Graph 状压dp
		题目链接 题目大意: 给一个无向图, n个点m条边, 每条边有权值, 问你从1出发, 每条边至少走一次, 最终回到点1. 所走的距离最短是多少. 如果这个图是一个欧拉回路, 即所有点的度数为偶数. 那 ... 
- Codeforces 895C - Square Subsets 状压DP
		题意: 给了n个数,要求有几个子集使子集中元素的和为一个数的平方. 题解: 因为每个数都可以分解为质数的乘积,所有的数都小于70,所以在小于70的数中一共只有19个质数.可以使用状压DP,每一位上0表 ... 
- Codeforces ----- Kefa and Dishes [状压dp]
		题目传送门:580D 题目大意:给你n道菜以及每道菜一个权值,k个条件,即第y道菜在第x道后马上吃有z的附加值,求从中取m道菜的最大权值 看到这道题,我们会想到去枚举,但是很显然这是会超时的,再一看数 ... 
- CodeForces 907E Party(bfs+状压DP)
		Arseny likes to organize parties and invite people to it. However, not only friends come to his part ... 
随机推荐
- Centos下Apache+Tomcat集群--搭建记录
			一.目的 利用apache的mod_jk模块,实现tomcat集群服务器的负载均衡以及会话复制,这里用到了<Cluster>. 二.环境 1.基础:3台主机,系统Centos6.5,4G内 ... 
- OD 实验(十二) - 对一个 Delphi 程序的逆向
			程序: 运行程序 界面显示的是未注册 点击 Help -> About 点击 Use Reg Key 这里输入注册码 用 PEiD 看一下 该程序是用 Delphi 6.0 - 7.0 写的 逆 ... 
- vue组件之echarts报表
			vue组件之echarts报表 将echarts报表封装成组件,动态传入数据,显示图表. 1.饼状图 父组件: <MPie :datas="piedata"></ ... 
- C# 有关控件、自定义类事件中的委托链的获取、移除操作
			直接来代码吧,这样干脆直接,也不耽误我午休了.一切尽在源码中. public class ControlEventTool { /// <summary> /// 移除控件的某类事件, 如 ... 
- 为什么是static?
			为什么是static因为系统开始执行一个程序前,并没有创建main()方法所在类的实例对象,它只能通过类名类调用主方法. public static void main(String args[])我 ... 
- PHP 性能优化之 PHP-FPM
			简介: PHP-FPM 是一个 PHP FastCGI 管理器,一般 Nginx 上面跑 PHP 程序都会将 PHP 程序丢给 PHP-FPM 来解析.好了,就这样! PHP 5.4 开始集成了 PH ... 
- Ryu控制器学习
			Ryu 在Mininet环境下实现Ryu为控制器控制ARP报文的实验中学习了Ryu相关的知识,记录如下 官方文档:http://ryu.readthedocs.io/en/latest/getting ... 
- win 10+ iis 10 部署.net core 1.1 web api
			今天上午部署了wcf,部署了好久,一直没有部署好,最后找到了dudu的部署方法,结果中午吃饭的时候成功了,这是链接:http://www.cnblogs.com/dudu/p/3328066.html ... 
- 浅谈JobExecutionContext&JobDataMap
- R包安装失败failed to download mirrors file
			在R console中使用install.packages()来安装第三方包时,会出现这样的错误: 即使我们选择的是China的镜像也解决不了问题. 这时候,可以先试试用IE打开上图中黑底部分的URL ... 
