BZOJ3209 花神的数论题 【组合数学+数位DP+快速幂】*

时间:2022-11-09 23:20:02

BZOJ3209 花神的数论题


Description

背景
众所周知,花神多年来凭借无边的神力狂虐各大 OJ、OI、CF、TC …… 当然也包括 CH 啦。
描述
话说花神这天又来讲课了。课后照例有超级难的神题啦…… 我等蒟蒻又遭殃了。
花神的题目是这样的
设 sum(i) 表示 i 的二进制表示中 1 的个数。给出一个正整数 N ,花神要问你
派(Sum(i)),也就是 sum(1)—sum(N) 的乘积。

Input

一个正整数 N。

Output

一个数,答案模 10000007 的值。

Sample Input

样例输入一
3

Sample Output

样例输出一
2

HINT

对于样例一,1∗1∗2=21*1*2=21∗1∗2=2;
数据范围与约定
对于 100% 的数据,N≤10^15


因为我们发现二进制位中1的个数不会太多,所以我们就想到了快速幂来做这题
但是如何计算小于等于n的数中二进制位有一定个数1的数的个数呢?
假设我们固定一个数的前i位和n相同,然后如果n的当前位是0必须相等,如果当前位n的二进制是1那么我们固定当前位置是0,就可以用组合数算出剩下的几个二进制位的方案数了


注意我们这样只能算严格小于n的贡献,所以n的贡献在一开始就需要统计上


 #include<bits/stdc++.h>
using namespace std;
#define fu(a,b,c) for(int a=b;a<=c;++a)
#define fd(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define N 60
#define Mod 10000007
LL n,p[N],len=;
LL c[N][N];
void init(){
fu(i,,N-)c[i][]=;
fu(i,,N-)
fu(j,,i)c[i][j]=c[i-][j]+c[i-][j-];
}
LL fast_pow(LL a,LL b){
LL res=;
while(b){
if(b&)res=res*a%Mod;
b>>=;
a=a*a%Mod;
}
return res;
}
LL count(int num){
LL res=;
fd(i,len,)if(p[i]){
res+=c[i-][num];
num--;
if(num<)return res;
}
return res;
}
int main(){
init();
scanf("%lld",&n);
LL ans=;
while(n){
p[++len]=n&;
n>>=;
}
fu(i,,len)ans+=p[i];
fu(i,,len)ans=ans*fast_pow(i,count(i))%Mod;
printf("%lld",ans);
return ;
}