Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <string>
  6. #include <math.h>
  7. #define ll long long
  8. using namespace std;
  9. //freopen("a.in", "r", stdin);
  10. //freopen("a.out", "w", stdout);
  11.  
  12.  
  13. string toBinary(int x){
  14. string res = "";
  15. while(x > 0){
  16. res += to_string(x % 2);
  17. x /= 2;
  18. }
  19. reverse(res.begin(), res.end());
  20. return res;
  21. }
  22.  
  23. int toDecimal(string s){
  24. int a = 0;
  25. int p;
  26. for(int i = 0; i < s.length(); i++){
  27. if(s[i] == '1'){
  28. p = 1;
  29. for(int j = 0; j < s.length() - i - 1; j++){
  30. p *= 2;
  31. }
  32. a += p;
  33. }
  34. }
  35. return a;
  36. }
  37.  
  38. int ranks (vector<int> a) {
  39. int rank = 0, p;
  40. int k = a.size();
  41. for(int i = 1; i <= k; i++){
  42. p = 1;
  43. for(int j = i + 1; j < a[i-1]; j++){
  44. p *= j;
  45. }
  46. for(int j = 1; j < a[i-1] - i; j++) {
  47. p = floor((double)p/ (double)j);
  48. }
  49. if(a[i-1] == 1)
  50. p = 0;
  51. rank += p;
  52. }
  53. rank++;
  54. return rank;
  55. }
  56.  
  57. vector<int> unranks (int k, int rank){
  58. int j = rank - 1, p;
  59. int ip;
  60. vector<int> a(k);
  61. for(int i = k; i >= 1; i--){
  62. ip = i - 1;
  63. p = 1;
  64. while(true){
  65. ip++;
  66. if(ip != i)
  67. p = floor((double)(ip*p) / (double)(ip - i));
  68. if (j < p)
  69. break;
  70. }
  71. if (ip != i)
  72. p = floor((double)p*(ip - i) / (double)ip);
  73. j -= p;
  74. a[i-1] = ip;
  75. }
  76. return a;
  77. }
  78.  
  79. void code(){
  80. string s;
  81. string res;
  82. char c;
  83. vector<int> a;
  84. int now;
  85. for(int i = 0; i < 51; i++){
  86. now = 51*i;
  87. for(int j = 0; j < 100; j++){
  88. cin >> c;
  89. if(c == '1')
  90. a.push_back(now + j + 1);
  91. }
  92. }
  93. int r = ranks(a);
  94. s = toBinary(int(a.size()));
  95. for(int i = 0; i < 13 - s.length(); i++){
  96. res += '0';
  97. }
  98. res += s;
  99. s = toBinary(r);
  100. res += s;
  101. cout << res;
  102. }
  103.  
  104. void decode(){
  105. string s, t;
  106. cin >> s;
  107. int u;
  108. for(int i = 0; i < 13; i++){
  109. if(s[i] == '1'){
  110. u = i;
  111. break;
  112. }
  113. }
  114. for(int i = u; i < 13; i++) {
  115. t += s[i];
  116. }
  117. int k = toDecimal(t);
  118. t = s.substr(13, s.length() - 13);
  119. int r = toDecimal(t);
  120. vector<int> res = unranks(k, r);
  121. int now = 0;
  122. for(int i = 0; i < 51; i++){
  123. for(int j = 1; j <= 100; j++) {
  124. if(res[now] == j){
  125. cout << '1';
  126. now++;
  127. }
  128. else
  129. cout << '0';
  130. }
  131. cout << '\n';
  132. }
  133. }
  134.  
  135. int main()
  136. {
  137. ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  138. int t;
  139. cin >> t;
  140. if(t == 0)
  141. code();
  142. else if(t == 1)
  143. decode();
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement