usaco Chapter 3 section 3.2 Stringsobits

本文介绍了一种算法,该算法用于在一个有序的位串集合中查找特定元素。位串长度固定为N,且每个位串包含不超过L个1。通过输入一个索引I,程序能够返回集合中第I个元素的具体值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
ID: niepeng1
LANG: C++
TASK:kimbits
*/
/*
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'.

*/
#include<iostream>
using namespace std;


int main()
{
 unsigned long int map[33][33];
 unsigned long int N,L,I;
 int i,j;
 freopen("kimbits.in","r",stdin);
 freopen("kimbits.out","w",stdout);
 scanf("%lu %lu %lu",&N,&L,&I);
 for(i=0;i<=N;i++)
 {
  map[0][i]=1;
  map[i][0]=1;
 }
 for(i=1;i<=N;i++)
  for(j=1;j<=N;j++)
  {
   if(j>i)
    map[i][j]=map[i][i];
   else
    map[i][j]=map[i-1][j]+map[i-1][j-1];
  }
 for(i=N-1;i>=0;i--)
 {
  if(map[i][L] < I)
  {
   printf("1");
   I-=map[i][L];
   L--;
  }
  else
   printf("0");
 }
 printf("/n");
 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值