Problem p02590 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef long double ld;
// typedef tree<int,null_type,less<int
// >,rb_tree_tag,tree_order_statistics_node_update>indexed_set;
template <class T1, class T2> ostream &operator<<(ostream &os, pair<T1, T2> &p);
template <class T> ostream &operator<<(ostream &os, vector<T> &v);
template <class T> ostream &operator<<(ostream &os, set<T> &v);
template <class T1, class T2> ostream &operator<<(ostream &os, map<T1, T2> &v);
#ifdef APURBA
#define debug(a) cout << #a << " --> " << (a) << "\\n";
#define debug2(a) cout << #a << " --> " << (a) << " ";
#define HERE cout << "here - " << __LINE__ << "\\n";
#else
#define debug(a)
#define debug2(a)
#define HERE
#endif
const ll mod = 200003;
const int N = 1e5 + 5;
typedef pair<int, int> pii;
ll gun(ll a, ll b) {
  ll x = (ll)a * b;
  x %= mod;
  return x;
}
ll bigmod(ll a, ll b) {
  ll tt = b;
  ll ans = 1;
  while (b > 0) {
    if (b & 1) {
      ans = gun(ans, a);
    }
    a = gun(a, a);
    b >>= 1;
  }
  return ans;
}
using cd = complex<double>;
const double PI = acos(-1);

void fft(vector<cd> &a, bool invert) {
  ll n = a.size();

  for (ll i = 1, j = 0; i < n; i++) {
    ll bit = n >> 1;
    for (; j & bit; bit >>= 1)
      j ^= bit;
    j ^= bit;

    if (i < j)
      swap(a[i], a[j]);
  }

  for (ll len = 2; len <= n; len <<= 1) {
    double ang = 2 * PI / len * (invert ? -1 : 1);
    cd wlen(cos(ang), sin(ang));
    for (ll i = 0; i < n; i += len) {
      cd w(1);
      for (ll j = 0; j < len / 2; j++) {
        cd u = a[i + j], v = a[i + j + len / 2] * w;
        a[i + j] = u + v;
        a[i + j + len / 2] = u - v;
        w *= wlen;
      }
    }
  }

  if (invert) {
    for (cd &x : a)
      x /= n;
  }
}

void multiply(vector<ll> const &a, vector<ll> const &b, vector<ll> &ans) {
  vector<cd> fa(a.begin(), a.end()), fb(b.begin(), b.end());
  ll n = 1;
  while (n < a.size() + b.size())
    n <<= 1;
  fa.resize(n);
  fb.resize(n);

  fft(fa, false);
  fft(fb, false);
  for (ll i = 0; i < n; i++)
    fa[i] *= fb[i];
  fft(fa, true);

  vector<ll> result(n);
  for (ll i = 0; i < n; i++)
    result[i] = round(fa[i].real());
  swap(result, ans);
}
vector<ll> v(mod, 0);
map<ll, ll> mp;
void TEST_CASES(int cas) {
  for (ll i = 0; i < mod; i++) {
    mp[bigmod(2, i)] = i;
  }
  ll n;
  scanf("%lld", &n);
  vector<ll> a;
  for (ll i = 0; i < n; i++) {
    ll x;
    scanf("%lld", &x);
    if (x == 0)
      continue;
    v[mp[x]]++;
    a.push_back(mp[x]);
  }
  if (a.size() == 0) {
    printf("0\\n");
    return;
  }
  vector<ll> res;
  multiply(v, v, res);
  ll ans = 0;
  for (ll i : a) {
    if (i + i < res.size())
      res[i + i]--;
  }
  for (ll i = 0; i < res.size(); i++) {
    if (res[i]) {
      // assert(res[i]%2==0);
      ll tmp = bigmod(2, i);
      tmp *= (ll)(res[i] / 2);
      ans += tmp;
    }
  }
  printf("%lld\\n", ans);
}

/*

*/

int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  int t = 1, cas = 0;
  // scanf("%d",&t);
  while (t--) {
    TEST_CASES(++cas);
  }
  return 0;
}

template <class T1, class T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p) {
  os << "{" << p.first << ", " << p.second << "} ";
  return os;
}
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  os << "[ ";
  for (int i = 0; i < v.size(); i++) {
    os << v[i] << " ";
  }
  os << " ]";
  return os;
}

