BZOJ 3209: 花神的数论题 [数位DP]

本文解析了一道求解1到n(n≤10^15)间所有数的二进制表示中1的个数乘积的问题,并通过数位DP的方法进行解答。文章详细介绍了如何统计不同数量的1在各个数中的分布,并使用快速幂来计算最终答案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

3209: 花神的数论题

题意:求\(1到n\le 10^{15}\)二进制1的个数的乘积,取模1e7+7


二进制最多50位,我们统计每种1的个数的数的个数,快速幂再乘起来就行了
裸数位DP..\(f[i][j]\)i位数j个1的方案数..不考虑天际线就是组合数...

比较坑的地方是本题求f要取模\(phi(1e7+7)\),然后它并不是质数...

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N=60;
const ll P=10000007, Phi=9988440;
inline ll read(){
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}

ll n, ans=1; int a[N], len;
ll Pow(ll a, ll b) { //printf("Pow %lld %lld\n",a,b);
    ll ans=1;
    for(; b; b>>=1, a=a*a%P) 
        if(b&1) ans=ans*a%P;
    return ans;
}
ll f[N][N];
ll dfs(int d, int sky, int x) {
    if(d==0) return x==0;
    if(!sky) return f[d][x];
    int lim = sky ? a[d] : 1; 
    ll now=0;
    for(int i=0; i<=lim; i++) (now += dfs(d-1, sky && i==lim, x-i))%=Phi;
    return sky ? now : f[d][x]=now;
}
int main() {
    freopen("in","r",stdin);
    n=read(); 
    while(n) a[++len]=n&1, n>>=1;
    //memset(f,-1,sizeof(f));
    f[0][0]=1;
    for(int i=1; i<=len; i++){
        f[i][0]=1;
        for(int j=1; j<=i; j++) f[i][j]=(f[i-1][j]+f[i-1][j-1])%Phi;
    }
    for(int i=2; i<=len; i++)
        ans = ans*Pow(i, dfs(len, 1, i) )%P;
    printf("%lld", ans);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值