7oSkaaa

J - Bugs Couples

Jul 12th, 2022 (edited)
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.26 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define cin_2d(vec, n, m) for(int i = 0; i < n; i++) for(int j = 0; j < m && cin >> vec[i][j]; j++);
  6. #define cout_2d(vec, n, m) for(int i = 0; i < n; i++, cout << "\n") for(int j = 0; j < m && cout << vec[i][j] << " "; j++);
  7. #define cout_map(mp) for(auto& [f, s] : mp) cout << f << "  " << s << "\n";
  8. #define Time cerr << "Time Taken: " << (float)clock() / CLOCKS_PER_SEC << " Secs" << "\n";
  9. #define fixed(n) fixed << setprecision(n)
  10. #define ceil(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
  11. #define fill(vec, value) memset(vec, value, sizeof(vec));
  12. #define Num_of_Digits(n) ((int)log10(n) + 1)
  13. #define mod_combine(a, b, m) (((a % m) * (b % m)) % m)
  14. #define all(vec) vec.begin(), vec.end()
  15. #define rall(vec) vec.rbegin(), vec.rend()
  16. #define sz(x) int(x.size())
  17. #define debug(x) cout << #x << ": " << (x) << "\n";
  18. #define fi first
  19. #define se second
  20. #define ll long long
  21. #define ull unsigned long long
  22. #define Mod  1'000'000'007
  23. #define OO 2'000'000'000
  24. #define EPS 1e-9
  25. #define PI acos(-1)
  26. template < typename T = int > using Pair = pair < T, T >;
  27.  
  28. template < typename T = int > istream& operator >> (istream &in, vector < T > &v) {
  29.     for (auto &x: v) in >> x;
  30.     return in;
  31. }
  32.  
  33. template < typename T = int > ostream& operator << (ostream &out, const vector < T > &v) {
  34.     for (const T &x: v) out << x << ' ';
  35.     return out;
  36. }
  37.  
  38. template < int MOD = 1000000007 > struct ModInt {
  39.  
  40.     int val;
  41.  
  42.     ModInt(int V = 0) : val(V) { val %= MOD; }
  43.  
  44.     ModInt& operator += (const ModInt& rhs) {
  45.         if ((val += rhs.val) >= MOD) val -= MOD;
  46.         return *this;
  47.     }
  48.     ModInt& operator += (const int rhs) {
  49.         if ((val += rhs) >= MOD) val -= MOD;
  50.         return *this;
  51.     }
  52.  
  53.     ModInt& operator -= (const ModInt& rhs) {
  54.         if ((val += MOD - rhs.val) >= MOD) val -= MOD;
  55.         return *this;
  56.     }
  57.     ModInt& operator -= (const int rhs) {
  58.         if ((val += MOD - rhs) >= MOD) val -= MOD;
  59.         return *this;
  60.     }
  61.  
  62.     ModInt& operator *= (const ModInt& rhs) { val = (1ll * val * rhs.val) % MOD; return *this; }
  63.     ModInt& operator *= (const int rhs) { val = (1ll * val * rhs) % MOD; return *this; }
  64.  
  65.     ModInt& operator /= (const ModInt& rhs) { return *this *= rhs.inverse(); }
  66.     ModInt& operator /= (const int rhs) { return *this *= ModInt(rhs).inverse(); }
  67.  
  68.     ModInt& operator %= (const ModInt& rhs) { val %= rhs.val; return *this; }
  69.     ModInt& operator %= (const int rhs) { val %= rhs; return *this; }
  70.  
  71.     ModInt& operator ++() { return *this += 1; }
  72.     ModInt& operator --() { return *this -= 1; }
  73.  
  74.     ModInt operator ++(int unused) { ModInt res(*this); ++*this; return res; }
  75.     ModInt operator --(int unused) { ModInt res(*this); --*this; return res; }
  76.    
  77.     ModInt operator + (const ModInt& rhs) const { ModInt res(*this); return res += rhs; }
  78.     ModInt operator + (const int rhs) const { ModInt res(*this); return res += rhs; }
  79.  
  80.     ModInt operator % (const ModInt& rhs) const { ModInt res(*this); return res %= rhs; }
  81.     ModInt operator % (const int rhs) const { ModInt res(*this); return res %= rhs; }
  82.  
  83.     ModInt operator - (const ModInt& rhs) const { ModInt res(*this); return res -= rhs; }
  84.     ModInt operator - (const int rhs) const { ModInt res(*this); return res -= rhs; }
  85.  
  86.     ModInt operator * (const ModInt& rhs) const { ModInt res(*this); return res *= rhs; }
  87.     ModInt operator * (const int rhs) const { ModInt res(*this); return res *= rhs; }
  88.  
  89.     ModInt operator / (const ModInt& rhs) const { ModInt res(*this); return res /= rhs; }
  90.     ModInt operator / (const int rhs) const { ModInt res(*this); return res /= rhs; }
  91.  
  92.     ModInt& operator = (const ModInt& rhs) { val = rhs.val; return *this; }
  93.     ModInt& operator = (const int rhs) { val = rhs; return *this; }
  94.  
  95.     bool operator == (const ModInt& rhs) const { return val == rhs.val; }
  96.     bool operator == (const int rhs) const { return val == rhs; }
  97.  
  98.     bool operator != (const ModInt& rhs) const { return val != rhs.val; }
  99.     bool operator != (const int rhs) const { return val != rhs; }
  100.  
  101.     bool operator < (const ModInt& rhs) const { return val < rhs.val; }
  102.     bool operator < (const int rhs) const { return val < rhs; }
  103.  
  104.     bool operator <= (const ModInt& rhs) const { return val <= rhs.val; }
  105.     bool operator <= (const int rhs) const { return val <= rhs; }
  106.  
  107.     bool operator > (const ModInt& rhs) const { return val > rhs.val; }
  108.     bool operator > (const int rhs) const { return val > rhs; }
  109.  
  110.     bool operator >= (const ModInt& rhs) const { return val >= rhs.val; }
  111.     bool operator >= (const int rhs) const { return val >= rhs; }
  112.  
  113.     int operator () () const { return val; }
  114.  
  115.     ModInt inverse() const { return power(MOD - 2); }
  116.  
  117.     ModInt power(ll n) const {
  118.         ModInt a = *this, res = 1;
  119.         while (n > 0) {
  120.             if (n & 1) res *= a;
  121.             a *= a, n >>= 1;
  122.         }
  123.         return res;
  124.     }
  125.  
  126.     ModInt power(ModInt n) const {
  127.         ModInt a = *this, res = 1;
  128.         int e = n();
  129.         while (e > 0) {
  130.             if (e & 1) res *= a;
  131.             a *= a, e >>= 1;
  132.         }
  133.         return res;
  134.     }
  135.  
  136.     friend ModInt operator ^ (ModInt rhs, ll n) { return rhs.power(n); }
  137.     friend ModInt operator ^ (ModInt rhs, ModInt n) { return rhs.power(n); }
  138.  
  139.     friend std::istream& operator>>(std::istream& is, ModInt& x) noexcept { return is >> x.val; }
  140.     friend std::ostream& operator<<(std::ostream& os, const ModInt& x) noexcept { return os << x.val; }
  141.  
  142. };
  143. using Mint = ModInt < >;
  144.  
  145. constexpr int N = 20;
  146. vector < vector < int > > adj(N + 5);
  147. vector < Mint > dp(1 << N, -1);
  148. int n;
  149.  
  150. Mint cnt_ways(int u, int mask){
  151.     if(u == n) return 1;
  152.     Mint& ret = dp[mask];
  153.     if(ret != -1) return ret;
  154.     ret = 0;
  155.     for(auto& v : adj[u])
  156.         if(!(mask & (1 << v)))
  157.             ret += cnt_ways(u + 1, mask | (1 << v));
  158.     return ret;
  159. }
  160.  
  161. void Solve(){
  162.     cin >> n;
  163.     for(int i = 0; i < n; i++)
  164.         for(int j = 0, val; j < n && cin >> val; j++)
  165.             if(val == 1)
  166.                 adj[i].push_back(j);
  167.     cout << cnt_ways(0, 0) << "\n";
  168. }
  169.  
  170. int main(){
  171.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  172.     int t = 1;
  173.     //cin >> t;
  174.     while(t--)
  175.         Solve();
  176.     return 0;
  177. }
Add Comment
Please, Sign In to add comment