# 2020CCPC长春D. Meaningless Sequence(思维+数位DP）

a=c^(二进制中1的个数）

#include<iostream>
#include<cstdio>
#include<string.h>
#include<string>
using namespace std;
#define ll long long
const int mod = 1e9 + 7;
ll ans;
int m;
string a;
int sz;
ll c[3100][3100]; //组合数
int num[3100];
ll fastpow(ll base, ll n, ll mod) {
ll ans = 1;
while (n) {
if (n & 1) ans *= base % mod, ans %= mod;
base *= base, base %= mod;
n >>= 1;

}
return ans % mod;
}

void init() {
cin >> a;
cin >> m;
sz = a.size();
for (int i = 0; i <= sz; i++) {
c[i][i] = c[i][0] = 1;
for (int j = 1; j < i; j++) {
c[i][j] = (c[i - 1][j] + c[i - 1][j - 1])%mod;
}
}
}
ll helper(int pos, int limit, int cnt, int k) {
if (pos == -1)
return cnt==k;
if (!limit) {
if (cnt <= k)
return c[pos + 1][k - cnt];
else
return 0;
}
ll res = 0;
int up = limit ? num[pos] : 1;
for (int i = 0; i <= up; i++) {
res += helper(pos - 1, limit && i == up, cnt+i, k) % mod;
res %= mod;
}
return res;
}
void solve() {
for (int i = 0; i < sz; i++)
num[i] = a[sz - i - 1] - '0';
ll base = 1;
for (int i = 0; i <= sz; i++) {  //枚举1的个数
ans += base * helper(sz - 1, 1, 0, i) % mod;
ans %= mod;
base *= m;
base %= mod;
}
cout << ans << endl;
}
int main() {
init();
solve();
return 0;
}


11-13 32
11-09 2569