codeforces 388D Fox and Perfect Sets(线性基+数位dp)

Posted wuyuanyuan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了codeforces 388D Fox and Perfect Sets(线性基+数位dp)相关的知识,希望对你有一定的参考价值。

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define sz(x) (int)x.size()
#define de(x) cout<< #x<<" = "<<x<<endl
#define dd(x) cout<< #x<<" = "<<x<<" "
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int P=1e9+7;
int n, k;
int a[33];
ll f[33][33][2], pw[33];

void upd(ll &a, ll b) {
    a+=b;
    if(a>=P) a-=P;
}
void init() {
    pw[0]=1;
    rep(i,1,33) pw[i]=pw[i-1]*2%P;
}

int main() {
    init();
    while(~scanf("%d",&k)) {
        n=0;
        while(k) {
            a[++n]=(k&1);
            k>>=1;
        }
        for(int l=1, r=n;l<r;++l, --r) swap(a[l], a[r]);
        memset(f,0,sizeof(f));
        f[0][0][1]=1;
        rep(i,0,n) rep(j,0,i+1) {
            if(f[i][j][0]) {
                upd(f[i+1][j+1][0], f[i][j][0]);
                upd(f[i+1][j][0], f[i][j][0]*pw[j]%P);
            }
            if(f[i][j][1]) {
                if(a[i+1]) upd(f[i+1][j+1][1], f[i][j][1]);
                if(a[i+1]) upd(f[i+1][j][0], f[i][j][1]*(j?pw[j-1]:1)%P);
                upd(f[i+1][j][1], f[i][j][1]*(j?pw[j-1]:0)%P);
            }
        }
        ll ans=0;
        rep(i,0,n+1) rep(j,0,2) if(f[n][i][j]) {
            upd(ans, f[n][i][j]);
        }
        printf("%lld\n",ans);
    } 
    return 0;
}

以上是关于codeforces 388D Fox and Perfect Sets(线性基+数位dp)的主要内容,如果未能解决你的问题,请参考以下文章

codeforce 510C Fox And Names

网络流(最大流)CodeForces 512C:Fox And Dinner

Codeforces 388 D. Fox and Perfect Sets

[Codeforces#510C] Fox And Names

[Codeforces#510D] Fox And Jumping

Codeforces 512D. Fox And Travelling 题解