zoukankan      html  css  js  c++  java
  • Codeforces Round #458C DP

    C. Travelling Salesman and Special Numbers
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2).

    代码:

     1 //#include "bits/stdc++.h"
     2 #include "cstdio"
     3 #include "map"
     4 #include "set"
     5 #include "cmath"
     6 #include "queue"
     7 #include "vector"
     8 #include "string"
     9 #include "cstring"
    10 #include "time.h"
    11 #include "iostream"
    12 #include "stdlib.h"
    13 #include "algorithm"
    14 #define db double
    15 #define ll long long
    16 //#define vec vector<ll>
    17 #define Mt  vector<vec>
    18 #define ci(x) scanf("%d",&x)
    19 #define cd(x) scanf("%lf",&x)
    20 #define cl(x) scanf("%lld",&x)
    21 #define pi(x) printf("%d
    ",x)
    22 #define pd(x) printf("%f
    ",x)
    23 #define pl(x) printf("%lld
    ",x)
    24 #define rep(i, x, y) for(int i=x;i<=y;i++)
    25 const int N   = 1e3 + 9;
    26 const int mod = 1e9 + 7;
    27 const int MOD = mod - 1;
    28 const db  eps = 1e-10;
    29 const db  PI  = acos(-1.0);
    30 using namespace std;
    31 
    32 int f[N],a[N],C[N][N];
    33 int n,m,ans,tot;
    34 char str[N];
    35 
    36 int calc(int x)
    37 {
    38     int res=0;
    39     while(x){if(x&1)res++;x>>=1;}
    40     return res;
    41 }
    42 
    43 void work(int x)
    44 {
    45     int cnt=0;
    46     for(int i=n;i>=1;i--)//x个1放在n个位置上
    47     {
    48         if(a[i])//当原数字对应位为1时,可以放1与0。
    49         {
    50             if(x>=cnt) ans=(ans+C[i-1][x-cnt])%mod;// 此位不放1的种数
    51             cnt++;//之后此位放1
    52         }
    53     }
    54     if(x==tot) ans=(ans+1)%mod;
    55 }
    56 
    57 int main()
    58 {
    59     scanf("%s%d",str+1,&m);n=strlen(str+1);
    60     for(int i=1;i<=n;i++) a[i]=str[i]-'0',tot+=a[i];
    61     reverse(a+1,a+1+n);
    62     for(int i=0;i<=n;i++)
    63     {
    64         C[i][0]=1;
    65         for(int j=1;j<=i;j++) C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;
    66     }
    67     f[1]=0;
    68     for(int i=2;i<=1000;i++) f[i]=f[calc(i)]+1;
    69     if(m==0) puts("1");
    70     else
    71     {
    72         for(int i=1;i<=1000;i++) if(f[i]==m-1) work(i);
    73         if(m==1) ans--;
    74         printf("%d
    ",(ans+mod)%mod);
    75     }
    76     return 0;
    77 }
  • 相关阅读:
    IM的扫码登录功能如何实现?一文搞懂主流的扫码登录技术原理
    IM“扫一扫”功能很好做?看看微信“扫一扫识物”的完整技术实现
    2020年了,Android后台保活还有戏吗?看我如何优雅的实现!
    P2P技术详解(三):P2P中的NAT穿越(打洞)方案详解(进阶分析篇)
    微信团队分享:极致优化,iOS版微信编译速度3倍提升的实践总结
    史上最通俗,彻底搞懂字符乱码问题的本质
    你知道,HTTPS用的是对称加密还是非对称加密?
    IM开发基础知识补课(七):主流移动端账号登录方式的原理及设计思路
    面视必备,史上最通俗计算机网络分层详解
    阿里钉钉技术分享:企业级IM王者——钉钉在后端架构上的过人之处
  • 原文地址:https://www.cnblogs.com/mj-liylho/p/8338353.html
Copyright © 2011-2022 走看看