USACO 3.2 Stringsobits

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

———————————————————————————————————————————————

把组合数求出来处理成前缀和

我们发现当I大于一段k长所有情况总和时,那么k+1位一定是1

那么这道题就做完了

其实最坑的一点,sizeof(S)=2147483648

…………………………

 

 1 /*
 2 ID: ivorysi
 3 PROG: kimbits
 4 LANG: C++
 5 */
 6 #include <iostream>
 7 #include <cstdio>
 8 #include <cstring>
 9 #include <algorithm>
10 #include <queue>
11 #include <set>
12 #include <vector>
13 #define siji(i,x,y) for(int i=(x);i<=(y);++i)
14 #define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
15 #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
16 #define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
17 #define inf 0x7fffffff
18 #define MAXN 400005
19 #define ivorysi
20 #define mo 97797977
21 #define ha 974711
22 #define ba 47
23 #define fi first
24 #define se second
25 //#define pis pair<int,string>
26 using namespace std;
27 typedef long long ll;
28 int c[40][40];
29 int n,l;
30 ll k;
31 int num[40];
32 void solve() {
33     scanf("%d%d%lld",&n,&l,&k);
34     siji(i,0,n) c[i][0]=1;
35     siji(i,1,n)    {
36         int z=min(l,i);
37         siji(j,1,z) {
38             c[i][j]=c[i-1][j-1]+c[i-1][j];
39         }
40     }
41     siji(i,0,n) siji(j,1,l) {
42         c[i][j]+=c[i][j-1];
43     }
44     int cnt=l;
45     gongzi(i,n-1,0) {
46         if(c[i][cnt]<k) {k=k-c[i][cnt];num[i+1]=1;--cnt;}
47     }
48     gongzi(i,n,1) {
49         printf("%c",num[i]+'0');
50     }
51     puts("");
52 }
53 int main(int argc, char const *argv[])
54 {
55 #ifdef ivorysi
56     freopen("kimbits.in","r",stdin);
57     freopen("kimbits.out","w",stdout);
58 #else
59     freopen("f1.in","r",stdin);
60 #endif
61     solve();
62 }

 

posted @ 2016-12-04 20:43  sigongzi  阅读(476)  评论(0编辑  收藏  举报