1. 程式人生 > >BZOJ_3209_花神的數論題_組合數+數位DP

BZOJ_3209_花神的數論題_組合數+數位DP

HR 多少 algorithm -s NPU return sum long pan

BZOJ_3209_花神的數論題_組合數+數位DP

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=2;

數據範圍與約定

對於 100% 的數據,N≤10^15


設f[i][j]表示所有i位數中1的個數為j的數的個數。

然後發現這是組合數,相當於i個數中選j個。

然後數位DP求出每個數在答案中出現了多少次。

乘起來即可。

代碼:

#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll mod=10000007;
ll c[150][150],n,cnt[150];
ll qp(ll x,ll y) {
    ll re=1;for(;y;y>>=1ll,x=x*x%mod) if(y&1ll) re=re*x%mod; return re;
}
int main() {
    int i,j;
    for(i=0;i<=60;i++) c[i][0]=c[i][i]=1;
    for(i=1;i<=60;i++) {
        for(j=1;j<i;j++) {
            c[i][j]=c[i-1][j]+c[i-1][j-1];
        }
    }
    scanf("%lld",&n); n++;
    int now=0;
    for(i=60;i>=1;i--) {
        // printf("%d",(n&(1ll<<(i-1)))!=0);
        if(!(n&(1ll<<(i-1)))) continue;
        for(j=1;j<=60;j++) {
            if(j>=now&&j-now<=i-1) cnt[j]+=c[i-1][j-now];
        }
        now++;
    }
    // puts("");
    ll ans=1;
    for(i=1;i<=60;i++) {
        // printf("%lld\n",cnt[i]);
        ans=ans*qp(i,cnt[i])%mod;
    }
    printf("%lld\n",ans);
}

BZOJ_3209_花神的數論題_組合數+數位DP