template <class T> ostream &operator<<(ostream &os, set<T> &v) {
  os << "[ ";
  for (T i : v) {
    os << i << " ";
  }
  os << " ]";
  return os;
}
template <class T1, class T2> ostream &operator<<(ostream &os, map<T1, T2> &v) {
  for (auto i : v) {
    os << "Key : " << i.first << " , Value : " << i.second << endl;
  }
  return os;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef long double ld;
// typedef tree<int,null_type,less<int
// >,rb_tree_tag,tree_order_statistics_node_update>indexed_set;
template <class T1, class T2> ostream &operator<<(ostream &os, pair<T1, T2> &p);
template <class T> ostream &operator<<(ostream &os, vector<T> &v);
template <class T> ostream &operator<<(ostream &os, set<T> &v);
template <class T1, class T2> ostream &operator<<(ostream &os, map<T1, T2> &v);
#ifdef APURBA
#define debug(a) cout << #a << " --> " << (a) << "\\n";
#define debug2(a) cout << #a << " --> " << (a) << " ";
#define HERE cout << "here - " << __LINE__ << "\\n";
#else
#define debug(a)
#define debug2(a)
#define HERE
#endif
const ll mod = 200003;
const int N = 1e5 + 5;
typedef pair<int, int> pii;
ll gun(ll a, ll b) {
  ll x = (ll)a * b;
  x %= mod;
  return x;
}
ll bigmod(ll a, ll b) {
  ll tt = b;
  ll ans = 1;
  while (b > 0) {
    if (b & 1) {
      ans = gun(ans, a);
    }
    a = gun(a, a);
    b >>= 1;
  }
  return ans;
}
using cd = complex<double>;
const double PI = acos(-1);

void fft(vector<cd> &a, bool invert) {
  ll n = a.size();

  for (ll i = 1, j = 0; i < n; i++) {
    ll bit = n >> 1;
    for (; j & bit; bit >>= 1)
      j ^= bit;
    j ^= bit;

    if (i < j)
      swap(a[i], a[j]);
  }

  for (ll len = 2; len <= n; len <<= 1) {
    double ang = 2 * PI / len * (invert ? -1 : 1);
    cd wlen(cos(ang), sin(ang));
    for (ll i = 0; i < n; i += len) {
      cd w(1);
      for (ll j = 0; j < len / 2; j++) {
        cd u = a[i + j], v = a[i + j + len / 2] * w;
        a[i + j] = u + v;
        a[i + j + len / 2] = u - v;
        w *= wlen;
      }
    }
  }

  if (invert) {
    for (cd &x : a)
      x /= n;
  }
}

void multiply(vector<ll> const &a, vector<ll> const &b, vector<ll> &ans) {
  vector<cd> fa(a.begin(), a.end()), fb(b.begin(), b.end());
  ll n = 1;
  while (n < a.size() + b.size())
    n <<= 1;
  fa.resize(n);
  fb.resize(n);

  fft(fa, false);
  fft(fb, false);
  for (ll i = 0; i < n; i++)
    fa[i] *= fb[i];
  fft(fa, true);

  vector<ll> result(n);
  for (ll i = 0; i < n; i++)
    result[i] = round(fa[i].real());
  swap(result, ans);
}
vector<ll> v(mod, 0);
map<ll, ll> mp;
void TEST_CASES(int cas) {
  for (ll i = 0; i < mod; i++) {
    mp[bigmod(2, i)] = i;
  }
  ll n;
  scanf("%lld", &n);
  vector<ll> a;
  for (ll i = 0; i < n; i++) {
    ll x;
    scanf("%lld", &x);
    if (x == 0)
      continue;
    v[mp[x]]++;
    a.push_back(mp[x]);
  }
  if (a.size() == 0) {
    printf("0\\n");
    return;
  }
  vector<ll> res;
  multiply(v, v, res);
  ll ans = 0;
  for (ll i : a) {
    if (i + i < res.size())
      res[i + i]--;
  }
  for (ll i = 0; i < res.size(); i++) {
    if (res[i]) {
      assert(res[i] % 2 == 0);
      ll tmp = bigmod(2, i);
      tmp *= (ll)(res[i] / 2);
      ans += tmp;
    }
  }
  printf("%lld\\n", ans);
}

/*

*/

int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  int t = 1, cas = 0;
  // scanf("%d",&t);
  while (t--) {
    TEST_CASES(++cas);
  }
  return 0;
}

template <class T1, class T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p) {
  os << "{" << p.first << ", " << p.second << "} ";
  return os;
}
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  os << "[ ";
  for (int i = 0; i < v.size(); i++) {
    os << v[i] << " ";
  }
  os << " ]";
  return os;
}

template <class T> ostream &operator<<(ostream &os, set<T> &v) {
  os << "[ ";
  for (T i : v) {
    os << i << " ";
  }
  os << " ]";
  return os;
}
template <class T1, class T2> ostream &operator<<(ostream &os, map<T1, T2> &v) {
  for (auto i : v) {
    os << "Key : " << i.first << " , Value : " << i.second << endl;
  }
  return os;
}