P2396 yyy loves Maths VII 状压dp 变态卡常

很容易想到dp,但是1<<24都有1600w的复杂度,要是普通做法肯定T掉

共有maxs=(1<<n)-1种情况

首先f[ ]记录某种情况的方案数,dis[ ]记录某种情况的步数和

然后每次到dis[i]=厄运值就不更新

解决卡常的方法

正常做法:2^(n+1) 肯定会T

改成 :2^nlogn  O2优化过了 恶心心

f[i]=去掉i中某一个1的所有情况f[j]和

寻找某一个1用lowbit
删除某一个1用异或^

代码如下:

// luogu-judger-enable-o2

//#pragma comment (linker, "/STACK:102400000,102400000")
#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<set>
#include<stack>
#include<vector>
#include<map>
#include<queue>
#include<list>
#include<time.h>
#include<bitset>

#define myself i,l,r
#define lson i<<1
#define rson i<<1|1
#define Lson i<<1,l,mid
#define Rson i<<1|1,mid+1,r
#define half (l+r)/2
#define lowbit(x) x&(-x)
#define min4(a, b, c, d) min(min(a,b),min(c,d))
#define min3(x, y, z) min(min(x,y),z)
#define max3(x, y, z) max(max(x,y),z)
#define max4(a, b, c, d) max(max(a,b),max(c,d))
#define pii make_pair
#define pr pair<int,int>
typedef unsigned long long ull;
typedef long long ll;
const int inff = 0x3f3f3f3f;
const long long inFF = 9223372036854775807;
const int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
const int mdir[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, -1, 1, 1, -1, -1, -1};
const double eps = 1e-10;
const double PI = acos(-1.0);
const double E = 2.718281828459;
using namespace std;
const int mod=1e9+7;
const int maxn=1e5+51;
int f[1<<24],dis[1<<24];
int eyun[2];
int n,op;
int main()
{
    cin>>n;
    int maxs=(1<<n)-1;
    for(int i=0;i<n;i++) scanf("%d",&dis[1<<i]);
    cin>>op;
    for(int i=0;i<op;i++) scanf("%d",&eyun[i]);
    f[0]=1;
    for(int i=1;i<=maxs;i++)
    {
        int j=i&(-i),k=i;
        dis[i]=dis[i^j]+dis[j];
        if(dis[i]==eyun[0]||dis[i]==eyun[1]) continue;
        while(k)
        {
            j=k&(-k);
            f[i]+=f[i^j];
            if(f[i]>=mod) f[i]-=mod;
            k^=j;
        }
    }
    printf("%d\n",f[maxs]);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值