Problem p02905 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define fo(i, l, r) for (int i = l; i <= r; i++)
#define of(i, l, r) for (int i = l; i >= r; i--)
#define fe(i, u) for (int i = head[u]; i; i = e[i].next)
#define el putchar('\n')
#define ta putchar('	')
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
inline void open(const char *s) {
#ifndef ONLINE_JUDGE
  char str[20];
  sprintf(str, "%s.in", s);
  freopen(str, "r", stdin);
  //	sprintf(str,"%s.out",s);
  //	freopen(str,"w",stdout);
#endif
}
inline int rd() {
  static int x, f;
  x = 0;
  f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = x * 10 + ch - '0';
  return f > 0 ? x : -x;
}
const int N = 1000010, mod = 998244353;
int n, a[N];
bool vis[N];
int pri[N], cnt = 0, miu[N], inv[N], f[N];
vector<int> fac[N];

inline int pls(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
inline int upd(int &a, int b) { return a = pls(a, b); }
inline int mns(int a, int b) { return a < b ? a - b + mod : a - b; }
inline int mul(int a, int b) { return (ll)a * b % mod; }

inline void prelude(int n) {
  miu[1] = 1;
  inv[0] = inv[1] = 1;
  fo(i, 2, n) {
    inv[i] = mns(0, mul(mod / i, inv[mod % i]));
    if (!vis[i])
      miu[i] = mod - 1, pri[++cnt] = i;
    for (int j = 1; j <= cnt && pri[j] * i <= n; ++j) {
      int x = pri[j] * i;
      vis[x] = 1;
      if (i % pri[j] == 0) {
        miu[x] = 0;
        break;
      }
      miu[x] = mns(0, miu[i]);
    }
  }
  fo(i, 1, n) {
    for (int j = i; j <= n; j += i) {
      upd(f[j], mul(miu[i], inv[j / i]));
      if (i ^ j)
        upd(a[i], a[j]);
    }
  }
  fo(i, 1, cnt) for (int j = pri[i]; j <= n; j += pri[i]) fac[j].push_back(
      pri[i]);
}

int main() {
  open("hh");
  n = rd();
  int ans = 0;
  fo(i, 1, n) {
    int x = rd();
    upd(a[x], x);
    ans = mns(ans, x);
  }
  prelude(N - 10);
  fo(i, 1, N - 10) if (f[i]) {
    int tmp = mul(f[i], mul(a[i], a[i]));
    upd(ans, tmp);
  }
  ans = mul(ans, (mod + 1) / 2);
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define fo(i, l, r) for (int i = l; i <= r; i++)
#define of(i, l, r) for (int i = l; i >= r; i--)
#define fe(i, u) for (int i = head[u]; i; i = e[i].next)
#define el putchar('\n')
#define ta putchar('	')
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
inline void open(const char *s) {
#ifndef ONLINE_JUDGE
  char str[20];
  sprintf(str, "%s.in", s);
  freopen(str, "r", stdin);
  //	sprintf(str,"%s.out",s);
  //	freopen(str,"w",stdout);
#endif
}
inline int rd() {
  static int x, f;
  x = 0;
  f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = x * 10 + ch - '0';
  return f > 0 ? x : -x;
}
const int N = 1000010, mod = 998244353;
int n, a[N];
bool vis[N];
int pri[N], cnt = 0, miu[N], inv[N], f[N];
vector<int> fac[N];

inline int pls(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
inline int upd(int &a, int b) { return a = pls(a, b); }
inline int mns(int a, int b) { return a < b ? a - b + mod : a - b; }
inline int mul(int a, int b) { return (ll)a * b % mod; }

inline void prelude(int n) {
  miu[1] = 1;
  inv[0] = inv[1] = 1;
  fo(i, 2, n) {
    inv[i] = mns(0, mul(mod / i, inv[mod % i]));
    if (!vis[i])
      miu[i] = mod - 1, pri[++cnt] = i;
    for (int j = 1; j <= cnt && pri[j] * i <= n; ++j) {
      int x = pri[j] * i;
      vis[x] = 1;
      if (i % pri[j] == 0) {
        miu[x] = 0;
        break;
      }
      miu[x] = mns(0, miu[i]);
    }
  }
  fo(i, 1, n) {
    for (int j = i; j <= n; j += i) {
      upd(f[j], mul(miu[i], inv[j / i]));
      if (i ^ j)
        upd(a[i], a[j]);
    }
  }
  fo(i, 1, cnt) for (int j = pri[i]; j <= n; j += pri[i]) fac[j].push_back(
      pri[i]);
}

int main() {
  n = rd();
  int ans = 0;
  fo(i, 1, n) {
    int x = rd();
    upd(a[x], x);
    ans = mns(ans, x);
  }
  prelude(N - 10);
  fo(i, 1, N - 10) if (f[i]) {
    int tmp = mul(f[i], mul(a[i], a[i]));
    upd(ans, tmp);
  }
  ans = mul(ans, (mod + 1) / 2);
  cout << ans;
  return 0;
}