CF 327E(Axis Walking-状态压缩Dp-lowbit的使用)
2013-07-05 09:11
459 查看
E. Axis Walking
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
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
output
input
output
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.
状态压缩DP
考试时由于24*2^24 复杂度太高没写
结果答案居然就是这样
不过枚举时要直接用lowbit(i),返回min(2^k) (i)2 第k位为1
大家直接看答案就行了
忽然发现几乎没做过状压DP的题
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
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.
状态压缩DP
考试时由于24*2^24 复杂度太高没写
结果答案居然就是这样
不过枚举时要直接用lowbit(i),返回min(2^k) (i)2 第k位为1
大家直接看答案就行了
忽然发现几乎没做过状压DP的题
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #include<functional> #include<cmath> #include<cctype> #include<cassert> #include<climits> using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Rep(i,n) for(int i=0;i<n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define ForD(i,n) for(int i=n;i;i--) #define Forp(x) for(int p=pre[x];p;p=next[p]) #define RepD(i,n) for(int i=n;i>=0;i--) #define MEM(a) memset(a,0,sizeof(a)) #define MEMI(a) memset(a,127,sizeof(a)) #define MEMi(a) memset(a,128,sizeof(a)) #define INF (2139062143) #define F (1000000007) #define MAXN (1<<24) #define MAXItem (24+10) typedef long long ll; int n,k; ll f[MAXN]={0},g[MAXN]={0},A[MAXN]={0},a[MAXItem]={0},b[MAXItem]={0}; int main() { // freopen("CF327E.in","r",stdin); scanf("%d",&n); For(i,n) scanf("%d",&a[i]),A[1<<i-1]=a[i]; cin>>k; For(i,k) scanf("%d",&b[i]);Fork(i,k+1,2) b[i]=-1; //除了x=-1,x^-1!=0 Rep(i,1<<n) f[i]=f[i-(i&(-i))]+A[i&(-i)]; g[0]=1; Rep(i,1<<n) if (f[i]^b[1]&&f[i]^b[2]&&f[i]^b[3]) { //for(int j=(1<<n)-1;j;j-=j&(-j)) f[i+(j&-j) for(int j=i;j;j-=j&(-j)) g[i]=g[i]+g[i-(j&(-j))]; g[i]%=F; } printf("%I64d",g[(1<<n)-1]%F); // while(1); return 0; }
相关文章推荐
- CF 327E(Axis Walking-状态压缩Dp-lowbit的使用)
- CF - 580D. Kefa and Dishes - 状态压缩DP
- CodeForces Round #191 (327E) - Axis Walking 状态DP..
- CF 259div2 D (状态压缩dp)
- CF 453B状态压缩dp
- CF 141 Div2 C Fractal Detector(状态压缩DP)
- CF 453B Little Pony and Harmony Chest 解题报告(状态压缩DP)
- CF 417D - Cunning Gena--状态压缩DP
- hdu1074 Doing Homework (状态压缩dp)
- hdu - 4628 - Pieces(状态压缩dp)
- POJ 1185 炮兵阵地(状态压缩dp)
- POJ 3254 Corn Fields (状态压缩DP)
- HLG 1473 教主的遗产【状态压缩DP】
- hdu4628之状态压缩dp
- hdu2817之状态压缩dp
- hdu 3001 Travelling (状态压缩dp-----模拟三进制)
- lightoj 1061 状态压缩DP
- [OpenJ_Bailian - 4124 海贼王之伟大航路] DFS+二进制状态压缩 / 状压DP
- ZOJ 2673 Hexagon and Rhombic Dominoes (状态压缩+dp)
- HDU 5135 Little Zu Chongzhi's Triangles(状态压缩dp+Vector)