bzoj3209: 花神的数论题

数位dp。

数位dp一直掌握的不好。

用数位dp求出含若干个1的数的个数。

#include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long 
using namespace std;
const LL maxn = 60;
const LL mod = 10000007;
LL C[maxn][maxn],a[maxn];
LL n,res,cnt;

LL power(LL x,LL e) {
    LL res=1;    
    while(e) {
        if(e&1) res=res*x%mod;
        x=x*x%mod;
        e>>=1;
    }
    return res;
}

LL calc(int x) {
    LL res=0; 
    for(int i=cnt;i && x>=0;i--) 
        if(a[i]) res+=C[i-1][x--];
    return res;
}

int main() {
    for(int i=0;i<=50;i++) {
        C[i][0]=1;
        for(int j=1;j<=i;j++)
            C[i][j]=C[i-1][j]+C[i-1][j-1];
    }
    scanf("%lld",&n),n++;
    
    while(n) {
        a[++cnt]=n%2;
        n>>=1;    
    }
    
    res=1;
    for(int i=1;i<=cnt;i++) 
        res=res*power(i,calc(i))%mod;
    printf("%lld\n",res);
    return 0;
}
posted @ 2016-07-10 18:52  invoid  阅读(255)  评论(0编辑  收藏  举报