Advertisement
Ritam_C

AND Sequences

Apr 21st, 2021
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define ull unsigned long long
  4. #define ld long double
  5. #define vi vector<int>
  6. #define vll vector<long long>
  7. #define pb push_back
  8. #define p_b pop_back
  9. #define mod (ll)1000000007
  10. using namespace std;
  11.  
  12. string bin(ll x){
  13.     string t;
  14.     while(x > 0){
  15.         t = to_string(x%2)+t;
  16.         x /= 2;
  17.     }
  18.     int l = t.length();
  19.     for(int i = 0; i < 31-l; i++){
  20.         t = "0"+t;
  21.     }
  22.     return t;
  23. }
  24.  
  25. ll fact(int n, int r){
  26.     ll k = 1;
  27.     for(int i = n-r+1; i <= n; i++){
  28.         k = ((k%mod)*(i)%mod)%mod;
  29.     }
  30.     return k;
  31. }
  32.  
  33. int main(){
  34.     ios_base::sync_with_stdio(false);
  35.     cin.tie(NULL);
  36.     int t;
  37.     cin >> t;
  38.     while(t--){
  39.         int n;
  40.         cin >> n;
  41.         vector<string> v;
  42.         for(int i = 0; i < n; i++){
  43.             ll x;
  44.             cin >> x;
  45.             v.pb(bin(x));
  46.         }
  47.         int count = 0;
  48.        
  49.         bool dp[n][31] = {0};
  50.         for(int i = 0; i < n; i++){
  51.             if(v[i][0] == '0'){
  52.                 dp[i][0] = 1;
  53.                 count++;
  54.             }
  55.         }
  56.  
  57.         if(count == 1){
  58.             cout << "0\n";
  59.         } else{
  60.             bool f = true;
  61.             if(count == 0){
  62.                 for(int i = 0; i < n; i++){
  63.                     dp[i][0] = 1;
  64.                 }
  65.             }
  66.             for(int j = 1; j < 31; j++){
  67.                 int count1 = 0;
  68.                 for(int i = 0; i < n; i++){
  69.                     if(v[i][j] == '0' && dp[i][j-1] == 1){
  70.                         count1++;
  71.                         dp[i][j] = 1;
  72.                     }
  73.                 }
  74.                 if(count1 == 0){
  75.                     for(int i = 0; i < n; i++){
  76.                         dp[i][j] = dp[i][j-1];
  77.                         if(dp[i][j-1] == 1){
  78.                             count1++;
  79.                         }
  80.                     }
  81.                 }
  82.                 if(count1 != 1){
  83.                     count = count1;
  84.                 } else{
  85.                     f = false;
  86.                     break;
  87.                 }
  88.             }
  89.             if(f){
  90.                 if(count == 0){
  91.                     cout << fact(n, n)%mod << "\n";
  92.                 } else{
  93.                     cout << ((fact(n-2, n-count)%mod)*(fact(count, count)%mod))%mod << "\n";
  94.                 }
  95.             } else{
  96.                 cout << "0\n";
  97.             }
  98.         }
  99.     }
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement