Advertisement
Jaydeep999997

Unique Strings - Simple Multiply

Feb 14th, 2021
551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.97 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. #define endl '\n'
  6. #define ff first
  7. #define ss second
  8. #define pb push_back
  9. // #define int long long
  10. #define sz(v) (int)v.size()
  11. #define inf 2147483647
  12. #define llinf 9223372036854775807
  13. #define all(v) v.begin(),v.end()
  14. #define bp(n) __builtin_popcountll(n)
  15. #define f(i,l,r) for(int i=l;i<=r;i++)
  16. #define rf(i,r,l) for(int i=r;i>=l;i--)
  17. #define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
  18.  
  19. const int N = 5e3 + 5, mod = 1e9 + 7, bit = 61;
  20.  
  21.  
  22.  
  23. // Be careful about overflow as we are using integer everywhere
  24.  
  25. template<const int &MOD>
  26. struct _m_int  // This is our new type which will do operations under modulo MOD
  27. {
  28.     int val;   // Value of variable
  29.  
  30.     _m_int(int64_t v = 0)  // Typecasting into our data type from 64 bit integer
  31.     {
  32.         if (v < 0) v = v % MOD + MOD;
  33.         if (v >= MOD) v %= MOD;
  34.         val = v;
  35.     }
  36.  
  37.     static int mod_inv(int a, int m = MOD)
  38.     {
  39.         // https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Example
  40.         int g = m, r = a, x = 0, y = 1;
  41.  
  42.         while (r != 0)
  43.         {
  44.             int q = g / r;
  45.             g %= r; swap(g, r);
  46.             x -= q * y; swap(x, y);
  47.         }
  48.  
  49.         return x < 0 ? x + m : x;
  50.     }
  51.  
  52.     explicit operator int() const
  53.     {
  54.         return val;
  55.     }
  56.  
  57.     explicit operator int64_t() const
  58.     {
  59.         return val;
  60.     }
  61.  
  62.     _m_int& operator += (const _m_int &other)  // Addition
  63.     {
  64.         val -= MOD - other.val;
  65.         if (val < 0) val += MOD;
  66.         return *this;
  67.     }
  68.  
  69.     _m_int& operator -= (const _m_int &other)  // Subtraction
  70.     {
  71.         val -= other.val;
  72.         if (val < 0) val += MOD;
  73.         return *this;
  74.     }
  75.  
  76.     static unsigned fast_mod(uint64_t x, unsigned m = MOD) // Mod operation
  77.     {
  78. #if !defined(_WIN32) || defined(_WIN64)
  79.         return x % m;
  80. #endif
  81.         // Optimized mod for Codeforces 32-bit machines.
  82.         // x must be less than 2^32 * m for this to work, so that x / m fits in a 32-bit integer.
  83.         unsigned x_high = x >> 32, x_low = (unsigned) x;
  84.         unsigned quot, rem;
  85.         asm("divl %4\n"
  86.             : "=a" (quot), "=d" (rem)
  87.             : "d" (x_high), "a" (x_low), "r" (m));
  88.         return rem;
  89.     }
  90.  
  91.     _m_int& operator*=(const _m_int &other)  // Multiplication
  92.     {
  93.         val = fast_mod((uint64_t) val * other.val);
  94.         return *this;
  95.     }
  96.  
  97.     _m_int& operator/=(const _m_int &other)  // Division
  98.     {
  99.         return *this *= other.inv();
  100.     }
  101.  
  102.     friend _m_int operator+(const _m_int &a, const _m_int &b) { return _m_int(a) += b; }
  103.     friend _m_int operator-(const _m_int &a, const _m_int &b) { return _m_int(a) -= b; }
  104.     friend _m_int operator*(const _m_int &a, const _m_int &b) { return _m_int(a) *= b; }
  105.     friend _m_int operator/(const _m_int &a, const _m_int &b) { return _m_int(a) /= b; }
  106.  
  107.     _m_int& operator++()  // Pre-increment
  108.     {
  109.         val = val == MOD - 1 ? 0 : val + 1;
  110.         return *this;
  111.     }
  112.  
  113.     _m_int& operator--()  // Pre-decrement
  114.     {
  115.         val = val == 0 ? MOD - 1 : val - 1;
  116.         return *this;
  117.     }
  118.  
  119.     _m_int operator++(int) { _m_int before = *this; ++*this; return before; }  // Post increment
  120.     _m_int operator--(int) { _m_int before = *this; --*this; return before; }  // Post decrement
  121.  
  122.     _m_int operator-() const  // Change sign
  123.     {
  124.         return val == 0 ? 0 : MOD - val;
  125.     }
  126.  
  127.     // Boolean operators
  128.     bool operator==(const _m_int &other) const { return val == other.val; }
  129.     bool operator!=(const _m_int &other) const { return val != other.val; }
  130.     bool operator< (const _m_int &other) const { return val <  other.val; }
  131.     bool operator<=(const _m_int &other) const { return val <= other.val; }
  132.     bool operator> (const _m_int &other) const { return val >  other.val; }
  133.     bool operator>=(const _m_int &other) const { return val >= other.val; }
  134.  
  135.     _m_int inv() const  // Calculating inverse
  136.     {
  137.         return mod_inv(val);
  138.     }
  139.  
  140.     _m_int pow(int64_t p) const  // Calculating power
  141.     {
  142.         if (p < 0)
  143.             return inv().pow(-p);
  144.  
  145.         _m_int a = *this, result = 1;
  146.  
  147.         while (p > 0)
  148.         {
  149.             if (p & 1)
  150.             {
  151.                 result *= a;
  152.             }
  153.             a *= a;
  154.             p >>= 1;
  155.         }
  156.  
  157.         return result;
  158.     }
  159.  
  160.     friend ostream& operator<<(ostream &os, const _m_int &m)  // Writing output
  161.     {
  162.         return os << m.val;
  163.     }
  164.  
  165. };
  166.  
  167. extern const int MOD = 1e9 + 7;
  168. using mod_int = _m_int<MOD>;
  169.  
  170.  
  171. mod_int fact[N], ifact[N];
  172.  
  173.  
  174. void pre()
  175. {
  176.     fact[0] = 1;
  177.     int i;
  178.     for (i = 1; i < N; i++)
  179.     {
  180.         fact[i] = (fact[i - 1] * i);
  181.     }
  182.     ifact[N - 1] = fact[N - 1].inv();
  183.     for (i = N - 2; i >= 0; i--)
  184.     {
  185.         ifact[i] = (ifact[i + 1] * (i + 1));
  186.     }
  187. }
  188.  
  189. vector<mod_int> multiply(vector<mod_int> &a, vector<mod_int> &b)
  190. {
  191.     int n = sz(a);
  192.     int m = sz(b);
  193.     vector<mod_int> res(n + m);
  194.     f(i, 0, n - 1)
  195.     {
  196.         f(j, 0, m - 1)
  197.         {
  198.             res[i + j] += a[i] * b[j];
  199.         }
  200.     }
  201.     return res;
  202. }
  203.  
  204. signed main()
  205. {
  206.     fast;
  207.  
  208.  
  209.     pre();
  210.     string s;
  211.     int mp[26] = {0};
  212.     cin >> s;
  213.     for (auto &x : s)
  214.     {
  215.         mp[x - 'a']++;
  216.     }
  217.     vector<mod_int> res{1};
  218.     f(i, 0, 25)
  219.     {
  220.         if (mp[i] == 0)
  221.         {
  222.             continue;
  223.         }
  224.         vector<mod_int> now;
  225.         f(j, 0, mp[i])
  226.         {
  227.             now.pb(ifact[j]);
  228.         }
  229.         res = multiply(res, now);
  230.     }
  231.     int n = sz(s);
  232.     n = min(n, sz(res));
  233.     mod_int ans = 0;
  234.     f(i, 1, n)
  235.     {
  236.         ans += (fact[i] * res[i]);
  237.     }
  238.     cout << ans;
  239.     return 0;
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement