Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <algorithm>
- #include <vector>
- #include <string>
- #include <math.h>
- #define ll long long
- using namespace std;
- //freopen("a.in", "r", stdin);
- //freopen("a.out", "w", stdout);
- string toBinary(int x){
- string res = "";
- while(x > 0){
- res += to_string(x % 2);
- x /= 2;
- }
- reverse(res.begin(), res.end());
- return res;
- }
- int toDecimal(string s){
- int a = 0;
- int p;
- for(int i = 0; i < s.length(); i++){
- if(s[i] == '1'){
- p = 1;
- for(int j = 0; j < s.length() - i - 1; j++){
- p *= 2;
- }
- a += p;
- }
- }
- return a;
- }
- int ranks (vector<int> a) {
- int rank = 0, p;
- int k = a.size();
- for(int i = 1; i <= k; i++){
- p = 1;
- for(int j = i + 1; j < a[i-1]; j++){
- p *= j;
- }
- for(int j = 1; j < a[i-1] - i; j++) {
- p = floor((double)p/ (double)j);
- }
- if(a[i-1] == 1)
- p = 0;
- rank += p;
- }
- rank++;
- return rank;
- }
- vector<int> unranks (int k, int rank){
- int j = rank - 1, p;
- int ip;
- vector<int> a(k);
- for(int i = k; i >= 1; i--){
- ip = i - 1;
- p = 1;
- while(true){
- ip++;
- if(ip != i)
- p = floor((double)(ip*p) / (double)(ip - i));
- if (j < p)
- break;
- }
- if (ip != i)
- p = floor((double)p*(ip - i) / (double)ip);
- j -= p;
- a[i-1] = ip;
- }
- return a;
- }
- void code(){
- string s;
- string res;
- char c;
- vector<int> a;
- int now;
- for(int i = 0; i < 51; i++){
- now = 51*i;
- for(int j = 0; j < 100; j++){
- cin >> c;
- if(c == '1')
- a.push_back(now + j + 1);
- }
- }
- int r = ranks(a);
- s = toBinary(int(a.size()));
- for(int i = 0; i < 13 - s.length(); i++){
- res += '0';
- }
- res += s;
- s = toBinary(r);
- res += s;
- cout << res;
- }
- void decode(){
- string s, t;
- cin >> s;
- int u;
- for(int i = 0; i < 13; i++){
- if(s[i] == '1'){
- u = i;
- break;
- }
- }
- for(int i = u; i < 13; i++) {
- t += s[i];
- }
- int k = toDecimal(t);
- t = s.substr(13, s.length() - 13);
- int r = toDecimal(t);
- vector<int> res = unranks(k, r);
- int now = 0;
- for(int i = 0; i < 51; i++){
- for(int j = 1; j <= 100; j++) {
- if(res[now] == j){
- cout << '1';
- now++;
- }
- else
- cout << '0';
- }
- cout << '\n';
- }
- }
- int main()
- {
- ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
- int t;
- cin >> t;
- if(t == 0)
- code();
- else if(t == 1)
- decode();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement