prithivMLmods/Coder-Stat huggingface dataset:
Problem p02792 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
ll modpow(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }

  return res;
}

ll comb(ll a, ll b) {
  ll ans = 1;

  for (ll i = a; i > a - b; i--) {
    ans = ans * i % mod;
  }
  for (ll i = 1; i <= b; i++) {
    ans = (ans * modpow(i, mod - 2)) % mod;
  }
  return ans;
}

int main() {
  ll N;
  cin >> N;
  ll num[N][N];
  for (ll i = 1; i <= N; i++) {
    string str = to_string(i);
    int size = str.size();
    const char *cstr = str.c_str();
    string f(cstr, 0, 1);
    string e(cstr, size - 1, 1);
    int a = stoi(f);
    int b = stoi(e);
    num[a][b] += 1;
  }
  ll ans = 0;
  for (int i = 1; i <= N; i++) {
    string str = to_string(i);
    int size = str.size();
    const char *cstr = str.c_str();
    string f(cstr, 0, 1);
    string e(cstr, size - 1, 1);
    int a = stoi(f);
    int b = stoi(e);

    ans += num[b][a];
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
ll modpow(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }

  return res;
}

ll comb(ll a, ll b) {
  ll ans = 1;

  for (ll i = a; i > a - b; i--) {
    ans = ans * i % mod;
  }
  for (ll i = 1; i <= b; i++) {
    ans = (ans * modpow(i, mod - 2)) % mod;
  }
  return ans;
}

int main() {
  ll N;
  cin >> N;
  ll num[10][10];
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      num[i][j] = 0;
    }
  }
  for (ll i = 1; i <= N; i++) {
    string str = to_string(i);
    int size = str.size();
    const char *cstr = str.c_str();
    string f(cstr, 0, 1);
    string e(cstr, size - 1, 1);
    int a = stoi(f);
    int b = stoi(e);
    num[a][b] += 1;
  }
  ll ans = 0;
  for (int i = 1; i <= N; i++) {
    string str = to_string(i);
    int size = str.size();
    const char *cstr = str.c_str();
    string f(cstr, 0, 1);
    string e(cstr, size - 1, 1);
    int a = stoi(f);
    int b = stoi(e);

    ans += num[b][a];
  }

  cout << ans << endl;
  return 0;
}
Problem p02792 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pi 3.14159265359
#define int long long
#define pii pair<int, int>
const int mod = 1e9 + 7;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
#endif

  map<pii, int> mp;
  int n;
  cin >> n;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int k1 = i % 10;
    int k2 = i;
    int j = i;
    while (j >= 10) {
      j /= 10;
    }
    k2 = j;
    mp[{k2, k1}]++;
  }

  map<pii, int> z = mp;

  for (auto i : mp) {
    int x = i.first.first;
    int y = i.first.second;
    ans += z[{x, y}] * z[{y, x}];
  }
  cout << ans << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pi 3.14159265359
