【Codeforces Round #228 (Div.1)】—D. Fox and Perfect Sets(线性基+数位dp)
考虑实际上就是构造一个线性基使得最大数不超过
令表示前位,已经有个基,是否抵着上界
考虑当前位是否加入一个新的基
如果不加,则考虑前面个异或起来这一位是否为
为的方案都是
#include<bits/stdc++.h>
using namespace std;
const int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ob==ib)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ob==ib)?EOF:*ib++;
}
#define gc getchar
inline int read(){
char ch=gc();
int res=0,f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
#define ll long long
#define re register
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define cs const
#define bg begin
#define poly vector<int>
cs int mod=1e9+7;
inline int add(int a,int b){return (a+=b)>=mod?a-mod:a;}
inline int dec(int a,int b){return (a-=b)<0?a+mod:a;}
inline int mul(int a,int b){return 1ll*a*b%mod;}
inline void Add(int &a,int b){(a+=b)>=mod?a-=mod:0;}
inline void Dec(int &a,int b){(a-=b)<0?a+=mod:0;}
inline void Mul(int &a,int b){a=1ll*a*b%mod;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),a);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
inline void chemx(int &a,int b){a<b?a=b:0;}
inline void chemn(int &a,int b){a>b?a=b:0;}
cs int N=36;
int k,p[N],len,f[N][N][2];
int main(){
k=read();
while(k){
p[++len]=k%2;
k/=2;
}
reverse(p+1,p+len+1);
f[0][0][1]=1;
for(int i=1;i<=len;i++)
for(int j=0;j<=len;j++){
int x=j?(1<<(j-1)):1,y=j?(1<<(j-1)):0;
Add(f[i][j][0],mul(f[i-1][j][0],1<<j));
Add(f[i][j+1][0],f[i-1][j][0]);
if(p[i]){
Add(f[i][j][0],mul(f[i-1][j][1],x));
Add(f[i][j][1],mul(f[i-1][j][1],y));
Add(f[i][j+1][1],f[i-1][j][1]);
}
else Add(f[i][j][1],mul(f[i-1][j][1],x));
}
int res=0;
for(int i=0;i<=len;i++)Add(res,add(f[len][i][0],f[len][i][1]));
cout<<res;
}