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;
}
posted @ 2018-03-18 23:10  yuanyuan-97  阅读(144)  评论(0编辑  收藏  举报