#define int long long
#define pii pair<int, int>
const int mod = 1e9 + 7;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  map<pii, int> mp;
  int n;
  cin >> n;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int k1 = i % 10;
    int k2 = i;
    int j = i;
    while (j >= 10) {
      j /= 10;
    }
    k2 = j;
    mp[{k2, k1}]++;
  }

  map<pii, int> z = mp;

  for (auto i : mp) {
    int x = i.first.first;
    int y = i.first.second;
    ans += z[{x, y}] * z[{y, x}];
  }
  cout << ans << "\n";
}
Problem p02792 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
using P = pair<int, int>;
P f(int x) {
  int fst;
  string s = to_string(x);
  fst = s[0] - '0';
  int last = x % 10;
  return P(fst, last);
}
int main() {
  int n;
  cin >> n;
  int si = to_string(n).size();
  if (si == 1) {
    cout << n << endl;
  } else {
    map<P, int> num;
    for (int i = 1; i <= n; i++) {
      P p = f(i);
      num.at(p)++;
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      P p = f(i);
      P q(p.second, p.first);
      ans += num[q];
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
using P = pair<int, int>;
P f(int x) {
  int fst;
  string s = to_string(x);
  fst = s[0] - '0';
  int last = x % 10;
  return P(fst, last);
}
int main() {
  int n;
  cin >> n;
  int si = to_string(n).size();
  if (si == 1) {
    cout << n << endl;
  } else {
    map<P, int> num;
    for (int i = 1; i <= n; i++) {
      P p = f(i);
      num[p]++;
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      P p = f(i);
      P q(p.second, p.first);
      ans += num[q];
    }
    cout << ans << endl;
  }
}
Problem p02792 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  long long n, fl[10][10], ans = 0;
  cin >> n;
  for (int i = 0; i < 10; i++)
    for (int j = 0; j < 10; j++)
      fl[i][j] = 0;
  for (int i = 1; i <= n; i++) {
    int x = i;
    while (x > 10)
      x /= 10;
    fl[x][i % 10]++;
  }
  for (int i = 0; i < 10; i++)
    for (int j = 0; j < 10; j++)
      ans += fl[i][j] * fl[j][i];
  cout << ans << endl;
}


Fixed submission:
#include <iostream>
using namespace std;
int main() {
  long long n, fl[10][10], ans = 0;
  cin >> n;
  for (int i = 0; i < 10; i++)
    for (int j = 0; j < 10; j++)
      fl[i][j] = 0;
  for (int i = 1; i <= n; i++) {
    int x = i;
    while (x >= 10)
      x /= 10;
    fl[x][i % 10]++;
  }
  for (int i = 0; i < 10; i++)
    for (int j = 0; j < 10; j++)
      ans += fl[i][j] * fl[j][i];
  cout << ans << endl;
}
Problem p02792 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iso646.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

#ifndef M_PI
#define M_PI 3.14159265358979
#endif
#define deg_to_rad(deg) (((deg) / 360) * 2 * M_PI)
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;
typedef vector<long long> vll;
typedef pair<long long, long long> pll;

const ll INF = 1e15;
const ll MOD = 1e9 + 7;

ll itoj(string N, ll i, ll j) {
  ll len = N.length();
  ll ans = 0;
  for (ll k = 1; k <= len; k++) {
    if (k == 1 && i == j) {
      ans++;
      continue;
    }
    if (k == 2) {
      if (i * 10 + j <= stoi(N)) {
        ans++;
      }
      continue;
    }
    if (k >= 3 && k < len) {
      ans += pow(10, k - 2);
      continue;
    }
    if (k == len) {
      if (i < N[0] - '0') {
        ans += pow(10, k - 2);
      } else if (i > N[0] - '0') {
        continue;
      } else {
        ans += stoi(N.substr(1, len - 2));
        if (j <= N[len - 1] - '0') {
          ans++;
        }
      }
    }
  }
  return ans;
}

int main() {
  ll i, j;
  string N;
  cin >> N;
  ll len = N.length();

  ll ans = 0;
  for (i = 1; i <= 9; i++) {
    for (j = 1; j <= 9; j++) {
      ans += itoj(N, i, j) * itoj(N, j, i);
    }
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iso646.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

#ifndef M_PI
#define M_PI 3.14159265358979
#endif
#define deg_to_rad(deg) (((deg) / 360) * 2 * M_PI)
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;
typedef vector<long long> vll;
typedef pair<long long, long long> pll;

const ll INF = 1e15;
const ll MOD = 1e9 + 7;

ll itoj(string N, ll i, ll j) {
  ll len = N.length();
  ll ans = 0;
  for (ll k = 1; k <= len; k++) {
    if (k == 1) {
      if (i <= stoi(N) && i == j) {
        ans++;
      }
      continue;
    }
    if (k == 2) {
      if (i * 10 + j <= stoi(N)) {
        ans++;
      }
      continue;
    }
    if (k >= 3 && k < len) {
      ans += pow(10, k - 2);
      continue;
    }
    if (k == len) {
      if (i < N[0] - '0') {
        ans += pow(10, k - 2);
      } else if (i > N[0] - '0') {
        continue;
      } else {
        ans += stoi(N.substr(1, len - 2));
        if (j <= N[len - 1] - '0') {
          ans++;
        }
      }
    }
  }
  return ans;
}

int main() {
  ll i, j;
  string N;
  cin >> N;
  ll len = N.length();

  ll ans = 0;
  for (i = 1; i <= 9; i++) {
    for (j = 1; j <= 9; j++) {
      ans += itoj(N, i, j) * itoj(N, j, i);
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p02792 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n = 0;
  cin >> n;
  vector<vector<int>> kazu(10, vector<int>(10));
  for (int i = 0; i <= 9; i++) {
    for (int j = 0; j <= 9; j++) {
      kazu[i][j] = 0;
    }
  }
  for (int i = 0; i <= 9; i++) {
    for (int j = 0; j <= 9; j++) {
      for (int k = 1; k <= n; k++) {
        string moji;
        moji = to_string(k);
        // cout << "mojiの最初" << moji[0] << " i" << i << " mojiの最後" <<
        // moji[moji.size() - 1] << " j" << j << endl;
        if (moji[0] - '0' == i && moji[moji.size() - 1] - '0' == j) {
          // cout << "a" << endl;
          kazu[i][j]++;
        }
      }
    }
  }
  long long kotae = 0;
  for (int i = 0; i <= 9; i++) {
    for (int j = 0; j <= 9; j++) {
      kotae += kazu[i][j] * kazu[j][i];
      // cout << kazu[i][j] << " " << kazu[j][i] << endl;
    }
  }
  cout << kotae << endl;
}

Fixed submission:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n = 0;
  cin >> n;
  vector<vector<int>> kazu(10, vector<int>(10));
  for (int i = 0; i <= 9; i++) {
    for (int j = 0; j <= 9; j++) {
      kazu[i][j] = 0;
    }
  }
  for (int k = 1; k <= n; k++) {
    string moji;
    moji = to_string(k);
    // cout << "mojiの最初" << moji[0] << " i" << i << " mojiの最後" <<
    // moji[moji.size() - 1] << " j" << j << endl;
    kazu[moji[0] - '0'][moji[moji.size() - 1] - '0']++;
  }
  long long kotae = 0;
  for (int i = 0; i <= 9; i++) {
    for (int j = 0; j <= 9; j++) {
      kotae += kazu[i][j] * kazu[j][i];
      // cout << kazu[i][j] << " " << kazu[j][i] << endl;
    }
  }
  cout << kotae << endl;
}
Problem p02792 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#include <math.h>
#include <time.h>
using namespace std;
const long long mod = 1000000007;
#define dump(x)                                                                \
  if (dbg) {                                                                   \
    cout << #x << " = " << (x) << endl;                                        \
  }
bool dbg = false;
long long gcd(long long a, long long b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}
long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; }
//---------------------------------------------------------------------------------------------------
/*ここからいじる*/
//---------------------------------------------------------------------------------------------------

void solve() {}
int main() {
  dbg = true;
  int n;
  cin >> n;
  vector<vector<int>> data(10, (vector<int>(10)));
  for (int i = 1; i <= 9; i++) {
    for (int j = 1; j <= 9; j++) {
      for (int k = 1; k <= n; k++) {
        char ich, jch;
        ich = i + '0';
        jch = j + '0';
        if (to_string(k).at(0) == (ich) &&
            to_string(k).at(to_string(k).size() - 1) == (jch)) {
          data.at(i).at(j)++;
        }
      }
    }
  }
  long long ans = 0;
  for (int i = 1; i <= 9; i++) {
    for (int j = 1; j <= 9; j++) {
      ans += data.at(i).at(j) * data.at(j).at(i);
    }
  }
  cout << ans << endl;
  solve();
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
#include <math.h>
#include <time.h>
using namespace std;
const long long mod = 1000000007;
#define dump(x)                                                                \
  if (dbg) {                                                                   \
    cout << #x << " = " << (x) << endl;                                        \
  }
bool dbg = false;
long long gcd(long long a, long long b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}
long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; }
//---------------------------------------------------------------------------------------------------
/*ここからいじる*/
//---------------------------------------------------------------------------------------------------

void solve() {}
int main() {
  dbg = true;
  int n;
  cin >> n;
  vector<vector<int>> data(10, (vector<int>(10)));
  int num;
  for (int i = 1; i <= n; i++) {
    data.at(to_string(i).at(0) - '0')
        .at(to_string(i).at(to_string(i).size() - 1) - '0')++;
  }
  long long ans = 0;
  for (int i = 1; i <= 9; i++) {
    for (int j = 1; j <= 9; j++) {
      ans += data.at(i).at(j) * data.at(j).at(i);
    }
  }
  cout << ans << endl;
  solve();
  return 0;
}
Problem p02792 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define _GLIBCXX_DEBUG
#define rep(i, c) for (int i = 0; i < (int)c; i++)
#define debug(x) cout << #x << " = " << x << endl
#define print(x) cout << x << endl;

int main() {
  int N;
  cin >> N;

  vector<vector<ll>> table(9, vector<ll>(9, 0));

  ll num = 0;
  for (int k = 1; k <= N; k++) {
    string k_str = to_string(k);
    int i = k_str.at(0) - '0';
    int j = k_str.at(k_str.size() - 1) - '0';
    table[i - 1][j - 1] += 1;
  }

  ll ans = 0;
  for (int i = 1; i <= N; i++) {
    string i_str = to_string(i);
    int l = i_str.at(0) - '0';
    int m = i_str.at(i_str.size() - 1) - '0';

    if (m == 0) {
      continue;
    }

    ans += table[m - 1][l - 1];
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define _GLIBCXX_DEBUG
#define rep(i, c) for (int i = 0; i < (int)c; i++)
#define debug(x) cout << #x << " = " << x << endl
#define print(x) cout << x << endl;

int main() {
  int N;
  cin >> N;

  vector<vector<ll>> table(9, vector<ll>(9, 0));

  ll num = 0;
  for (int k = 1; k <= N; k++) {
    string k_str = to_string(k);
    int i = k_str.at(0) - '0';
    int j = k_str.at(k_str.size() - 1) - '0';
    if (j == 0) {
      continue;
    }
    table[i - 1][j - 1] += 1;
  }

  ll ans = 0;
  for (int i = 1; i <= N; i++) {
    string i_str = to_string(i);
    int l = i_str.at(0) - '0';
    int m = i_str.at(i_str.size() - 1) - '0';

    if (m == 0) {
      continue;
    }

    ans += table[m - 1][l - 1];
  }

  cout << ans << endl;
}
Problem p02792 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<vector<int>> count(10, vector<int>(10, 0));
  for (int i = 1; i <= N; i++) {
    int a = i % 10;
    int b = i;
    while (b > 10) {
      b /= 10;
    }
    count[a][b]++;
  }
  long long ans = 0;
  for (int i = 1; i <= 9; i++) {
    for (int j = 1; j <= 9; j++) {
      ans += count[i][j] * count[j][i];
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<vector<int>> count(10, vector<int>(10, 0));
  for (int i = 1; i <= N; i++) {
    int a = i % 10;
    int b = i;
    while (b >= 10) {
      b /= 10;
    }
    count[a][b]++;
  }
  long long ans = 0;
  for (int i = 1; i <= 9; i++) {
    for (int j = 1; j <= 9; j++) {
      ans += count[i][j] * count[j][i];
    }
  }
  cout << ans << endl;
}
Problem p02792 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define print(x) cout << x << endl

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; }

ll MOD = 1000000007;

int main() {
  ll N;
  cin >> N;
  vector<vector<ll>> mat(N, vector<ll>(N, 0));
  rep(i, N) {
    string tmp = to_string(i + 1);
    ll s = tmp[0] - '0';
    ll e = tmp.back() - '0';
    mat[s][e]++;
  }
  ll sum = 0;
  rep(i, 10) {
    rep(j, 10) { sum += mat[i][j] * mat[j][i]; }
    cout << endl;
  }
  cout << sum << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define print(x) cout << x << endl

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; }

ll MOD = 1000000007;

int main() {
  ll N;
  cin >> N;
  vector<vector<ll>> mat(10, vector<ll>(10, 0));
  rep(i, N) {
    string tmp = to_string(i + 1);
    ll s = tmp[0] - '0';
    ll e = tmp.back() - '0';
    mat[s][e]++;
  }
  ll sum = 0;
  rep(i, 10) {
    rep(j, 10) { sum += mat[i][j] * mat[j][i]; }
    cout << endl;
  }
  cout << sum << endl;
  return 0;
}
Problem p02792 (C++)

Buggy submission (status: Runtime Error):
/*
        Name:
        Author: xiaruize
        Date:
*/

#include <bits/stdc++.h>
using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define ll long long
#define form(i, j, n) for (int i = int(j); i <= int(n); i++)
#define pb push_back
#define mk make_pair
#define pii pair<int, int>
#define pis pair<int, string>
#define sec second

inline ll read() {
  ll f = 1, x = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

int f[20][20];
int num[20];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  int n;
  n = read();
  int x;
  form(i, 1, n) {
    x = i;
    while (x >= 10) {
      x /= 10;
    }
    num[i] = x;
    f[x][i % 10]++;
  }
  int ans = 0;
  form(i, 1, n) { ans += f[i % 10][num[i]]; }
  cout << ans << endl;
  return 0;
}

Fixed submission:
/*
        Name:
        Author: xiaruize
        Date:
*/

#include <bits/stdc++.h>
using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define ll long long
#define form(i, j, n) for (int i = int(j); i <= int(n); i++)
#define pb push_back
#define mk make_pair
#define pii pair<int, int>
#define pis pair<int, string>
#define sec second

inline ll read() {
  ll f = 1, x = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

int f[20][20];
int num[200000];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  int n;
  n = read();
  int x;
  form(i, 1, n) {
    x = i;
    while (x >= 10) {
      x /= 10;
    }
    num[i] = x;
    f[x][i % 10]++;
  }
  int ans = 0;
  form(i, 1, n) { ans += f[i % 10][num[i]]; }
  cout << ans << endl;
  return 0;
}
Problem p02792 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
using namespace std;
const ll INF = 1e18;

ll int aditya(ll int x) {
  while (x / 10 > 0) {
    x /= 10;
  }
  return x;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ll int c[10][10];
  memset(c, 0, sizeof(c));
  ll int n, i, j, res = 0;
  cin >> n;
  for (i = 1; i <= n; i++) {
    c[aditya(i)][i % 10]++;
  }
  for (i = 0; i < 10; i++) {
    for (j = 0; j < 10; j++) {
      res += c[i][j] * c[j][i];
    }
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
using namespace std;
const ll INF = 1e18;

ll int aditya(ll int x) {
  while (x / 10 > 0) {
    x /= 10;
  }
  return x;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll int c[10][10];
  memset(c, 0, sizeof(c));
  ll int n, i, j, res = 0;
  cin >> n;
  for (i = 1; i <= n; i++) {
    c[aditya(i)][i % 10]++;
  }
  for (i = 0; i < 10; i++) {
    for (j = 0; j < 10; j++) {
      res += c[i][j] * c[j][i];
    }
  }
  cout << res << endl;
  return 0;
}
Problem p02792 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
ull firstDigit(ull n) {
  while (n >= 10)
    n /= 10;

  return n;
}

int lastDigit(int n) { return (n % 10); }

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;
  vector<vector<ull>> arr = vector<vector<ull>>(n + 1, vector<ull>(n + 1, 0));

  for (int i = 1; i <= n; i++) {
    arr[firstDigit(i)][lastDigit(i)]++;
  }

  unsigned long long sum = 0;
  for (int i = 0; i < 9; i++) {
    for (int j = 0; j < 9; j++) {
      sum += arr[i + 1][j + 1] * arr[j + 1][i + 1];
    }
  }

  cout << sum;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
ull firstDigit(ull n) {
  while (n >= 10)
    n /= 10;

  return n;
}

int lastDigit(int n) { return (n % 10); }

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;
  vector<vector<ull>> arr = vector<vector<ull>>(10, vector<ull>(10, 0));

  for (int i = 1; i <= n; i++) {
    arr[firstDigit(i)][lastDigit(i)]++;
  }

  unsigned long long sum = 0;
  for (int i = 0; i < 9; i++) {
    for (int j = 0; j < 9; j++) {
      sum += arr[i + 1][j + 1] * arr[j + 1][i + 1];
    }
  }

  cout << sum;

  return 0;
}
Problem p02792 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"

using namespace std;

#define sp(x) cout << setprecision(x);
#define all(a) (a).begin(), (a).end()
#define inf 10000000
#define linf INT64_MAX * 0.99
#define print(s) cout << (s) << endl
#define lint long long
#define FOR(i, a, b) for (lint i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define yes "Yes"
#define no "No"
#define divs 1000000007
#define dive 1000000009

typedef pair<int, int> P;

lint ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<vector<int>> memo(9);
  REP(i, 9) {
    REP(j, 9) { memo[i].push_back(0); }
  }
  REP(i, n) {
    string s = to_string(i + 1);
    int a = ctoi(s[0]);
    int b = ctoi(s[s.length() - 1]);
    memo[a - 1][b - 1]++;
  }
  lint ans = 0;
  REP(i, 9) {
    REP(j, 9) { ans += memo[i][j] * memo[j][i]; }
  }
  print(ans);
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"

using namespace std;

#define sp(x) cout << setprecision(x);
#define all(a) (a).begin(), (a).end()
#define inf 10000000
#define linf INT64_MAX * 0.99
#define print(s) cout << (s) << endl
#define lint long long
#define FOR(i, a, b) for (lint i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define yes "Yes"
#define no "No"
#define divs 1000000007
#define dive 1000000009

typedef pair<int, int> P;

lint ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<vector<int>> memo(9);
  REP(i, 9) {
    REP(j, 9) { memo[i].push_back(0); }
  }
  REP(i, n) {
    string s = to_string(i + 1);
    int a = ctoi(s[0]);
    int b = ctoi(s[s.length() - 1]);
    if (b == 0)
      continue;
    memo[a - 1][b - 1]++;
  }
  lint ans = 0;
  REP(i, 9) {
    REP(j, 9) { ans += memo[i][j] * memo[j][i]; }
  }
  print(ans);
  return 0;
}
Problem p02792 (C++)

Buggy submission (status: Time Limit Exceeded):
// 5/14
// 過去問ガリガリ
#include <bits/stdc++.h>
#include <string>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

int dp[10][10];

int main() {
  int N;
  cin >> N;

  for (int i = 1; i < 10; i++) {
    for (int j = 1; j < 10; j++) {
      dp[i][j] = 0;
      for (int k = 1; k <= N; k++) {
        string str = to_string(k);
        if (str[0] - '0' == i && str[str.size() - 1] - '0' == j) {
          dp[i][j] += 1;
        }
      }
    }
  }

  int ans = 0;

  for (int i = 1; i < 10; i++) {
    for (int j = 1; j < 10; j++) {
      ans += dp[i][j] * dp[j][i];
    }
  }

  cout << ans << endl;
}


Fixed submission:
// 5/14
// 過去問ガリガリ
#include <bits/stdc++.h>
#include <string>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

int dp[10][10];

int main() {
  int N;
  cin >> N;

  memset(dp, 0, sizeof(dp));

  // for (int i = 1; i < 10; i++) {
  //     for (int j = 1; j < 10; j++) {
  //         dp[i][j] = 0;
  //         for (int k = 1; k <= N; k++) {
  //             string str = to_string(k);
  //             if (str[0] - '0' == i && str[str.size() - 1] - '0' == j) {
  //                 dp[i][j] += 1;
  //             }
  //         }
  //     }
  // }

  for (int k = 1; k <= N; k++) {
    string str = to_string(k);
    int i = str[0] - '0';
    int j = str[str.size() - 1] - '0';
    dp[i][j]++;
  }

  int ans = 0;

  for (int i = 1; i < 10; i++) {
    for (int j = 1; j < 10; j++) {
      ans += dp[i][j] * dp[j][i];
    }
  }

  cout << ans << endl;
}
Problem p02792 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1001001001;
const ll INFL = 1e17;
const int MOD = 1e9 + 7;

P freq(int x) {
  int a = x % 10;
  int b;
  while (x) {
    b = x;
    b /= 10;
  }
  return P(a, b);
}

int main() {
  int n;
  cin >> n;
  map<P, int> fr;

  for (int i = 1; i <= n; i++) {
    P p = freq(i);
    fr[p]++;
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    P p = freq(i);
    P q(p.second, p.first);
    ans += fr[q];
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1001001001;
const ll INFL = 1e17;
const int MOD = 1e9 + 7;

P freq(int x) {
  int a = x % 10;
  int b;
  while (x) {
    b = x;
    x /= 10;
  }
  return P(a, b);
}

int main() {
  int n;
  cin >> n;
  map<P, int> fr;

  for (int i = 1; i <= n; i++) {
    P p = freq(i);
    fr[p]++;
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    P p = freq(i);
    P q(p.second, p.first);
    ans += fr[q];
  }
  cout << ans << endl;
  return 0;
}
Problem p02793 (C++)

Buggy submission (status: Time Limit Exceeded):
/**
 * @author: Mackenzie
 **/
#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define F first
#define S second
#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define pb push_back
#define fastIO ios::sync_with_stdio(0), cin.tie(0)
#define endl '\n'
typedef long long ll;
ll m = 1e9 + 7;

ll mulmod(ll a, ll b) {
  ll r = a * b - (ll)((long double)a * b / m + .5) * m;
  return r < 0 ? r + m : r;
}
ll expmod(ll b, ll e) {
  if (e == 0)
    return 1;
  if (e & 1)
    return mulmod(b, expmod(b, e - 1));
  b = expmod(b, e >> 1);
  return mulmod(b, b);
}
ll invmod(ll a) { return expmod(a, m - 2); }
ll add(ll a, ll b) {
  a += b;
  if (a >= m)
    a -= m;
  return a;
}
ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); }
int main() {
#ifdef LOCAL
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#else
  fastIO;
#endif
  int n;
  cin >> n;
  vector<ll> A(n), B(n);
  // ll L = 1;
  forn(i, n) cin >> A[i];
  map<ll, ll> Fmax;
  forn(i, n) {
    map<ll, ll> F;
    ll cur = A[i];
    for (ll j = 2; j <= cur; j++) {
      while (cur % j == 0)
        F[j]++, cur /= j;
      Fmax[j] = max(Fmax[j], F[j]);
    }
    if (cur > 1)
      F[cur]++, Fmax[cur] = max(F[cur], Fmax[cur]);
  }
  ll L = 1;
  for (auto x : Fmax) {
    if (x.S != 0) {
      L = mulmod(L, expmod(x.F, x.S));
    }
  }
  ll ans = 0;
  forn(i, n) { ans = add(ans, mulmod(L, invmod(A[i]))); }
  cout << ans << endl;
  return 0;
}

Fixed submission:
/**
 * @author: Mackenzie
 **/
#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define F first
#define S second
#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define pb push_back
#define fastIO ios::sync_with_stdio(0), cin.tie(0)
#define endl '\n'
typedef long long ll;
ll m = 1e9 + 7;

ll mulmod(ll a, ll b) {
  ll r = a * b - (ll)((long double)a * b / m + .5) * m;
  return r < 0 ? r + m : r;
}
ll expmod(ll b, ll e) {
  if (e == 0)
    return 1;
  if (e & 1)
    return mulmod(b, expmod(b, e - 1));
  b = expmod(b, e >> 1);
  return mulmod(b, b);
}
ll invmod(ll a) { return expmod(a, m - 2); }
ll add(ll a, ll b) {
  a += b;
  if (a >= m)
    a -= m;
  return a;
}
ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); }
int main() {
#ifdef LOCAL
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#else
  fastIO;
#endif
  int n;
  cin >> n;
  vector<ll> A(n), B(n);
  // ll L = 1;
  forn(i, n) cin >> A[i];
  map<ll, ll> Fmax;
  forn(i, n) {
    map<ll, ll> F;
    ll cur = A[i];
    for (ll j = 2; j * j <= cur; j++) {
      while (cur % j == 0)
        F[j]++, cur /= j;
      Fmax[j] = max(Fmax[j], F[j]);
    }
    if (cur > 1)
      F[cur]++, Fmax[cur] = max(F[cur], Fmax[cur]);
  }
  ll L = 1;
  for (auto x : Fmax) {
    if (x.S != 0) {
      L = mulmod(L, expmod(x.F, x.S));
    }
  }
  ll ans = 0;
  forn(i, n) { ans = add(ans, mulmod(L, invmod(A[i]))); }
  cout << ans << endl;
  return 0;
}
Problem p02793 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define FILE
#define fr first
#define se second

using namespace std;

const long long N = 2e5 + 7;
const long long inf = 1e9 + 7;
const long long mod = 1e9 + 7;

int n;
int a[N];
int cnt[N];
int pw(int a, int n) {
  int res = 1;
  while (n > 0) {
    if (n & 1) {
      res = res * 1ll * a % mod;
    }
    a = a * 1ll * a % mod;
    n >>= 1;
  }
  return res;
}
int inv(int x) { return pw(x, mod - 2); }
int mult(long long a, int b) { return (a * 1ll * b) % mod; }
int main() {
  ios_base::sync_with_stdio(false);

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    int x = a[i];
    for (int j = 2; j * j <= x; j++) {
      int ans = 0;
      while (x % j == 0) {
        ans++;
        x /= j;
      }
      cnt[j] = max(cnt[j], ans);
    }
    if (x != 1) {
      cnt[x] = max(cnt[x], 1);
    }
  }
  int s = 1;
  for (int i = 2; i < N; i++) {
    s = mult(s, pw(i, cnt[i]));
  }
  int res = 0;
  for (int i = 1; i <= n; i++) {
    res = (res + mult(s, inv(a[i]))) % mod;
  }
  cout << res << "\n";
}


Fixed submission:
#include <bits/stdc++.h>

#define FILE
#define fr first
#define se second

using namespace std;

const long long N = 1e6 + 7;
const long long inf = 1e9 + 7;
const long long mod = 1e9 + 7;

int n;
int a[N];
int cnt[N];
int pw(int a, int n) {
  int res = 1;
  while (n > 0) {
    if (n & 1) {
      res = res * 1ll * a % mod;
    }
    a = a * 1ll * a % mod;
    n >>= 1;
  }
  return res;
}
int inv(int x) { return pw(x, mod - 2); }
int mult(long long a, int b) { return (a * 1ll * b) % mod; }
int main() {
  ios_base::sync_with_stdio(false);

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    int x = a[i];
    for (int j = 2; j * j <= x; j++) {
      int ans = 0;
      while (x % j == 0) {
        ans++;
        x /= j;
      }
      cnt[j] = max(cnt[j], ans);
    }
    if (x != 1) {
      cnt[x] = max(cnt[x], 1);
    }
  }
  int s = 1;
  for (int i = 2; i < N; i++) {
    s = mult(s, pw(i, cnt[i]));
  }
  int res = 0;
  for (int i = 1; i <= n; i++) {
    res = (res + mult(s, inv(a[i]))) % mod;
  }
  cout << res << "\n";
}
Problem p02793 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define lim 100000
typedef long long ll;
using namespace std;
const long long M = 1e9 + 7;
ll p[lim];

ll _pow(ll no, ll p, ll m) {
  ll ans = 1;
  while (p != 0) {
    if (p & 1)
      ans = ans * no % m;
    no = (no % m * no % m) % m;
    p = p >> 1;
    // cout<<ans<<endl;
    // cout<<p<<endl;
  }
  return ans;
}
int main() {

  long long N;
  ll i, j;
  cin >> N;
  ll A[N];

  for (i = 0; i < N; i++) {
    cin >> A[i];
  }

  for (i = 0; i < N; i++) {
    ll number = A[i];
    ll c = 0;

    for (j = 2; j * j <= number; j++) {
      while (number % j == 0) {
        number /= j;
        c++;
      }
      p[j] = max((int)p[j], (int)c);
      c = 0;
    }
    if (number > 1)
      p[number] = max((int)p[number], (int)1);
  }
  ll lcm = 1;

  for (i = 2; i < lim; i++) {
    if (p[i] != 0) {
      // cout<<i<<" "<<p[i]<<endl;
      lcm = (lcm * _pow(i, p[i], M));
      lcm = lcm % M;
    }
    // cout<<lcm<<endl;
  }
  // cout<<lcm;
  ll ans = 0;
  for (i = 0; i < N; i++) {
    ans = ans + lcm * _pow(A[i], M - 2, M);
    ans = ans % M;
  }
  cout << ans;
}


Fixed submission:
#include <iostream>
#define lim 1000001
typedef long long ll;
using namespace std;
const long long M = 1e9 + 7;
ll p[lim];

ll _pow(ll no, ll p, ll m) {
  ll ans = 1;
  while (p != 0) {
    if (p & 1)
      ans = ans * no % m;
    no = (no % m * no % m) % m;
    p = p >> 1;
    // cout<<ans<<endl;
    // cout<<p<<endl;
  }
  return ans;
}
int main() {

  long long N;
  ll i, j;
  cin >> N;
  ll A[N];

  for (i = 0; i < N; i++) {
    cin >> A[i];
  }

  for (i = 0; i < N; i++) {
    ll number = A[i];
    ll c = 0;

    for (j = 2; j * j <= number; j++) {
      while (number % j == 0) {
        number /= j;
        c++;
      }
      p[j] = max((int)p[j], (int)c);
      c = 0;
    }
    if (number > 1)
      p[number] = max((int)p[number], (int)1);
  }
  ll lcm = 1;

  for (i = 2; i < lim; i++) {
    if (p[i] != 0) {
      // cout<<i<<" "<<p[i]<<endl;
      lcm = (lcm * _pow(i, p[i], M));
      lcm = lcm % M;
    }
    // cout<<lcm<<endl;
  }
  // cout<<lcm;
  ll ans = 0;
  for (i = 0; i < N; i++) {
    ans = ans + lcm * _pow(A[i], M - 2, M);
    ans = ans % M;
  }
  cout << ans;
}
Problem p02793 (Python)

Buggy submission (status: Time Limit Exceeded):
from fractions import gcd
import sys

input = sys.stdin.buffer.readline
mod = 10**9 + 7


def modinv(a, mod):
    return pow(a, mod - 2, mod)


n = int(input())
if n == 1:
    print(1)
    exit()
A = list(map(int, input().split()))
lcm = A[0]
for i in range(n):
    g = gcd(lcm, A[i])
    lcm = lcm * A[i] // g
ans = 0
for i in range(n):
    gyaku = modinv(A[i], mod)
    ans += lcm * gyaku
    ans %= mod
print(ans)


Fixed submission:
from fractions import gcd
import sys

input = sys.stdin.buffer.readline
mod = 10**9 + 7


def modinv(a, mod):
    return pow(a, mod - 2, mod)


n = int(input())
if n == 1:
    print(1)
    exit()
A = list(map(int, input().split()))
lcm = A[0]
for i in range(n):
    g = gcd(lcm, A[i])
    lcm *= A[i] // g
ans = 0
for i in range(n):
    gyaku = modinv(A[i], mod)
    ans += lcm * gyaku
    ans %= mod
print(ans)
Problem p02793 (Python)

Buggy submission (status: Time Limit Exceeded):
from fractions import gcd

n = int(input())
aaa = list(map(int, input().split()))
MOD = 10**9 + 7
lcm = aaa[0]
for a in aaa[1:]:
    lcm = lcm * a // gcd(lcm, a)

ans = 0
for a in aaa:
    ans = (ans + lcm // a) % MOD
print(ans)


Fixed submission:
from fractions import gcd

n = int(input())
aaa = list(map(int, input().split()))
MOD = 10**9 + 7
lcm = aaa[0]
for a in aaa[1:]:
    lcm = lcm * a // gcd(lcm, a)

ans = sum(lcm // a for a in aaa) % MOD
print(ans)
Problem p02793 (Python)

Buggy submission (status: Time Limit Exceeded):
from collections import Counter

MOD = 10**9 + 7
N = int(input())
A = list(map(int, input().split()))


def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a


prime = [0] * (10**6 + 1)
for i in range(N):
    c = Counter(prime_factorize(A[i]))
    for j in c.items():
        prime[j[0]] = max(j[1], prime[j[0]])
lcm = 1
for i in range(10**6):
    if prime[i] > 0:
        lcm *= pow(i, prime[i], MOD)

ans = 0
for i in range(N):
    ans += lcm // A[i]
    ans %= MOD
print(ans)


Fixed submission:
from collections import Counter

MOD = 10**9 + 7
N = int(input())
A = list(map(int, input().split()))


def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a


prime = [0] * (10**6 + 1)
for i in range(N):
    c = Counter(prime_factorize(A[i]))
    for j in c.items():
        prime[j[0]] = max(j[1], prime[j[0]])
lcm = 1
for i in range(10**6):
    if prime[i] > 0:
        lcm *= pow(i, prime[i], MOD)

ans = 0
for i in range(N):
    ans += lcm * pow(A[i], MOD - 2, MOD)
    ans %= MOD
print(ans)
Problem p02793 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define MOD (1000000007l)
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)

using namespace std;

ll modinv(ll a) {
  ll M = MOD, b = M, u = 1, v = 0;
  while (b)
    u -= a / b * v, swap(u, v), a %= b, swap(a, b);
  return (u % M + M) % M;
}

void solve() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];

  unordered_map<ll, ll> yakusu;

  rep(i, N) {
    unordered_map<ll, ll> aaa;
    ll tmp = A[i];
    for (ll j = 2; tmp > 1; j++) {
      while (tmp % j == 0) {
        aaa[j]++;
        tmp /= j;
      }
    }
    for (auto it : aaa) {
      yakusu[it.first] = max(yakusu[it.first], it.second);
    }
  }

  ll lcm = 1;
  for (auto it : yakusu) {
    rep(i, it.second) {
      lcm *= it.first;
      lcm %= MOD;
    }
  }

  ll ans = 0;
  rep(i, N) {
    ll tmp = lcm;
    tmp *= modinv(A[i]);
    tmp %= MOD;
    ans += tmp;
    ans %= MOD;
  }

  cout << ans << endl;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout.precision(12);
  cout << fixed;
  solve();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define MOD (1000000007l)
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)

using namespace std;

ll modinv(ll a) {
  ll M = MOD, b = M, u = 1, v = 0;
  while (b)
    u -= a / b * v, swap(u, v), a %= b, swap(a, b);
  return (u % M + M) % M;
}

void solve() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];

  unordered_map<ll, ll> yakusu;

  rep(i, N) {
    unordered_map<ll, ll> aaa;
    ll tmp = A[i];
    for (ll j = 2; tmp > 1; j++) {
      if (j * j > A[i]) {
        aaa[tmp]++;
        tmp = 1;
        break;
      }
      while (tmp % j == 0) {
        aaa[j]++;
        tmp /= j;
      }
    }
    for (auto it : aaa) {
      yakusu[it.first] = max(yakusu[it.first], it.second);
    }
  }

  ll lcm = 1;
  for (auto it : yakusu) {
    rep(i, it.second) {
      lcm *= it.first;
      lcm %= MOD;
    }
  }

  ll ans = 0;
  rep(i, N) {
    ll tmp = lcm;
    tmp *= modinv(A[i]);
    tmp %= MOD;
    ans += tmp;
    ans %= MOD;
  }

  cout << ans << endl;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout.precision(12);
  cout << fixed;
  solve();
  return 0;
}
Problem p02793 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define F first
#define S second
#define PB push_back

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef priority_queue<int> HEAP;
typedef priority_queue<int, vector<int>, greater<int>> RHEAP;

