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 2b−1中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;
}