Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.68 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. using cd = complex<double>;
  4. const double PI = acos(-1);
  5. /**
  6. when invert is true this function will act as inverse dft otherwise dft
  7. when invert is false
  8.     it take an array of co-efficient  and return as output array
  9. when invert is true
  10.     it take an array of output and return array of co-efficient
  11. */
  12. void fft(vector<cd> & a, bool invert) {
  13.     int n = a.size();
  14.     for (int i = 1, j = 0; i < n; i++) {
  15.         int bit = n >> 1;
  16.         for (; j & bit; bit >>= 1)
  17.             j ^= bit;
  18.         j ^= bit;
  19.         if (i < j)
  20.             swap(a[i], a[j]);
  21.     }
  22.     for (int len = 2; len <= n; len <<= 1) {
  23.         double ang = 2 * PI / len * (invert ? -1 : 1);
  24.         cd wlen(cos(ang), sin(ang));
  25.         for (int i = 0; i < n; i += len) {
  26.             cd w(1);
  27.             for (int j = 0; j < len / 2; j++) {
  28.                 cd u = a[i+j], v = a[i+j+len/2] * w;
  29.                 a[i+j] = u + v;
  30.                 a[i+j+len/2] = u - v;
  31.                 w *= wlen;
  32.             }
  33.         }
  34.     }
  35.  
  36.     if (invert) {
  37.         for (cd & x : a)
  38.             x /= n;
  39.     }
  40. }
  41. /**
  42. this function take to array of coefficient of two polynomial
  43. and return an array of coefficient of a polynomial which is multiplication of previous polynomials
  44. */
  45. vector<int> multiply(vector<int> const& a, vector<int> const& b) {
  46.     vector<cd> fa(a.begin(), a.end()), fb(b.begin(), b.end());
  47.     int n = 1;
  48.     while (n < a.size() + b.size())
  49.         n <<= 1;
  50.     fa.resize(n);
  51.     fb.resize(n);
  52.  
  53.     fft(fa, false);
  54.     fft(fb, false);
  55.     for (int i = 0; i < n; i++)
  56.         fa[i] *= fb[i];
  57.     fft(fa, true);
  58.     vector<int> result(n);
  59.     for (int i = 0; i < n; i++)
  60.         result[i] = round(fa[i].real());
  61.     return result;
  62. }
  63.  
  64.  
  65. /**
  66. ntt
  67. calculate those constant first
  68. calculation:
  69. mod=c*2^k+1
  70. find any c and k;
  71. find g = primitive root of mod
  72. find root=g^c
  73. find root_1=modular multiplicative inverse root=(1/root)%mod
  74. find root_pw=2^k
  75. */
  76. const int mod = 7340033;
  77. const int root = 5;
  78. const int root_1 = 4404020;
  79. const int root_pw = 1 << 20;
  80.  
  81. void fft(vector<int> & a, bool invert) {
  82.     int n = a.size();
  83.  
  84.     for (int i = 1, j = 0; i < n; i++) {
  85.         int bit = n >> 1;
  86.         for (; j & bit; bit >>= 1)
  87.             j ^= bit;
  88.         j ^= bit;
  89.         if (i < j)
  90.             swap(a[i], a[j]);
  91.     }
  92.     for (int len = 2; len <= n; len <<= 1) {
  93.         int wlen = invert ? root_1 : root;
  94.         for (int i = len; i < root_pw; i <<= 1)
  95.             wlen = (int)(1LL * wlen * wlen % mod);
  96.  
  97.         for (int i = 0; i < n; i += len) {
  98.             int w = 1;
  99.             for (int j = 0; j < len / 2; j++) {
  100.                 int u = a[i+j], v = (int)(1LL * a[i+j+len/2] * w % mod);
  101.                 a[i+j] = u + v < mod ? u + v : u + v - mod;
  102.                 a[i+j+len/2] = u - v >= 0 ? u - v : u - v + mod;
  103.                 w = (int)(1LL * w * wlen % mod);
  104.             }
  105.         }
  106.     }
  107.  
  108.     if (invert) {
  109.         int n_1 = inverse(n, mod);
  110.         for (int & x : a)
  111.             x = (int)(1LL * x * n_1 % mod);
  112.     }
  113. }
  114.  
  115. /**
  116. primitive root generator
  117. */
  118.  
  119. int generator (int p) {
  120.     vector<int> fact;
  121.     int phi = p-1,  n = phi;
  122.     for (int i=2; i*i<=n; ++i)
  123.         if (n % i == 0) {
  124.             fact.push_back (i);
  125.             while (n % i == 0)
  126.                 n /= i;
  127.         }
  128.     if (n > 1)
  129.         fact.push_back (n);
  130.  
  131.     for (int res=2; res<=p; ++res) {
  132.         bool ok = true;
  133.         for (size_t i=0; i<fact.size() && ok; ++i)
  134.             ok &= powmod (res, phi / fact[i], p) != 1;
  135.         if (ok)  return res;
  136.     }
  137.     return -1;
  138. }
  139.  
  140. int main()
  141. {
  142.     cout<<generator(7340033)<<endl;
  143.     return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement