Count The Bits —— 数位DP

202 篇文章 5 订阅

Given an integer k and a number of bits b (1 ≤ b ≤ 128), calculate the total number of 1 bits in the binary representations of multiples of k between 0 and 2b −1 (inclusive), modulo 1,000,000,009.
Input
The input will consist of two integers k and b on a single line, with 1 ≤ k ≤ 1000 and 1 ≤ b ≤ 128.
Output
Write your result as an integer on a single line.
Sample Input and Output
1 4 32
10 5 8
100 7 3
3 28 252698795

题意:

给你一个数k,让你找出 2 b − 1 2^b-1 2b1中k所有倍数的所有位中1的和,比如说4就有1位,5有2位。

题解:

一般让你在一个范围内找一个数的倍数什么的就是数位dp,这道题有三个约束条件:位数,余数,1的个数,由于我们只需要知道这个数的倍数的1位有多少,而不是那个数到底是什么,比如说4和8对于2来说是一样的,那么dp[i][j][k]表示的是枚举到i这一位的时候,1位的数量为j的时候,余数为k的时候有多少种可能。
(主要数位dp都长这样,敲起来很简单)

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll dp[130][130][1005];
ll mod=1e9+9;
int k,b;
ll dfs(int pos,int res,int num1,int maxn)
{
    if(pos<=0)
        return res==0?num1:0;
    if(!maxn&&dp[pos][num1][res]!=-1)
        return dp[pos][num1][res];
    ll ans=0;
    for(int i=0;i<=1;i++)
        ans=(ans+dfs(pos-1,(res*2+i)%k,num1+i,maxn&i))%mod;
    dp[pos][num1][res]=ans;
    return ans;
}
int main()
{
    memset(dp,-1,sizeof(dp));
    scanf("%d%d",&k,&b);
    printf("%lld\n",dfs(b,0,0,1));
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值