USACO-Section 3.2-PROB 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
 
 
用组合数缩小范围
比如对于样例中的10000,小于它的有C43+C42+C41+C04个,(右边4个0,选几个变成1)
从左往右一个一个确定是不是1(根据范围,若比它小的数的个数小于题目所求,则这一位为1)
注意取一个1后,L--

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#define name "kimbits"
using namespace std;
int C[32][32];
long long  n,l,x;
int get(int a,int b)
{
	int ret=0;
	for (int i=0;i<=b;i++)
	ret+=C[a][i];
	return ret;
}
int main()
{
	freopen(name ".in","r",stdin);
	freopen(name ".out","w",stdout);
	cin>>n>>l>>x;
	int i,j,tmp;
	C[1][0]=C[0][0]=C[1][1]=1;
	for (i=2;i<=n;i++)
	  for (j=0;j<=i;j++)
	  C[i][j]=C[i-1][j]+C[i-1][j-1];	
	if (x==1) {for (i=1;i<=n;i++)cout<<"0"; cout<<endl;return 0;}
	for (i=1;i<=n;i++)
	{
		tmp=get(i-1,l);
		if (tmp>=x) {i--;break;}
	}
	if (i==n+1) i=n;
	tmp=get(i-1,l);
	l--;
	for (j=i+1;j<=n;j++) cout<<"0";
	cout<<"1";
	for (j=i-1;j>=1;j--)
	{
		int g=get(j-1,l);
		if (tmp+g>=x) cout<<"0";
		else {cout<<"1";l--;tmp+=g;}
	}
	cout<<endl;
	return 0;
}


发布了20 篇原创文章 · 获赞 0 · 访问量 5208

猜你喜欢

转载自blog.csdn.net/SoYouTry/article/details/50593819
今日推荐