Problem p02590 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
using cd = complex<ld>;
const ld PI = acos(-1);

int inverse(int x, int lg_n) {
  int y = 0;
  for (int i = 0; i < lg_n; i++) {
    if (x & (1 << i)) {
      y ^= (1 << (lg_n - 1 - i));
    }
  }
  return y;
}

void fft(vector<cd> &a, bool inv) {
  int n = (int)a.size(), lg_n = log2(n);
  for (int i = 0; i < n; i++) {
    int j = inverse(i, lg_n);
    if (i < j) {
      swap(a[i], a[j]);
    }
  }
  for (int len = 2; len <= n; len *= 2) {
    double ang = 2 * PI / len * (inv ? -1 : 1);
    cd mul(cos(ang), sin(ang));
    for (int i = 0; i < n; i += len) {
      cd w(1);
      for (int j = 0; j < len / 2; j++) {
        cd x = a[i + j], y = a[i + len / 2 + j] * w;
        a[i + j] = x + y;
        a[i + len / 2 + j] = x - y;
        w *= mul;
      }
    }
  }
  if (inv) {
    for (int i = 0; i < n; i++) {
      a[i] /= n;
    }
  }
}

vector<ll> multiply(vector<ll> &xa, vector<ll> &xb) {
  vector<cd> a(xa.begin(), xa.end()), b(xb.begin(), xb.end());
  int n = 1;
  while (n < (int)a.size() + (int)b.size()) {
    n *= 2;
  }
  a.resize(n);
  b.resize(n);
  fft(a, 0);
  fft(b, 0);
  for (int i = 0; i < n; i++) {
    a[i] *= b[i];
  }
  fft(a, 1);
  vector<ll> sol(n);
  for (int i = 0; i < n; i++) {
    sol[i] = round(real(a[i]));
  }
  return sol;
}

const int M = 200003;
int pt[M], goback[2 * M];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll sol = 0;
  int x = 1;
  for (int i = 0; i < M; i++) {
    pt[x] = i;
    goback[i] = x;
    goback[i + M - 1] = x;
    x = 2 * x % M;
  }
  vector<ll> a(M, 0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x == 0) {
      continue;
    }
    sol -= (ll)x * x % M;
    a[pt[x]]++;
  }
  a = multiply(a, a);
  for (int i = 0; i < 2 * M; i++) {
    if (a[i]) {
      int val = goback[i];
      sol += a[i] * val;
    }
  }
  sol /= 2;
  cout << sol << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll MOD = 200003;
ll n, s[MOD], s2[MOD], suf[MOD];

ll cnt(ll l, ll r) {
  if (l == 0) {
    return s[r];
  } else {
    return s[r] - s[l - 1];
  }
}

ll sum(ll l, ll r) {
  if (l == 0) {
    return s2[r];
  } else {
    return s2[r] - s2[l - 1];
  }
}

ll getscad() {
  ll scad = 0;
  for (ll x = 2; x < MOD; x++) {
    if (cnt(x, x) == 0) {
      continue;
    }
    ll ant = -1;
    ll now = 0;
    for (ll y = 1; y < x; y++) {
      ll bg = (ll)MOD * y / x + 1;
      ant = bg;
      now += suf[bg];
    }
    now *= MOD;
    scad += (ll)cnt(x, x) * now;
  }
  return scad;
}

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  /** for (ll x = 1; x <= 20; x++) {
     for (ll y = 1; y < x; y++) {
       ll a = (ll) MOD / x * y;
       ll b = (ll) MOD * y / x;
       if (a != b) {
         cout << x << " " << y << "\\n";
         cout << "error\\n";
       }
     }
   }
   return 0;**/
  ll sol = 0;
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    ll x;
    cin >> x;
    s[x]++;
    s2[x] += x;
    sol -= (ll)x * x % MOD;
  }
  for (ll i = 1; i < MOD; i++) {
    s[i] += s[i - 1];
    s2[i] += s2[i - 1];
  }
  for (ll i = 0; i < MOD; i++) {
    suf[i] = cnt(i, MOD - 1);
  }
  sol += sum(0, MOD - 1) * sum(0, MOD - 1);
  sol -= getscad();
  sol /= 2;
  cout << sol << "\\n";
  return 0;
}