递推DP URAL 1081 Binary Lexicographic Sequence

 

题目传送门

 1 /*  2  dp[i][1]/dp[i][0] 表示从左往右前i个,当前第i个放1或0的方案数  3  k -= dp[n][0] 表示当前放0的方案数不够了,所以必须放1,那么dp[n][0]个方案数都不能用了  4  相当于k减去这么多  5  详细解释:http://www.cnblogs.com/scau20110726/archive/2013/02/05/2892587.html  6 */  7 #include <cstdio>  8 #include <algorithm>  9 #include <cmath> 10 #include <cstring> 11 #include <string> 12 using namespace std; 13 14 const int MAXN = 1e4 + 10; 15 const int INF = 0x3f3f3f3f; 16 int dp[50][2]; 17 18 void solve(void) 19 { 20 memset (dp, 0, sizeof (dp)); 21 dp[1][1] = 1; dp[1][0] = 1; 22 for (int i=2; i<44; ++i) 23  { 24 dp[i][1] = dp[i-1][0]; 25 dp[i][0] = dp[i-1][1] + dp[i-1][0]; 26  } 27 } 28 29 int main(void) //URAL 1081 Binary Lexicographic Sequence 30 { 31 //freopen ("Q.in", "r", stdin); 32 33  solve (); 34 int n, k; 35 while (scanf ("%d%d", &n, &k) == 2) 36  { 37 if (k > dp[n][1] + dp[n][0]) {puts ("-1"); continue;} 38 while (n) 39  { 40 if (dp[n][0] >= k) 41  { 42 printf ("0"); 43  } 44 else {printf ("1"); k -= dp[n][0];} 45 n--; 46  } 47 puts (""); 48  } 49 50 return 0; 51 }

 

你可能感兴趣的:(sequence)