1. 程式人生 > >Binary Lexicographic Sequence URAL - 1081 (有關數的排列)

Binary Lexicographic Sequence URAL - 1081 (有關數的排列)

esp using ring namespace stream algo char while enc

 

 1 #include<iostream>
 2 #include<vector>
 3 #include<algorithm>
 4 #include<map>
 5 #include<set>
 6 #include<string>
 7 using namespace std;
 8 typedef unsigned long long LL;
 9 const int maxn = 46;
10 LL dp[maxn][2];
11 
12 void init()
13 {
14     dp[1][0] = dp[1
][1] = 1; 15 for (int i = 2; i < maxn; i++) { 16 dp[i][0] = dp[i - 1][0] + dp[i - 1][1]; 17 dp[i][1] = dp[i - 1][0]; 18 } 19 } 20 21 void solve(int n, int k) 22 { 23 if (k > dp[n][0] + dp[n][1]) { //若k大於所有可能排列數 24 cout << "-1" << endl; 25 return
; 26 } 27 vector<char> ans; 28 for (int i = n; i >= 1; i--) { // 29 if (dp[i][0] >= k) cout << "0"; 30 else { 31 k -= dp[i][0]; 32 cout << "1"; 33 } 34 } 35 cout << endl; 36 } 37 38 int main() 39 { 40 init();
41 int n, k; 42 while (cin >> n >> k) 43 solve(n, k); 44 }

Binary Lexicographic Sequence URAL - 1081 (有關數的排列)