[CF327E]Axis Walking

时间:2020-08-08
本文章向大家介绍[CF327E]Axis Walking,主要包括[CF327E]Axis Walking使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

题目

传送门

题解

一道很水的状压题...

\(f[s]\) 为数字出现情况为 \(s\) 时的方案数,显然如果 \(\exist k[i],sum[s]=k[i]\) 那么 \(f[s]=0\),其中 \(sum[s]\) 为选择数字情况为 \(s\) 时的数字和,而转移也很好写了:

\[f[s]=\sum f[s'] \]

其中 \(s'\)\(s\) 少出现了一个数字的状态。

有一个边界条件 —— \(f[0]=1\),其他的没什么好说的了......

但是洛谷推荐有一道模型一样的题,需要对题目有很深了解并且合理转化,推荐去做一做。

推荐题唯一 \(ex\) 的就是它卡常...

代码

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;

#define rep(i,__l,__r) for(register signed i=(__l),i##_end_=(__r);i<=i##_end_;++i)
#define fep(i,__l,__r) for(register signed i=(__l),i##_end_=(__r);i>=i##_end_;--i)
#define erep(i,u) for(signed i=tail[u],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
typedef long long LL;
typedef pair<int,int> pii;
#define ft first
#define sd second
typedef unsigned long long ull;
typedef unsigned uint;
#define Endl putchar('\n')
// #define int long long
// #define int unsigned
// #define int unsigned long long

#define cg (c=getchar())
template<class T>inline void read(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
template<class T>inline T read(const T sample){
    T x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
template<class T>void fwrit(const T x){//just short,int and long long
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);
    putchar(x%10^48);
}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
    inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

const int MOD=1e9+7;
const int MAXK=2;
const int MAXN=24;
const int MAXSIZE=1<<24;

int n,k,all,a[MAXSIZE+5],ban[3];

#define lowbit(i) ((i)&-(i))

int f[MAXSIZE+5];

inline void Init(){
    n=1<<read(1);
    for(int i=1;i<n;i<<=1)a[i]=read(1);
    (k=read(1))&&(ban[1]=read(1),k>1)&&(ban[2]=read(1));
    ((k>=1 && !ban[1]) || (k==2 && !ban[2])) && (writc(0,'\n'),exit(0),0);
}

inline void Get_f(){
    f[0]=1;
    register int low;
    rep(i,1,n-1){
        if(k && (a[i]=a[i^(low=lowbit(i))]+a[low])==ban[1])continue;
        if(k>1 && a[i]==ban[2])continue;
        for(int j=i;j;j^=low)(f[i]+=f[i^(low=lowbit(j))])>=MOD&&(f[i]-=MOD);
    }
    writc(f[n-1],'\n');
}

signed main(){
    Init();
    Get_f();
    return 0;
}

原文地址:https://www.cnblogs.com/Arextre/p/13460640.html