Guest User

FUNKVAL - SLOW

a guest
Mar 31st, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define check(N, A, B) assert((N) >= (A) && (N) <= (B))
  3. using namespace std;
  4. const int maxn = 30;
  5. typedef long long ll;
  6. int a[maxn][maxn];
  7. int floor(int x){
  8.     int l = 0, r = x;
  9.     while(l < r){
  10.         int m = l+(r-l)/2;
  11.         if(m*m <= x) l = m+1;
  12.         else r = m;
  13.     }
  14.     if(l*l > x) l--;
  15.     return l;
  16. }
  17. int main(){
  18.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  19.    
  20.     int n, k; cin >> n;
  21.     check(n,1,25);
  22.     for(int i=0;i<n;i++)
  23.         for(int j=0;j<n;j++){
  24.             cin >> a[i][j]; check(a[i][j],0,1e6);
  25.         }
  26.     for(int i=0;i<n;i++){
  27.         assert(a[i][i] == 0);
  28.         for(int j=0;j<n;j++)
  29.             assert(a[i][j] == a[j][i]);
  30.     }
  31.  
  32.     ll ans = 0;
  33.     for(int i=0;i<(1<<n);i++){
  34.         ll cur = 0;
  35.         for(int j=0;j<n;j++)
  36.             if(i&(1<<j)) for(int k=0;k<j;k++)
  37.                 if(i&(1<<k)) cur += a[j][k];
  38.         // cout << i << ": " << cur << endl;
  39.         ans += cur * floor(__builtin_popcount(i));
  40.     }
  41.     cout << ans << "\n";
  42. }
Add Comment
Please, Sign In to add comment