const int N = 10010, M = 100010, mod = 1e9 + 7;

int n;
int a[N], f[M];
int primes[M], cnt;
bool st[M];

int qp(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = (LL)res * a % mod;
    a = (LL)a * a % mod;
    b >>= 1;
  }
  return res;
}

void init(int n) {
  for (int i = 2; i <= n; i++) {
    if (!st[i])
      primes[cnt++] = i;
    for (int j = 0; primes[j] <= n / i; j++) {
      st[primes[j] * i] = true;
      if (i % primes[j] == 0)
        break;
    }
  }
}

int main() {
  init(M - 1);

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];

    int x = a[i];
    for (int i = 2; i <= x / i; i++)
      if (x % i == 0) {
        int cnt = 0;
        while (x % i == 0) {
          cnt++;
          x /= i;
        }
        f[i] = max(f[i], cnt);
      }
    if (x > 1)
      f[x] = max(f[x], 1);
  }

  int s = 1;
  for (int i = 2; i < M; i++)
    s = (LL)s * qp(i, f[i]) % mod;

  int res = 0;
  for (int i = 0; i < n; i++)
    res = (res + (LL)s * qp(a[i], mod - 2) % mod) % mod;
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define F first
#define S second
#define PB push_back

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef priority_queue<int> HEAP;
typedef priority_queue<int, vector<int>, greater<int>> RHEAP;

