[BZOJ3209]花神的数论题
[BZOJ3209]花神的数论题
BZOJ
luogu
枚举二进制位上1的个数i
数位dp求出n以内的个数k
答案即\(\prod i^k\)
注意\(x^k\mod p\neq x^{k\mod p}\mod p\)
#define ll long long
#include<bits/stdc++.h>
using namespace std;
const int mod=10000007;
int len,ans=1,k,w[55];
ll n,f[55][55];
void mul(int&x,int y){x=1ll*x*y%mod;}
int ksm(int x,ll y){
int s=1;
while(y){if(y&1)mul(s,x);mul(x,x);y>>=1;}
return s;
}
ll dfs(int p,int s,bool lim){
if(!p)return (s==k);
if(!lim&&f[p][s]!=-1)return f[p][s];
int up=lim?w[p]:1;ll res=0;
for(int i=0;i<=up;i++)
res+=dfs(p-1,s+i,lim&&(i==up));
return lim?res:f[p][s]=res;
}
int main(){
cin>>n;
while(n){w[++len]=n&1;n>>=1;}
for(int i=1;i<=len;i++){
memset(f,-1,sizeof(f));k=i;
mul(ans,ksm(i,dfs(len,0,1)));
}
cout<<ans;return 0;
}