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 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[1<<24],sum[1<<24],kkk[2],k;

inline int lowbit(int x)
{
    return x&-x;
}

int main()
{
    scanf("%lld",&n);
    int tmp;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&tmp);
        sum[1<<i]=tmp;
    }
    scanf("%lld",&k);
    for(int i=0;i<k;i++)
    {
        scanf("%lld",&kkk[i]);
    }
    dp[0]=1;
    for(int i=1;i<(1<<n);i++)
    {
        sum[i]=sum[i^lowbit(i)]+sum[lowbit(i)];
        if(sum[i]==kkk[1]||sum[i]==kkk[0])
        {
            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[(1<<n)-1]);
}

原文地址:https://www.cnblogs.com/stxy-ferryman/p/9388853.html

时间: 2024-09-28 18:56:35

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

Codeforces 327E Axis Walking (状压dp lowbit优化)

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 integ

Codeforces 327E Axis Walking 状压dp(水

题目链接:点击打开链接 题意: 给定n个数,随意排列. 给定k个违禁数b[]. 问:有多少个排列使得这个排列的 n项前缀和中不出现违禁数. (formally,if it's a legal permutation, sum[i] != b[j] (1<=i<=n, 1<=j<=k)) sum[0] = 0; sum[i] = sum[i-1]+a[permutaion[i]]; ==java党表示被tle,心疼自己T^T #include <stdio.h> cons

CodeForces 21D Traveling Graph 状压dp+欧拉回路

题目链接:点击打开链接 题意: 给定n个点m条边的无向图 求从1点开始经过每条边至少一次最后回到1点的最小路程 显然就是找一条路径可重复的欧拉回路 思路: 首先对于欧拉回路的结论是:所有点的度数都为偶数 因为所有边至少经过一次,那么可以把题意转换成加最少多少条边使得图满足以上结论 而加的边目的是为了把奇度数转成偶度数,先floyd一下得到任意点间加边的最小花费 dp[i]表示状态i下度数都为偶数的最小花费. 状压dp,把i状态下,所有未选择的点中挑2个奇度数的转移即可. #include <cs

Codeforces 580D-Kefa and Dishes(状压DP)

原题链接:http://codeforces.com/problemset/problem/580/D 题意:在n个数字中有顺序地选择m个数字,每个数字对应有个值ai,每取一个数字答案加上ai,并且存在k个关系:x y c,如果x恰好排在y的前面,那么答案再加上ci的值.输出最大值. 思路:状压dp.dp[i][j]中,i是已经选了若干个数的情况,j是最后一个被选取的数,i从选取1个到m个枚举下去,j从第1个数到第n个数进行枚举就能得到答案. AC代码: 1 #include<iostream>

COdeforces#417D Cunning Gena(状压DP)

A boy named Gena really wants to get to the "Russian Code Cup" finals, or at least get a t-shirt. But the offered problems are too complex, so he made an arrangement with his n friends that they will solve the problems for him. The participants

Codeforces 544E Remembering Strings 状压dp

题目链接 题意: 给定n个长度均为m的字符串 下面n行给出字符串 下面n*m的矩阵表示把对应的字母修改成其他字母的花费. 问: 对于一个字符串,若它是easy to remembering 当 它存在一个字母,使得这个字母在这一列是独一无二的. 要使得n个字符串都是easy to remembering 的最小花费. 第一个样例是把第一列的4个a中3个a修改成别的字母,所以花费为3. 思路: 显然是个状压dp,但需要一点转化. 首先得到一个结论: 对于某一列,设这一列的字母是 a,a,b,b,a

CodeForces 907E Party(bfs+状压DP)

Arseny likes to organize parties and invite people to it. However, not only friends come to his parties, but friends of his friends, friends of friends of his friends and so on. That's why some of Arseny's guests can be unknown to him. He decided to

【BZOJ】1072: [SCOI2007]排列perm(状压dp+特殊的技巧)

http://www.lydsy.com/JudgeOnline/problem.php?id=1072 首先无限膜拜题解orz表示只会暴力orz 数据那么小我竟然想不到状压! orz 这种题可以取模设状态orz f[i,j]表示状态为i,mod d为j的方案 则答案为f[all, 0] 转移就太简单了orz f[i|1<<k, (j*10+c[k])%d]+=f[i, j] 然后我有一sb错一直没找到,wa了n发... QAQ #include <cstdio> #include

Codeforces 453B Little Pony and Harmony Chest:状压dp【记录转移路径】

题目链接:http://codeforces.com/problemset/problem/453/B 题意: 给你一个长度为n的数列a,让你构造一个长度为n的数列b. 在保证b中任意两数gcd都为1的情况下,使得 ∑|a[i]-b[i]|最小. 让你输出构造的数列b. (1<=n<=100, 1<=a[i]<=30) 题解: 因为1<=a[i]<=30,所以有1<=b[i]<=60,此时才有可能最优. 因为b中任意两数gcd为1,所以对于一个质因子p[i]