Korotkodul

СПБГУ_J_1

Dec 29th, 2021 (edited)
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int,int>
  11. using namespace std;
  12. using ll = long long;
  13. using ld = long double;
  14. void cv(vector <int> &v){
  15.     for (auto x: v) cout<<x<<' ';
  16.     cout<<"\n";
  17. }
  18.  
  19. int n;
  20. int ans = 0;
  21. int inf = 1e9 + 7;
  22. string two(int x){
  23.     string r = "";
  24.     while (x > 0){
  25.         r += x % 2 + 48;
  26.         x /= 2;
  27.     }
  28.     reverse(r.begin(), r.end());
  29.     while (r.size() < n){
  30.         r = '0' + r;
  31.     }
  32.     return r;
  33. }
  34.  
  35. int zer(int x, int id){
  36.     string s = two(x);
  37.     //cout<<"s = "<<s<<'\n';
  38.     id = n - id - 1;
  39.     //cout<<"id = "<<id<<'\n';
  40.     int r = 0;
  41.     for (int i = 0; i < id;++i){//cout<<"i = "<<i<<' ';
  42.         if (s[i] == '0')r++;
  43.     }//cout<<'\n';
  44.     return r;
  45. }
  46.  
  47. int var(int msk, int num){
  48.  
  49.     //cout<<"msk = "<<two(msk)<<"\n";
  50.     //cout<<"num = "<<num<<"\n";
  51.     if (msk == (1 << n) - 1) return num;
  52.     int pl=0, now,z;
  53.     for (int i = 0; i < n; ++i){
  54.         //cout<<"i = "<<i<<'\n';
  55.         if ( (msk & (1<<i)) != 0) {
  56.                 //cout<<"skip\n";
  57.                 continue;
  58.         }
  59.         z = zer(msk | (1<<i), i);
  60.         //cout<<"z = "<<z<<"\n";
  61.         now = pow(2, n - z - 1);
  62.         //cout<<"now = "<<now<<"\n";
  63.         pl += var(msk | (1 << i), num * now);
  64.     }
  65.     //cout<<"\n";
  66.     return pl;
  67.     //cout<<"\n";
  68. }
  69.  
  70.  
  71. int main()
  72. {
  73.     /*ios::sync_with_stdio(0);
  74.     cin.tie(0);
  75.     cout.tie(0);*/
  76.     cin>>n;
  77.     int msk = 0;
  78.     ans = var(0, 1);
  79.     cout<<"ans = "<<ans<<"\n";
  80. }
  81.  
Add Comment
Please, Sign In to add comment