Advertisement
Korotkodul

СПЬГУ_J_2

Dec 29th, 2021 (edited)
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 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. ll n;
  20. ll ans = 0;
  21. ll inf = 1e9 + 7;
  22. string two(ll 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. ll zer(ll x, ll id){
  36.     string s = two(x);
  37.     //cout<<"s = "<<s<<'\n';
  38.     id = n - id - 1;
  39.     //cout<<"id = "<<id<<'\n';
  40.     ll 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. ll var(ll msk, ll num){
  48.     num %= inf;
  49.     //cout<<"msk = "<<two(msk)<<"\n";
  50.     //cout<<"num = "<<num<<"\n";
  51.     if (msk == (1 << n) - 1) return num;
  52.     ll pl=0, now,z, add;
  53.     for (ll 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.         now %= inf;
  63.         //cout<<"now = "<<now<<"\n";
  64.         add = var(msk | (1 << i), num * now) % inf;
  65.         add %= inf;
  66.         pl += add;
  67.         pl %= inf;
  68.     }
  69.     //cout<<"\n";
  70.     return pl;
  71.     //cout<<"\n";
  72. }
  73.  
  74.  
  75. int main()
  76. {
  77.     /*ios::sync_with_stdio(0);
  78.     cin.tie(0);
  79.     cout.tie(0);*/
  80.     cin>>n;
  81.     ans = var(0, 1) % inf;
  82.     ans %= inf;
  83.     //cout<<"ans = "<<ans<<"\n";
  84.     cout<<ans<<'\n';
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement