Codecraft-18 and Codeforces Round#458 C. Travelling Salesman and Special Numbers(数位DP)

The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 1310 = 11012, so it has 3 bits set and 13 will be reduced to 3 in one operation.

He calls a number special if the minimum number of operations to reduce it to 1 is k.

He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination!

Since the answer can be large, output it modulo 109 + 7.

Input

The first line contains integer n (1 ≤ n < 21000).

The second line contains integer k (0 ≤ k ≤ 1000).

Note that n is given in its binary representation without any leading zeros.

Output

Output a single integer — the number of special numbers not greater than n, modulo 109 + 7.

Examples
input
110
2
output
3
input
111111011
2
output
169
Note

In the first sample, the three special numbers are 35 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 35 and 6) and then to 1 in one more operation (since there is only one set bit in 2).

题意:有一个操作,可以使一个数字变成另一个数字,这个数字就是原数字二进制形式中1的个数。比如13的二进制位1101,有3个1,那么经过一次操作就变成了3,直到变为1为止,所经过的步数就是最小操作步数。

现在问1~n中有多少个数最小操作步数为k。

思路:因为n很大,有2的1000次方这么大,换成二进制也就是有1000位。我们先预处理出二进制形式中有i个1的数字变为1需要的步数,然后用数位DP的形式把小于n且最小操作步数为k的数量求出来就可以了。

还有注意特判k == 0和k == 1的情况就可以了。


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long

using namespace std;

const int mod = 1e9 + 7;

int c[1100];//c数组表示二进制有i个1的数字需要c[i]步到1
int f[1100];//f数组表示二进制有i个1的数字的步数是否为1
int n;
int a[1100];
char s[1100];
LL dp[1100][1100];

int cont(int x)
{
    int cnt = 0;
    while(x)
    {
        if(x % 2 == 1)
            cnt++;
        x/=2;
    }
    return cnt;
}

LL dfs(int len,int s,int fp)//s表示1的个数
{
    if(!len)
        return f[s];
    if(!fp && dp[len][s] != -1)
        return dp[len][s];
    LL res = 0;
    int n = fp?a[len]:1;
    for(int i=0;i<=n;i++)
    {
        int ts = s;
        if(i == 1)
            ts++;
        res += dfs(len-1,ts,fp && i == n);
        res %= mod;
    }
    if(!fp)
        dp[len][s] = res;
    return res;
}
LL sum()
{
    int len = 0;
    for(int i=n;i>=1;i--)
        a[++len] = s[i]-'0';
    return dfs(len,0,1);
}

int main(void)
{
    int i,j,k;
    memset(dp,-1,sizeof(dp));
    for(i=1;i<=1000;i++)
    {
        int t = i;
        int cnt = 0;
        while(t != 1)
        {
            t = cont(t);
            cnt++;
        }
        c[i] = cnt+1;
    }
    LL ans = 0;
    scanf("%s",s+1);
    scanf("%d",&k);
    n = strlen(s+1);
    if(k == 0)
    {
        printf("1\n");
        return 0;
    }
    if(k == 1)
    {
        printf("%d\n",n-1);
        return 0;
    }
    for(i=1;i<=1000;i++)
        if(c[i] == k)
           f[i] = 1;
    ans = sum();
    cout << ans << endl;

    return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值