现在的位置: 首页 > 综合 > 正文

NEU 1013 bits 动态规划

2013年10月03日 ⁄ 综合 ⁄ 共 2481字 ⁄ 字号 评论关闭

题目描述:

Rory 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'.

输入要求

There're multiple test cases.
Each testcase is a single line with three space separated integers: N, L, and I.

输出要求

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

假如输入

5 3 19

应当输出

10011

翻译一下:

中文意思就是:
给定N,L,I,找出所有长度为N,最多含有L个1,第I大的二进制串(允许类似00000之类的有前导0的串) 

样例解读
00000  -> 1
00001  -> 2
00010  -> 3
00011  -> 4
00100  -> 5
00101  -> 6
00110  -> 7
00111  -> 8
01000  -> 9
01001  -> 10
01010  -> 11
01011  -> 12
01100  -> 13
01101  -> 14
01110  -> 15
01111 这是不行的,超出N位了!
10000  -> 16
10001  -> 17
10010  -> 18
10011  -> 19  答案!

i表示字串长度,j表示最多含有的1的个数,dp[i][j]表示所含有的排列总数
初始化 dp[i][0]=1,dp[0][j]=1;
如此理解。 字串添加的要么是0,要么是1.
当添加是0的时候。 为dp[i-1][j]
当添加是1的时候。 为dp[i-1][j-1]
所以dp[i][j] = dp[i-1][j]+dp[i-1][j-1];

输出时即判断需不需要输出1.

    if(t > dp[i-1][l]) 输出1; 因为 当t>dp[i-1][j]这种添加0的情况时。你就知道它必定包含部分dp[i-1][j-1]中的数字。但应当知道 t <= dp[i-1][j]+dp[i-1][j-1];
所以此题可解

需要注意的是。。如果输出了1之后。要把l--掉。。我当时没注意,wa这里了。。还有这个最大总数是2^32-1。你需要开long long 或者 unsigned int 或者 unsigned long。不然,会错!

附上我的代码:

  1. /* 
  2.  * @user ipqhjjybj 
  3.  * @Time 
  4.  * @data 20130629 
  5.  */  
  6. #include <cstdio>  
  7. #include <cmath>  
  8. #include <cstdlib>  
  9. #include <ctime>  
  10.   
  11. #include <iostream>  
  12. #include <cmath>  
  13. #include <algorithm>  
  14. #include <numeric>  
  15. #include <utility>  
  16.   
  17. #include <cstring>  
  18. #include <vector>  
  19. #include <stack>  
  20. #include <queue>  
  21. #include <map>  
  22. #include <string>  
  23. using namespace std;  
  24.   
  25. #define inf 0x3f3f3f3f  
  26. #define MAXN 100  
  27. #define MAXX 10010  
  28. #define clr(x,k) memset((x),(k),sizeof(x))  
  29. #define clrn(x,k) memset((x),(k),(n+1)*sizeof(int))  
  30. #define cpy(x,k) memcpy((x),(k),sizeof(x))  
  31. #define Base 10000  
  32.   
  33. typedef vector<int> vi;  
  34.   
  35. #define foreach(it,c) for(vi::iterator it = (c).begin();it != (c).end();++it)  
  36.   
  37. #define max(a,b) ((a)>(b)?(a):(b))  
  38. #define min(a,b) ((a)<(b)?(a):(b))  
  39. long long dp[MAXN][MAXN];  
  40. int main(){  
  41.     long long I;  
  42.     int n,l,i,j;  
  43.     clr(dp,0);  
  44.     for(i = 0;i < MAXN;i++) dp[i][0]=dp[0][i]=1;  
  45.     for(i = 1;i < MAXN;i++)  
  46.         for(j=1;j < MAXN;j++)  
  47.             dp[i][j]=dp[i-1][j-1]+dp[i-1][j];  
  48.     while(scanf("%d %d %lld",&n,&l,&I)!=EOF){  
  49.         while(n--){  
  50.             if(I>dp[n][l])  
  51.                 printf("1"),I-=dp[n][l],l--;  
  52.             else printf("0");  
  53.         }  
  54.         printf("\n");  
  55.     }  
  56.     return 0;  
  57. }  

抱歉!评论已关闭.