const int N = 10010, M = 1000010, mod = 1e9 + 7;

int n;
int a[N], f[M];
int primes[M], cnt;
bool st[M];

int qp(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = (LL)res * a % mod;
    a = (LL)a * a % mod;
    b >>= 1;
  }
  return res;
}

void init(int n) {
  for (int i = 2; i <= n; i++) {
    if (!st[i])
      primes[cnt++] = i;
    for (int j = 0; primes[j] <= n / i; j++) {
      st[primes[j] * i] = true;
      if (i % primes[j] == 0)
        break;
    }
  }
}

int main() {
  init(M - 1);

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];

    int x = a[i];
    for (int i = 2; i <= x / i; i++)
      if (x % i == 0) {
        int cnt = 0;
        while (x % i == 0) {
          cnt++;
          x /= i;
        }
        f[i] = max(f[i], cnt);
      }
    if (x > 1)
      f[x] = max(f[x], 1);
  }

  int s = 1;
  for (int i = 2; i < M; i++)
    s = (LL)s * qp(i, f[i]) % mod;

  int res = 0;
  for (int i = 0; i < n; i++)
    res = (res + (LL)s * qp(a[i], mod - 2) % mod) % mod;
  cout << res << endl;
  return 0;
}
Problem p02793 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
long long mod = pow(10, 9) + 7;
long long pow_mod(long long a, long long b) {
  if (b == 0)
    return 1;
  long long k = pow_mod(a, b / 2);
  long long kk = k * k % mod;
  if (b & 1) {
    return kk * a % mod;
  } else {
    return kk;
  }
}
int MA = pow(10, 6);
vector<bool> pf(MA + 1, true);
vector<long long> P;
int main() {
  pf[0] = pf[1] = false;
  for (int i = 2; i <= MA; i++) {
    if (pf[i]) {
      P.push_back(i);
      for (int j = i + i; j <= MA; j += i) {
        pf[j] = false;
      }
    }
  }
  long long N;
  cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    ans += pow_mod(A[i], mod - 2);
    ans %= mod;
  }
  map<int, int> mp;
  for (int i = 0; i < N; i++) {
    long long k = A[i];
    for (auto p : P) {
      int c = 0;
      while (k % p == 0) {
        c++;
        k /= p;
      }
      mp[p] = max(mp[p], c);
      if (k == 1)
        break;
    }
  }
  for (auto x : mp) {
    ans *= pow_mod(x.first, x.second);
    ans %= mod;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long mod = pow(10, 9) + 7;
long long pow_mod(long long a, long long b) {
  if (b == 0)
    return 1;
  long long k = pow_mod(a, b / 2);
  long long kk = k * k % mod;
  if (b & 1) {
    return kk * a % mod;
  } else {
    return kk;
  }
}
int MA = pow(10, 6);
vector<bool> pf(MA + 1, true);
vector<long long> P;
int main() {
  pf[0] = pf[1] = false;
  for (int i = 2; i <= MA; i++) {
    if (pf[i]) {
      P.push_back(i);
      for (int j = i + i; j <= MA; j += i) {
        pf[j] = false;
      }
    }
  }
  long long N;
  cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    ans += pow_mod(A[i], mod - 2);
    ans %= mod;
  }
  map<int, int> mp;
  for (int i = 0; i < N; i++) {
    long long k = A[i];
    for (auto p : P) {
      if (pf[k]) {
        mp[k] = max(mp[k], 1);
        break;
      }
      int c = 0;
      while (k % p == 0) {
        c++;
        k /= p;
      }
      mp[p] = max(mp[p], c);
      if (k == 1)
        break;
    }
  }
  for (auto x : mp) {
    ans *= pow_mod(x.first, x.second);
    ans %= mod;
  }
  cout << ans << endl;
}
Problem p02793 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, a[1005], f[1000005], mod = 1e9 + 7;
int inv[1000005];
signed main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    int t = a[i];
    for (int j = 2; j * j <= a[i]; j++) {
      if (t % j == 0) {
        int sum = 0;
        while (t % j == 0) {
          t /= j;
          sum++;
        }
        f[j] = max(f[j], sum);
      }
    }
    if (t != 1) {
      if (!f[t])
        f[t] = 1;
    }
  }
  int p = 1;
  for (int i = 2; i < 1000000; i++) {
    for (int j = 0; j < f[i]; j++)
      p = p * i % mod;
  }
  inv[1] = 1;
  for (int i = 2; i <= 1000000; i++)
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = ans + p * inv[a[i]];
    ans = ans % mod;
  }
  cout << ans;
  return 0;
}


Fixed submission:
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, a[10005], f[1000005], mod = 1e9 + 7;
int inv[1000005];
signed main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    int t = a[i];
    for (int j = 2; j * j <= a[i]; j++) {
      if (t % j == 0) {
        int sum = 0;
        while (t % j == 0) {
          t /= j;
          sum++;
        }
        f[j] = max(f[j], sum);
      }
    }
    if (t != 1) {
      if (!f[t])
        f[t] = 1;
    }
  }
  int p = 1;
  for (int i = 2; i < 1000000; i++) {
    for (int j = 0; j < f[i]; j++)
      p = p * i % mod;
  }
  inv[1] = 1;
  for (int i = 2; i <= 1000000; i++)
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = ans + p * inv[a[i]];
    ans = ans % mod;
  }
  cout << ans;
  return 0;
}