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 }
  • 相关阅读:
    spring-boot启动debug信息中non-fatal error解决
    is present but cannot be translated into a null value due to being declared as a primitive type
    mysql联合索引
    Spring RestTemplate 专题
    Android Studio开发入门-引用jar及so文件
    它们偷偷干了啥?教你监督APP的运行
    解决Android中No resource found that matches android:TextAppearance.Material.Widget.Button.Inverse问题
    No resource found that matches the given name 'android:WindowTitle'
    Android studio:library module依赖篇
    com.android.support:appcompat-v7:21.+
  • 原文地址:https://www.cnblogs.com/mj-liylho/p/8338353.html
Copyright © 2011-2022 走看看