Stringsobits
Kim Schrijvers

Consider an ordered set S of strings of N (1 <= N <= 31) bits. Bits, of course, are either 0 or 1.

This set of strings is interesting because it is ordered and contains all possible strings of length N that have L (1 <= L <= N) or fewer bits that are `1'.

Your task is to read a number I (1 <= I <= sizeof(S)) from the input and print the Ith element of the ordered set for N bits with no more than L bits that are `1'.

PROGRAM NAME: kimbits

INPUT FORMAT

A single line with three space separated integers: N, L, and I.

SAMPLE INPUT (file kimbits.in)

5 3 19

OUTPUT FORMAT

A single line containing the integer that represents the Ith element from the order set, as described.

SAMPLE OUTPUT (file kimbits.out)

10011
思路:早就想到了是组合数,但是请注意两点:第一:前导0的问题,这个东西我们提前预处理一下f[i]=f[i+1]+c(l-cnt,n-i+1);
然后就是枚举放1的位置,如果当前计算出的组合数>要求的,则这个位置肯定放1,然后将pos移至末尾,最后当x=1或0时便跳出循环即可!
Executing...
   Test 1: TEST OK [0.000 secs, 3236 KB]
   Test 2: TEST OK [0.000 secs, 3236 KB]
   Test 3: TEST OK [0.000 secs, 3236 KB]
   Test 4: TEST OK [0.000 secs, 3236 KB]
   Test 5: TEST OK [0.000 secs, 3236 KB]
   Test 6: TEST OK [0.000 secs, 3236 KB]
   Test 7: TEST OK [0.000 secs, 3236 KB]
   Test 8: TEST OK [0.000 secs, 3236 KB]
   Test 9: TEST OK [0.000 secs, 3236 KB]
   Test 10: TEST OK [0.000 secs, 3236 KB]
   Test 11: TEST OK [0.000 secs, 3236 KB]
   Test 12: TEST OK [0.000 secs, 3236 KB]
   Test 13: TEST OK [0.000 secs, 3236 KB]

All tests OK.
  1 /*
  2 ID:wuhuaju2
  3 PROG:kimbits
  4 LANG:C++
  5 */
  6 #include <cstdio>
  7 #include <iostream>
  8 #include <cstdlib>
  9 #include <algorithm>
 10 #include <cstring>
 11 #include <string>
 12 using namespace std;
 13 
 14 long long a[40],b[40],p[40];
 15      long long c[35][35];
 16 long long n,l,x,pos,t,cnt;
 17 
 18 void close()
 19 {
 20     fclose(stdin);
 21     fclose(stdout);
 22     exit(0);
 23 }
 24 
 25 long long cal(long long k)
 26 {
 27     long long tt=1;
 28         for (int i=1;i<=l-cnt;i++)
 29     {
 30         tt+=c[k][i];
 31     }
 32     return tt;
 33 }
 34 
 35 void work()
 36 {
 37     long long t=1;
 38     for (int i=1;i<=n;i++)
 39     {
 40         t=1;
 41         for (int j=1;j<=i;j++)
 42         {
 43             c[i][j]=t*(i-j+1)/j;
 44             t=c[i][j];
 45         }
 46     }
 47        p[n]=2;
 48        for (int i=1;i<=n-1;i++)
 49        {
 50             t=1;
 51             for (int j=1;j<=l-1;j++)
 52                 t+=c[i][j];
 53            p[n-i]=p[n-i+1]+t;
 54         }
 55         for (int i=1;i<=n;i++)
 56         {
 57    //       cout<<"i:"<<i<<" p:"<<p[i]<<endl;
 58         }
 59 }
 60 
 61 
 62 void init ()
 63 {
 64 freopen("kimbits.in","r",stdin);
 65 freopen("kimbits.out","w",stdout);
 66   // scanf("%d %d %d",&n,&l,&x);
 67   cin>>n>>l>>x;
 68     work();
 69     memset(a,0,sizeof(a));
 70     pos=n;
 71     p[n+1]=2;
 72     for (int i=pos;i>=1;i--)
 73     {
 74         if (i==pos && p[i]>x)
 75         {
 76             x=0;
 77             break;
 78         }
 79         if (p[i]>=x)
 80         {
 81             x-=p[i+1];
 82             a[i]=1;
 83             break;
 84         }
 85     }
 86     cnt=1;
 87     while (x!=1 && x!=0)
 88     {
 89         t=cal(n-pos+1);
 90         if (x<=t)
 91         {
 92             x-=cal(n-pos);
 93             a[pos]=1;
 94             pos=n;
 95             cnt++;
 96             if (x==1)
 97                 break;
 98         }
 99         else
100             pos--;
101     }
102     for (int i=1;i<=n;i++)
103         cout<<a[i];
104     printf("\n");
105 }
106 
107 
108 int main ()
109 {
110     init();
111     work();
112     close();
113     return 0;
114 }

 


posted on 2013-02-12 22:06  cssystem  阅读(322)  评论(0编辑  收藏  举报