zoukankan      html  css  js  c++  java
  • Stringsobits chapter 3.2

    跪跪跪,第三章没做几题,已经连跪几天了.好桑心,不过能咋办,不会就看别人的学啊.

    同样是暴力搜,别人的剪枝实在是不能太叼.

      1 /*
      2 
      3 ID: hubiao cave
      4 
      5 PROG: kimbits
      6 
      7 LANG: C++
      8 
      9 */
     10 
     11 
     12 
     13 
     14 #include<iostream>
     15 
     16 #include<fstream>
     17 
     18 #include<string>
     19 
     20 using namespace std;
     21 
     22 
     23 
     24 int GetOne(int);
     25  unsigned int N,L,num;
     26 int main()
     27 
     28 {
     29 
     30     ifstream fin("kimbits.in");
     31     ofstream fout("kimbits.out");
     32     fin>>N>>L>>num;
     33     unsigned int counter=0;
     34     unsigned int bnum=0,left;
     35     unsigned int i=0;
     36     while(i<(unsigned int )1<<N)
     37     {
     38         
     39         bnum=GetOne(i);
     40         if(bnum<=L)
     41         {
     42             counter++;
     43         left=L-bnum;
     44         if(!(i&(1LL<<left)-1))
     45         {
     46         if(counter+(1LL<<left)-1<num)
     47         {
     48             counter+=(1LL<<left)-1;
     49             i+=(1LL<<left)-1;
     50         }
     51         else
     52         {
     53                 i=i+num-counter;
     54                 int p=N-1;
     55                 while(p>=0)
     56                 {
     57                 fout<<(i>>p&1);
     58                 p--;
     59                 }
     60                 fout<<endl;
     61                  return 0;
     62         }
     63         }
     64         }
     65         else
     66         {
     67             i+=(i&(i^(i-1)))-1;
     68         }
     69 
     70         if(counter==L)
     71         {
     72                 int p=N-1;
     73     while(p>=0)
     74     {
     75         fout<<(i>>p&1);
     76         p--;
     77     }
     78     fout<<endl;
     79     return 0;
     80 
     81         }
     82         i++;
     83     }
     84 
     85     int p=N-1;
     86     while(p>=0)
     87     {
     88         fout<<(i>>p&1);
     89         p--;
     90     }
     91     fout<<endl;
     92     return 0;
     93 
     94 
     95 }
     96 
     97 
     98 
     99 int GetOne(int i)
    100 {
    101     int counter=0;
    102     while(i)
    103     {
    104         counter++;
    105         i=i&(i-1);
    106     }
    107     return counter;
    108 }
  • 相关阅读:
    c3p0配置
    0624软件工程的回顾和总结
    0619学习进度条
    MySQL中wait_timeout的坑
    js/jquery禁止页面回退
    jquery打印页面(jquery.jqprint)
    input file multiple 批量上传文件
    Python学习笔记——Python Number(数字)
    正则表达式
    Python学习笔记(三)——条件语句、循环语句
  • 原文地址:https://www.cnblogs.com/cavehubiao/p/3346259.html
Copyright © 2011-2022 走看看