Problem p02905 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
#define ll long long
#define itn int
#define endl '\n'
#define co(ans) cout << ans << endl
#define COYE cout << "YES" << endl
#define COYe cout << "Yes" << endl
#define COye cout << "yes" << endl
#define CONO cout << "NO" << endl
#define CONo cout << "No" << endl
#define COno cout << "no" << endl
#define FORE(i, a) for (auto &i : a)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FFOR(i, 1, n)
#define PB push_back
#define MP make_pair
#define ALL(V) (V).begin(), (V).end()
#define SORT(V) sort((V).begin(), (V).end())
#define REVERSE(V) reverse((V).begin(), (V).end())
#define EACH(V, i)                                                             \
  for (typeof((V).begin()) i = (V).begin(); i != (V).end(); ++i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
#define INF ((1LL << 62) - (1LL << 31))
#define EPS 1e-10
#define PI 3.141592653589793238
#define MOD 998244353
#define MAX 2010101
using namespace std;
using Edge = pair<ll, ll>;
using Graph = vector<vector<int>>;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

////////// 二項係数(N<=10^7) //////////

ll fac[MAX], finv[MAX], inv[MAX];
// fac[i] i!
// finv[i] i!の逆元
// inv[i] iの逆元

// テーブルを作る前処理
void COM_init() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (ll i = 2; i < MAX; ++i) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
ll COM(ll n, ll k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

//////////////////////////////////////////////////

////////// 約数列挙 //////////

vector<ll> divs[MAX];
// divs[i]=iの約数の集合

void DIVS_init() {
  for (int i = 1; i < MAX; ++i) {
    for (int j = i; j < MAX; j += i) {
      divs[j].push_back(i);
    }
  }
}

//////////////////////////////////////////////////

int main() {
  ll N, ans = 0;
  cin >> N;
  COM_init();
  DIVS_init();

  ll F[MAX];    // F[d] d=gcd(A[i],A[j])となるA[i]*A[j]の総和
  ll G[MAX];    // G[d] d|A[i],A[j]となるA[i]*A[j]の総和
  ll sum1[MAX]; // sum1[d] d|A[i]となるA[i]の総和
  ll sum2[MAX]; // sum2[d] d|A[i]となるA[i]^2の総和
  REP(i, MAX) F[i] = 0, G[i] = 0, sum1[i] = 0, sum2[i] = 0;

  ll A[N];
  REP(i, N) {
    cin >> A[i];
    FORE(v, divs[A[i]]) {
      sum1[v] += A[i];
      sum1[v] %= MOD;
      sum2[v] += A[i] * A[i];
      sum2[v] %= MOD;
    }
  }

  // 2*ΣA[i]*A[j]=(ΣA[i])^2-Σ(A[i]^2)
  for (int i = 1; i < MAX; ++i) {
    G[i] += sum1[i] * sum1[i] % MOD;
    G[i] -= sum2[i];
    G[i] *= inv[2];
    G[i] %= MOD;
    if (G[i] < 0)
      G[i] += MOD;
  }

  for (int i = 1010101; i >= 1; --i) {
    ll cnt = G[i];
    for (int j = 2; j * i < 1010101; ++j) {
      cnt -= F[j * i];
      cnt %= MOD;
      if (cnt < 0)
        cnt += MOD;
    }
    F[i] = cnt;
    ans += F[i] * inv[i];
    ans %= MOD;
    if (ans < 0)
      ans += MOD;
  }

  co(ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
#define ll long long
#define itn int
#define endl '\n'
#define co(ans) cout << ans << endl
#define COYE cout << "YES" << endl
#define COYe cout << "Yes" << endl
#define COye cout << "yes" << endl
#define CONO cout << "NO" << endl
#define CONo cout << "No" << endl
#define COno cout << "no" << endl
#define FORE(i, a) for (auto &i : a)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FFOR(i, 1, n)
#define PB push_back
#define MP make_pair
#define ALL(V) (V).begin(), (V).end()
#define SORT(V) sort((V).begin(), (V).end())
#define REVERSE(V) reverse((V).begin(), (V).end())
#define EACH(V, i)                                                             \
  for (typeof((V).begin()) i = (V).begin(); i != (V).end(); ++i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
#define INF ((1LL << 62) - (1LL << 31))
#define EPS 1e-10
#define PI 3.141592653589793238
#define MOD 998244353
#define MAX 1010101
using namespace std;
using Edge = pair<ll, ll>;
using Graph = vector<vector<int>>;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

////////// 二項係数(N<=10^7) //////////

ll fac[MAX], finv[MAX], inv[MAX];
// fac[i] i!
// finv[i] i!の逆元
// inv[i] iの逆元

// テーブルを作る前処理
void COM_init() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (ll i = 2; i < MAX; ++i) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
ll COM(ll n, ll k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

//////////////////////////////////////////////////

////////// 約数列挙 //////////

vector<ll> divs[MAX];
// divs[i]=iの約数の集合

void DIVS_init() {
  for (int i = 1; i < MAX; ++i) {
    for (int j = i; j < MAX; j += i) {
      divs[j].push_back(i);
    }
  }
}

//////////////////////////////////////////////////

int main() {
  ll N, ans = 0;
  cin >> N;
  COM_init();
  DIVS_init();

  ll F[MAX];    // F[d] d=gcd(A[i],A[j])となるA[i]*A[j]の総和
  ll G[MAX];    // G[d] d|A[i],A[j]となるA[i]*A[j]の総和
  ll sum1[MAX]; // sum1[d] d|A[i]となるA[i]の総和
  ll sum2[MAX]; // sum2[d] d|A[i]となるA[i]^2の総和
  REP(i, MAX) F[i] = 0, G[i] = 0, sum1[i] = 0, sum2[i] = 0;

  ll A[N];
  REP(i, N) {
    cin >> A[i];
    FORE(v, divs[A[i]]) {
      sum1[v] += A[i];
      sum1[v] %= MOD;
      sum2[v] += A[i] * A[i];
      sum2[v] %= MOD;
    }
  }

  // 2*ΣA[i]*A[j]=(ΣA[i])^2-Σ(A[i]^2)
  for (int i = 1; i < MAX; ++i) {
    G[i] += sum1[i] * sum1[i] % MOD;
    G[i] -= sum2[i];
    G[i] *= inv[2];
    G[i] %= MOD;
    if (G[i] < 0)
      G[i] += MOD;
  }

  for (int i = 1010101; i >= 1; --i) {
    ll cnt = G[i];
    for (int j = 2; j * i < 1010101; ++j) {
      cnt -= F[j * i];
      cnt %= MOD;
      if (cnt < 0)
        cnt += MOD;
    }
    F[i] = cnt;
    ans += F[i] * inv[i];
    ans %= MOD;
    if (ans < 0)
      ans += MOD;
  }

  co(ans);
  return 0;
}