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

Buggy submission (status: Runtime Error):
// #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#define repe(i, a, b) for (int i = (a); i <= (b); i++)
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define mst(a, b) memset((a), (b), sizeof(a))
#define gc() gutchar()
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pr;
typedef long long ll;
const double eps = 1e-5;
const int inf = 0x3f3f3f3f;
const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};
const ll mod = 1e9 + 7;
const int N = 1e4 + 5;

ll p[N], mp[N];

ll power(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res % mod;
}

int main() {
  ll n, cs = 0;
  scanf("%lld", &n);
  repe(i, 1, n) {
    scanf("%lld", &p[i]);
    cs = max(cs, p[i]);
  }
  for (ll i = 1; i <= n; i++) {
    ll x = p[i];
    ll y = sqrt(x);
    for (ll j = 2; j <= y; j++) {
      ll cnt = 0;
      while (x % j == 0) {
        x /= j;
        cnt++;
      }
      mp[j] = max(mp[j], cnt);
    }
    if (x != 1)
      mp[x] = max(mp[x], 1ll);
  }
  ll sum = 1;
  for (ll i = 1; i <= cs; i++)
    sum = (sum % mod * power(i, mp[i]) % mod) % mod;
  ll res = 0;
  for (ll i = 1; i <= n; i++)
    res += sum * power(p[i], mod - 2) % mod;
  cout << res % mod << '\n';
  return 0;
}

Fixed submission:
// #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#define repe(i, a, b) for (int i = (a); i <= (b); i++)
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define mst(a, b) memset((a), (b), sizeof(a))
#define gc() gutchar()
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pr;
typedef long long ll;
const double eps = 1e-5;
const int inf = 0x3f3f3f3f;
const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};
const ll mod = 1e9 + 7;
const int N = 1e6 + 5;

ll p[N], mp[N];

ll power(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res % mod;
}

int main() {
  ll n, cs = 0;
  scanf("%lld", &n);
  repe(i, 1, n) {
    scanf("%lld", &p[i]);
    cs = max(cs, p[i]);
  }
  for (ll i = 1; i <= n; i++) {
    ll x = p[i];
    ll y = sqrt(x);
    for (ll j = 2; j <= y; j++) {
      ll cnt = 0;
      while (x % j == 0) {
        x /= j;
        cnt++;
      }
      mp[j] = max(mp[j], cnt);
    }
    if (x != 1)
      mp[x] = max(mp[x], 1ll);
  }
  ll sum = 1;
  for (ll i = 1; i <= cs; i++)
    sum = (sum % mod * power(i, mp[i]) % mod) % mod;
  ll res = 0;
  for (ll i = 1; i <= n; i++)
    res += sum * power(p[i], mod - 2) % mod;
  cout << res % mod << '\n';
  return 0;
}
Problem p02793 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ss second
#define ff first
#define all(x) x.begin(), x.end()

using namespace std;
using ll = long long;
using pii = pair<ll, ll>;

const int oo = 1e9 + 7;
const ll mod = 1e9 + 7, maxn = 1000001;
const double PI = acos(-1);
bitset<1000010> isprime;
vector<int> primos;
int v[maxn];

void sieve() {
  isprime.set();
  isprime[0] = isprime[1] = 0;
  primos.push_back(2);

  for (int i = 3; i * i < maxn; i += 2) {
    if (isprime[i]) {
      for (int j = i * i; j < maxn; j += (2 * i)) {
        isprime[j] = 0;
      }
    }
  }

  for (int i = 3; i < maxn; i += 2) {
    if (isprime[i])
      primos.push_back(i);
  }
}

ll fexp(ll base, ll p) {
  ll ans = 1;
  while (p) {
    if (p & 1) {
      ans = (ans * base) % mod;
    }
    p = p >> 1;
    base = (base * base) % mod;
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  ll n, ans = 0, lcm = 1;
  sieve();
  cin >> n;

  int pn = primos.size();
  vector<ll> pot(pn, 0);

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

  for (auto x : v) {
    for (int i = 0; i < pn; i++) {
      ll val = primos[i], power = 0;
      if (val > x)
        break;
      while (x % val == 0)
        x /= val, power++;
      pot[i] = max(pot[i], power);
    }
  }

  for (int i = 0; i < pn; i++) {
    lcm = (lcm * fexp(primos[i], pot[i])) % mod;
  }

  for (int i = 0; i < n; i++) {
    ans = (ans + lcm * fexp(v[i], mod - 2)) % mod;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ss second
#define ff first
#define all(x) x.begin(), x.end()

using namespace std;
using ll = long long;
using pii = pair<ll, ll>;

const int oo = 1e9 + 7;
const ll mod = 1e9 + 7, maxn = 1000001;
const double PI = acos(-1);
bitset<1000010> isprime;
vector<int> primos;
int v[maxn];

void sieve() {
  isprime.set();
  isprime[0] = isprime[1] = 0;
  primos.push_back(2);

  for (int i = 3; i * i < maxn; i += 2) {
    if (isprime[i]) {
      for (int j = i * i; j < maxn; j += (2 * i)) {
        isprime[j] = 0;
      }
    }
  }

  for (int i = 3; i < maxn; i += 2) {
    if (isprime[i])
      primos.push_back(i);
  }
}

ll fexp(ll base, ll p) {
  ll ans = 1;
  while (p) {
    if (p & 1) {
      ans = (ans * base) % mod;
    }
    p = p >> 1;
    base = (base * base) % mod;
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  ll n, ans = 0, lcm = 1;
  sieve();
  cin >> n;

  int pn = primos.size();
  vector<ll> pot(pn, 0);

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

  for (auto x : v) {
    for (int i = 0; i < pn; i++) {
      if (isprime[x] and x & 1) {
        int pos = lower_bound(all(primos), x) - primos.begin();
        pot[pos] = max(1LL, pot[pos]);
        break;
      }
      ll val = primos[i], power = 0;
      if (val > x)
        break;
      while (x % val == 0)
        x /= val, power++;
      pot[i] = max(pot[i], power);
    }
  }

  for (int i = 0; i < pn; i++) {
    lcm = (lcm * fexp(primos[i], pot[i])) % mod;
  }

  for (int i = 0; i < n; i++) {
    ans = (ans + lcm * fexp(v[i], mod - 2)) % mod;
  }
  cout << ans << endl;
  return 0;
}
Problem p02793 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define MOD 1000000007
#define int long long

using namespace std;
int n = 0;
int arr[10001];
map<int, int> mm[10001];
map<int, int> Map;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  for (int i = 0; i < n; i++) {
    int sqrtN = round(sqrt(arr[i]));
    for (int j = 2; j <= sqrtN; j++) {
      while (arr[i] % j == 0) {
        mm[i][j]++;
        arr[i] /= j;
      }
    }
    if (arr[i] != 1)
      mm[i][arr[i]]++;
    arr[i] = 1;
  }
  for (int i = 0; i < n; i++) {
    for (auto p : mm[i])
      Map[p.first] = max(Map[p.first], p.second);
  }
  int sum = 0;
  for (int i = 0; i < n; i++) {
    for (auto p : mm[i])
      Map[p.first] -= p.second;
    int x = 1;
    for (auto p : Map) {
      for (int j = 0; j < p.second; j++) {
        x = x * p.first % MOD;
      }
    }
    sum = (sum + x) % MOD;
    for (auto p : mm[i])
      Map[p.first] += p.second;
  }
  cout << sum;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define MOD 1000000007
#define int long long

using namespace std;
int n = 0;
int arr[10001];
unordered_map<int, int> mm[10001];
unordered_map<int, int> Map;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  for (int i = 0; i < n; i++) {
    int sqrtN = round(sqrt(arr[i]));
    for (int j = 2; j <= sqrtN; j++) {
      while (arr[i] % j == 0) {
        mm[i][j]++;
        arr[i] /= j;
      }
    }
    if (arr[i] != 1)
      mm[i][arr[i]]++;
    arr[i] = 1;
  }
  for (int i = 0; i < n; i++) {
    for (auto p : mm[i])
      Map[p.first] = max(Map[p.first], p.second);
  }
  int sum = 0;
  for (int i = 0; i < n; i++) {
    for (auto p : mm[i])
      Map[p.first] -= p.second;
    int x = 1;
    for (auto p : Map) {
      for (int j = 0; j < p.second; j++) {
        x = x * p.first % MOD;
      }
    }
    sum = (sum + x) % MOD;
    for (auto p : mm[i])
      Map[p.first] += p.second;
  }
  cout << sum;
  return 0;
}
Problem p02793 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
ll n, a[2000], b[2000], MOD = 1000000007, A, B, ans, d[1000], cnt;
map<ll, ll> mp;
vector<ll> v, so;
ll f(ll x, ll y) {
  if (y == 0)
    return 1;
  if (y % 2 == 0)
    return f(x, y / 2) * f(x, y / 2) % MOD;
  return x * f(x, y - 1) % MOD;
}
ll lcd(ll x, ll y) {
  if (y == 0)
    return x;
  return lcd(y, x % y);
}
int main(void) {
  for (int i = 2; i < 1000; i++) {
    if (d[i] == 0)
      so.push_back(i);
    for (int j = 1; j * i < 1000; j++) {
      d[i * j] = 1;
    }
  }
  // for(auto x:so)cout<<x<<endl;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    A = a[i];
    for (auto x : so) {
      if (A % x == 0 && A != x) {
        cnt = 0;
        while (A % x == 0) {
          A /= x;
          cnt++;
        }
        mp[x] = max(mp[x], cnt);
      }
    }
    if (A != 1)
      mp[A] = max(mp[A], 1LL);
  }
  B = 1;
  for (auto x : mp) {
    B *= f(x.first, x.second);
    B %= MOD;
  }
  // cout<<B<<endl;
  for (int i = 0; i < n; i++) {
    ans += B * f(a[i], MOD - 2);
    ans %= MOD;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
ll n, a[10000], b[10000], MOD = 1000000007, A, B, ans, d[1000], cnt;
map<ll, ll> mp;
vector<ll> v, so;
ll f(ll x, ll y) {
  if (y == 0)
    return 1;
  if (y % 2 == 0)
    return f(x, y / 2) * f(x, y / 2) % MOD;
  return x * f(x, y - 1) % MOD;
}
ll lcd(ll x, ll y) {
  if (y == 0)
    return x;
  return lcd(y, x % y);
}
int main(void) {
  for (int i = 2; i < 1000; i++) {
    if (d[i] == 0)
      so.push_back(i);
    for (int j = 1; j * i < 1000; j++) {
      d[i * j] = 1;
    }
  }
  // for(auto x:so)cout<<x<<endl;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    A = a[i];
    for (auto x : so) {
      if (A % x == 0 && A != x) {
        cnt = 0;
        while (A % x == 0) {
          A /= x;
          cnt++;
        }
        mp[x] = max(mp[x], cnt);
      }
    }
    if (A != 1)
      mp[A] = max(mp[A], 1LL);
  }
  B = 1;
  for (auto x : mp) {
    B *= f(x.first, x.second);
    B %= MOD;
  }
  // cout<<B<<endl;
  for (int i = 0; i < n; i++) {
    ans += B * f(a[i], MOD - 2);
    ans %= MOD;
  }
  cout << ans << endl;
}
Problem p02793 (C++)

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

ll powmod(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b % 2 == 1)
    return (a * powmod(a, b - 1)) % p;
  else {
    ll x = powmod(a, b / 2);
    return (x * x) % p;
  }
}

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  return gcd(a % b, b);
}

ll lcm(ll a, ll b) {
  // cout << a << " " << b << " " << endl;
  return a * b / gcd(a, b);
}

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a.at(i);

  ll l = a.at(0);
  rep(i, n - 1) { l = lcm(l, a.at(i + 1)); }
  l %= p;

  ll ans = 0;
  rep(i, n) {
    ans += powmod(a.at(i), p - 2);
    ans %= p;
  }
  ans *= l;
  ans %= p;
  cout << ans << endl;
  return 0;
}

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

ll powmod(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b % 2 == 1)
    return (a * powmod(a, b - 1)) % p;
  else {
    ll x = powmod(a, b / 2);
    return (x * x) % p;
  }
}

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  return gcd(a % b, b);
}

ll lcm(ll a, ll b) {
  // cout << a << " " << b << " " << endl;
  return a * b / gcd(a, b);
}

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a.at(i);

  vector<int> b(1e6 + 1);

  rep(i, n) {
    int tmp = a.at(i);
    int cnt = 0;
    for (int j = 2; j < 1e3; j++) {
      cnt = 0;
      while (tmp % j == 0) {
        tmp /= j;
        cnt++;
      }
      b.at(j) = max(b.at(j), cnt);
    }
    b.at(tmp) = max(b.at(tmp), 1);
  }

  ll l = 1;
  rep(i, b.size()) {
    if (b.at(i) > 0) {
      l *= powmod(i, b.at(i));
      l %= p;
    }
  }

  ll ans = 0;
  rep(i, n) {
    ans += powmod(a.at(i), p - 2);
    ans %= p;
  }
  ans *= l;
  ans %= p;
  cout << ans << endl;
  return 0;
}
Problem p02793 (C++)

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

typedef long long ll;
typedef pair<int, int> ii;

const int N = 1e4 + 5;
const int maxv = 1e6 + 1;
const int MOD = 1e9 + 7;
int n, t;
int A[N];
int p[maxv];
int cnt[maxv];
ll ans;

ll power(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1) {
      res = (res * a) % MOD;
    }
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}

void init() {
  for (int i = 2; i < maxv; ++i) {
    if (p[i] == 0) {
      p[i] = 1;
      for (int j = i + i; j < maxv; j += i) {
        p[j] = i;
      }
    }
  }
}

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

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> t;
    A[i] = t;
    unordered_map<int, int> ma;
    while (p[t] != 1) {
      int d = p[t];
      ++ma[d];
      t /= d;
    }
    ++ma[t];
    for (auto pr : ma) {
      if (pr.second > cnt[pr.first])
        cnt[pr.first] = pr.second;
    }
  }

  ll m = 1;
  for (int i = 2; i < maxv; ++i) {
    if (cnt[i]) {
      m = m * power(i, cnt[i]) % MOD;
    }
  }
  for (int i = 0; i < n; ++i) {
    ans = (ans + m * power(A[i], MOD - 2) % MOD) % MOD;
  }
  cout << ans << endl;

  return 0;
}


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

typedef long long ll;
typedef pair<int, int> ii;

const int N = 1e4 + 5;
const int maxv = 1e6 + 1;
const int MOD = 1e9 + 7;
int n, t;
int A[N];
int p[maxv];
int cnt[maxv];
ll ans;

ll power(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1) {
      res = (res * a) % MOD;
    }
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}

void init() {
  p[1] = 1;
  for (int i = 2; i < maxv; ++i) {
    if (p[i] == 0) {
      p[i] = 1;
      for (int j = i + i; j < maxv; j += i) {
        p[j] = i;
      }
    }
  }
}

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

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> t;
    A[i] = t;
    unordered_map<int, int> ma;
    while (p[t] != 1) {
      int d = p[t];
      ++ma[d];
      t /= d;
    }
    ++ma[t];
    for (auto pr : ma) {
      if (pr.second > cnt[pr.first])
        cnt[pr.first] = pr.second;
    }
  }

  ll m = 1;
  for (int i = 2; i < maxv; ++i) {
    if (cnt[i]) {
      m = m * power(i, cnt[i]) % MOD;
    }
  }
  for (int i = 0; i < n; ++i) {
    ans = (ans + m * power(A[i], MOD - 2) % MOD) % MOD;
  }
  cout << ans << endl;

  return 0;
}
Problem p02793 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int n, mod = 1e9 + 7;
ll arr[N], arr2[N];
bool found[N] = {};
ll fp(ll base, ll exp) {
  if (exp == 0)
    return 1;
  ll ans = fp(base, exp / 2);
  ans = (ans * ans) % mod;
  if (exp % 2 != 0)
    ans = (ans * (base % mod)) % mod;
  return ans;
}

int main() {
  ll cnt[1000] = {};
  bool primes[1005];
  int n = 1000;
  memset(primes, true, sizeof primes);
  for (int i = 2; i * i <= n; i++) {
    if (primes[i]) {
      for (int j = i * i; j <= n; j += i)
        primes[j] = false;
    }
  }
  vector<int> v;
  ll ans = 1, aa;
  cin >> aa;
  for (int i = 0; i < aa; i++) {
    cin >> arr[i];
    arr2[i] = arr[i];
  }

  for (int i = 2; i <= n; i++) {
    if (primes[i])
      v.push_back(i);
  }
  // cout<<v.size();
  ll lcm = 1;
  for (int i = 0; i < v.size(); i++) {
    for (int j = 0; j < aa; j++) {
      ll k = 0;
      while (arr[j] % v[i] == 0) {
        found[arr[j]] = 1;
        k++;
        arr[j] /= v[i];
      }
      cnt[i] = max(k, cnt[i]);
    }

    lcm *= fp(v[i], cnt[i]);
    lcm %= mod;
  }
  for (int i = 0; i < aa; i++) {
    if (!found[arr[i]]) {
      lcm *= arr[i];
      found[arr[i]] = 1;
      lcm %= mod;
    }
  }
  // cout<<lcm<<endl;

  ll answer = 0;

  for (int i = 0; i < aa; i++) {
    answer += (lcm * fp(arr2[i], mod - 2)) % mod;
    answer %= mod;
    // ans%=mod;
  }
  cout << answer;
  return 0;
}


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
int n, mod = 1e9 + 7;
ll arr[N], arr2[N];
bool found[N] = {};
ll fp(ll base, ll exp) {
  if (exp == 0)
    return 1;
  ll ans = fp(base, exp / 2);
  ans = (ans * ans) % mod;
  if (exp % 2 != 0)
    ans = (ans * (base % mod)) % mod;
  return ans;
}

int main() {
  ll cnt[1000] = {};
  bool primes[1005];
  int n = 1000;
  memset(primes, true, sizeof primes);
  for (int i = 2; i * i <= n; i++) {
    if (primes[i]) {
      for (int j = i * i; j <= n; j += i)
        primes[j] = false;
    }
  }
  vector<int> v;
  ll ans = 1, aa;
  cin >> aa;
  for (int i = 0; i < aa; i++) {
    cin >> arr[i];
    arr2[i] = arr[i];
  }

  for (int i = 2; i <= n; i++) {
    if (primes[i])
      v.push_back(i);
  }
  // cout<<v.size();
  ll lcm = 1;
  for (int i = 0; i < v.size(); i++) {
    for (int j = 0; j < aa; j++) {
      ll k = 0;
      while (arr[j] % v[i] == 0) {
        found[arr[j]] = 1;
        k++;
        arr[j] /= v[i];
      }
      cnt[i] = max(k, cnt[i]);
    }

    lcm *= fp(v[i], cnt[i]);
    lcm %= mod;
  }
  for (int i = 0; i < aa; i++) {
    if (!found[arr[i]]) {
      lcm *= arr[i];
      found[arr[i]] = 1;
      lcm %= mod;
    }
  }
  // cout<<lcm<<endl;

  ll answer = 0;

  for (int i = 0; i < aa; i++) {
    answer += (lcm * fp(arr2[i], mod - 2)) % mod;
    answer %= mod;
    // ans%=mod;
  }
  cout << answer;
  return 0;
}
Problem p02793 (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};

int pow(int x, int p) {
  int res = 1;
  while (p) {
    if (p & 1) {
      res = (res * x) % mod;
    }
    p >>= 1;
    x = (x * x) % mod;
  }
  return res;
}

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

  int n;
  cin >> n;

  vector<int> v(n);
  map<int, int> mp;

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

    int x = v[i];
    for (int j = 2; j * j <= v[i] && x > 1; j++) {
      if (x % j == 0) {
        int cnt = 0;
        while (x % j == 0) {
          cnt++;
          x /= j;
        }
        mp[j] = max(mp[j], cnt);
      }
    }
    if (x > 1)
      mp[x] = max(mp[x], 1ll);
  }
  vector<pii> vp;
  for (auto i : mp) {
    vp.push_back({i.first, i.second});
  }

  int ans = 0;

  for (int i = 0; i < n; i++) {
    int val = 1;
    for (auto j : vp) {
      int x = j.first;
      int cnt = 0;
      while (v[i] % x == 0) {
        cnt++;
        v[i] /= x;
      }

      val = (val * pow(x, j.second - cnt)) % mod;
    }
    ans = (val + ans) % mod;
  }
  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};

int pow(int x, int p) {
  int res = 1;
  while (p) {
    if (p & 1) {
      res = (res * x) % mod;
    }
    p >>= 1;
    x = (x * x) % mod;
  }
  return res;
}

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

  int n;
  cin >> n;

  vector<int> v(n);
  map<int, int> mp;

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

    int x = v[i];
    for (int j = 2; j * j <= v[i] && x > 1; j++) {
      if (x % j == 0) {
        int cnt = 0;
        while (x % j == 0) {
          cnt++;
          x /= j;
        }
        mp[j] = max(mp[j], cnt);
      }
    }
    if (x > 1)
      mp[x] = max(mp[x], 1ll);
  }
  vector<pii> vp;
  for (auto i : mp) {
    vp.push_back({i.first, i.second});
  }

  int ans = 0;

  for (int i = 0; i < n; i++) {
    int val = 1;
    for (auto j : vp) {
      int x = j.first;
      int cnt = 0;
      while (v[i] % x == 0) {
        cnt++;
        v[i] /= x;
      }

      val = (val * pow(x, j.second - cnt)) % mod;
    }
    ans = (val + ans) % mod;
  }
  cout << ans << "\n";
}
Problem p02793 (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};

int pow(int x, int p) {
  int res = 1;
  while (p) {
    if (p & 1) {
      res = (res * x) % mod;
    }
    p >>= 1;
    x = (x * x) % mod;
  }
  return res;
}

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

  int n;
  cin >> n;

  vector<int> v(n);
  map<int, int> mp;

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

    int x = v[i];
    for (int j = 2; j * j <= v[i] && x > 1; j++) {
      if (x % j == 0) {
        int cnt = 0;
        while (x % j == 0) {
          cnt++;
          x /= j;
        }
        mp[j] = max(mp[j], cnt);
      }
    }
    if (x > 1)
      mp[x] = max(mp[x], 1ll);
  }

  int l = 1;

  for (auto i : mp) {
    l = (l * pow(i.first, i.second)) % mod;
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = (ans + (l * pow(v[i], mod - 2)) % mod) % mod;
  }

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

int pow(int x, int p) {
  int res = 1;
  while (p) {
    if (p & 1) {
      res = (res * x) % mod;
    }
    p >>= 1;
    x = (x * x) % mod;
  }
  return res;
}

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

  int n;
  cin >> n;

  vector<int> v(n);
  map<int, int> mp;

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

    int x = v[i];
    for (int j = 2; j * j <= v[i] && x > 1; j++) {
      if (x % j == 0) {
        int cnt = 0;
        while (x % j == 0) {
          cnt++;
          x /= j;
        }
        mp[j] = max(mp[j], cnt);
      }
    }
    if (x > 1)
      mp[x] = max(mp[x], 1ll);
  }

  int l = 1;

  for (auto i : mp) {
    l = (l * pow(i.first, i.second)) % mod;
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = (ans + (l * pow(v[i], mod - 2)) % mod) % mod;
  }

  cout << ans << "\n";
}
Problem p02793 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll spf[1000005], hell = pow(10, 9) + 7, res;
map<ll, ll> m;
ll exp(ll x, ll y) {
  ll res = 1;
  x %= hell;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % hell;
    y >>= 1;
    x = (x * x) % hell;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  spf[1] = 1;
  for (ll i = 2; i * i <= 1000000; i++)
    if (spf[i] == 0) {
      spf[i] = i;
      for (ll j = i * i; j <= 1000000; j += i)
        if (spf[j] == 0)
          spf[j] = i;
    }
  ll n;
  cin >> n;
  for (ll i = 0; i < n; i++) {
    ll x;
    cin >> x;
    res += exp(x, hell - 2);
    res %= hell;
    map<ll, ll> st;
    while (x > 1) {
      if (st.find(spf[x]) == st.end())
        st.insert({spf[x], 0});
      st[spf[x]]++;
      x /= spf[x];
    }
    for (auto it = st.begin(); it != st.end(); it++)
      if (m.find(it->first) == m.end())
        m.insert({it->first, it->second});
      else
        m[it->first] = max(m[it->first], it->second);
  }
  for (auto it = m.begin(); it != m.end(); it++) {
    res *= exp(it->first, it->second);
    res %= hell;
  }
  cout << res;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll spf[1000005], hell = pow(10, 9) + 7, res;
map<ll, ll> m;
ll exp(ll x, ll y) {
  ll res = 1;
  x %= hell;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % hell;
    y >>= 1;
    x = (x * x) % hell;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  spf[1] = 1;
  for (ll i = 2; i * i <= 1000000; i++)
    if (spf[i] == 0) {
      spf[i] = i;
      for (ll j = i * i; j <= 1000000; j += i)
        if (spf[j] == 0)
          spf[j] = i;
    }
  for (ll i = 1; i <= 1000000; i++)
    if (spf[i] == 0)
      spf[i] = i;
  ll n;
  cin >> n;
  for (ll i = 0; i < n; i++) {
    ll x;
    cin >> x;
    res += exp(x, hell - 2);
    res %= hell;
    map<ll, ll> st;
    while (x > 1) {
      if (st.find(spf[x]) == st.end())
        st.insert({spf[x], 0});
      st[spf[x]]++;
      x /= spf[x];
    }
    for (auto it = st.begin(); it != st.end(); it++)
      if (m.find(it->first) == m.end())
        m.insert({it->first, it->second});
      else
        m[it->first] = max(m[it->first], it->second);
  }
  for (auto it = m.begin(); it != m.end(); it++) {
    res *= exp(it->first, it->second);
    res %= hell;
  }
  cout << res;
}
Problem p02793 (C++)

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

const int MAX_N = 2000005;
const long long MOD = 1000000007;

int n;

long long ans;

int arr[MAX_N];

std::map<int, int> mp;

void update(int val) {
  int d, exp;
  d = 2;
  while (d * d <= val) {
    exp = 0;
    while (val % d == 0) {
      ++exp;
      val /= d;
    }
    if (exp > 0) {
      mp[d] = std::max(mp[d], exp);
    }
    d += 2 - (d == 2);
  }
  if (val > 1) {
    mp[val] = std::max(mp[val], 1);
  }
}

int get_times(int n, int d) {
  int cnt;
  cnt = 0;
  while (n % d == 0) {
    ++cnt;
    n /= d;
  }
  return cnt;
}

long long lgpow(long long base, long long exp) {
  long long ret;
  ret = 1;
  while (exp > 0) {
    if ((exp & 1) > 0) {
      ret = (ret * base) % MOD;
    }
    base = (base * base) % MOD;
    exp >>= 1;
  }
  return ret;
}

void query(int val) {
  long long sum, pow;
  int e;
  sum = pow = 1;
  for (auto div : mp) {
    e = get_times(val, div.first);
    if (e < div.second) {
      pow = lgpow(div.first, div.second - e);
      sum = (sum * pow) % MOD;
    }
  }
  ans = (ans + sum) % MOD;
}

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);
  std::cout.tie(0);
  std::cin >> n;
  for (int i = 1; i <= n; ++i) {
    std::cin >> arr[i];
    update(arr[i]);
  }
  for (int i = 1; i <= n; ++i) {
    query(arr[i]);
  }
  std::cout << ans << "\n";
  return 0;
}


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

const int MAX_N = 2000005;
const long long MOD = 1000000007;

int n;

long long ans;

int arr[MAX_N];

std::unordered_map<int, int> mp;

void update(int val) {
  int d, exp;
  d = 2;
  while (d * d <= val) {
    exp = 0;
    while (val % d == 0) {
      ++exp;
      val /= d;
    }
    if (exp > 0) {
      mp[d] = std::max(mp[d], exp);
    }
    d += 2 - (d == 2);
  }
  if (val > 1) {
    mp[val] = std::max(mp[val], 1);
  }
}

int get_times(int n, int d) {
  int cnt;
  cnt = 0;
  while (n % d == 0) {
    ++cnt;
    n /= d;
  }
  return cnt;
}

long long lgpow(long long base, long long exp) {
  long long ret;
  ret = 1;
  while (exp > 0) {
    if ((exp & 1) > 0) {
      ret = (ret * base) % MOD;
    }
    base = (base * base) % MOD;
    exp >>= 1;
  }
  return ret;
}

void query(int val) {
  long long sum, pow;
  int e;
  sum = pow = 1;
  for (auto div : mp) {
    e = get_times(val, div.first);
    if (e < div.second) {
      pow = lgpow(div.first, div.second - e);
      sum = (sum * pow) % MOD;
    }
  }
  ans = (ans + sum) % MOD;
}

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);
  std::cout.tie(0);
  std::cin >> n;
  for (int i = 1; i <= n; ++i) {
    std::cin >> arr[i];
    update(arr[i]);
  }
  for (int i = 1; i <= n; ++i) {
    query(arr[i]);
  }
  std::cout << ans << "\n";
  return 0;
}
Problem p02793 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll mod = 1e9 + 7;

ll mpow(ll x,
        ll n) { // x^n(mod M) ←普通にpow(x,n)では溢れてしまうため，随時mod計算
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % mod;
    x = x * x % mod;
    n = n >> 1;
  }
  return ans;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<ll> so(1e6 + 1, 0);
  for (ll i = 0; i < n; i++) {
    map<ll, ll> tso;
    ll temp = a[i];
    while (temp > 1) {
      for (ll i = 1; i < sqrt(temp) + 1; i++) {
        if (temp % i == 0) {
          temp /= i;
          tso[i]++;
          break;
        } else if (i >= sqrt(temp)) {
          tso[temp]++;
          temp = 1;
        }
      }
    }
    for (auto p : tso) {
      so[p.first] = max(so[p.first], p.second);
    }
  }
  ll bai = 1;
  for (ll i = 1; i <= 1e6; i++) {
    if (so[i] > 0) {
      bai = bai * mpow(i, so[i]) % mod;
    }
  }
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    ans += bai * mpow(a[i], mod - 2) % mod;
    ans %= mod;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll mod = 1e9 + 7;

ll mpow(ll x,
        ll n) { // x^n(mod M) ←普通にpow(x,n)では溢れてしまうため，随時mod計算
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % mod;
    x = x * x % mod;
    n = n >> 1;
  }
  return ans;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<ll> so(1e6 + 1, 0);
  for (ll i = 0; i < n; i++) {
    map<ll, ll> tso;
    ll temp = a[i];
    while (temp > 1) {
      for (ll i = 2; i < sqrt(temp) + 1; i++) {
        if (temp % i == 0) {
          temp /= i;
          tso[i]++;
          break;
        } else if (i >= sqrt(temp)) {
          tso[temp]++;
          temp = 1;
        }
      }
    }
    for (auto p : tso) {
      so[p.first] = max(so[p.first], p.second);
    }
  }
  ll bai = 1;
  for (ll i = 1; i <= 1e6; i++) {
    if (so[i] > 0) {
      bai = bai * mpow(i, so[i]) % mod;
    }
  }
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    ans += bai * mpow(a[i], mod - 2) % mod;
    ans %= mod;
  }
  cout << ans << endl;
}
Problem p02793 (Python)

Buggy submission (status: Runtime Error):
def lcm(a, b):
    from math import gcd

    return a // gcd(a, b) * b


def main():
    _ = int(input())
    a = list(map(int, input().split()))
    x = 1

    for e in a:
        x = lcm(x, e)

    ans = 0
    for e in a:
        ans += x // e

    print(ans % int(1e9 + 7))


main()


Fixed submission:
def lcm(a, b):
    from fractions import gcd

    return a // gcd(a, b) * b


def main():
    _ = int(input())
    a = list(map(int, input().split()))
    x = 1

    for e in a:
        x = lcm(x, e)

    ans = 0
    for e in a:
        ans += x // e

    print(ans % int(1e9 + 7))


main()
Problem p02793 (C++)

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

const long long mod = 1e9 + 7;
const long long maxn = 1e4 + 10;

long long n;
long long ans;
long long lca;
long long a[maxn];
long long alpha[maxn];

void in();
void solve();
void upd_lca(long long);
long long pwr(long long, long long);
void out();

int main() {
  in();
  solve();
  out();
}

void in() {
  ios::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  cin >> n;
  for (long long i = 0; i < n; i++)
    cin >> a[i];
}

void solve() {
  lca = 1;
  for (long long i = 0; i < n; i++)
    upd_lca(a[i]);
  for (long long i = 0; i < n; i++) {
    ans += lca * pwr(a[i], -1) % mod;
    ans %= mod;
  }
}

void upd_lca(long long x) {
  for (long long i = 2; i * i <= x; i++) {
    long long tmp = 0;
    while (x % i == 0) {
      tmp++;
      x /= i;
    }
    if (tmp > alpha[i]) {
      lca = lca * pwr(i, tmp - alpha[i]) % mod;
      alpha[i] = tmp;
    }
  }
  if (x != 1) {
    if (1 > alpha[x]) {
      lca = lca * pwr(x, 1 - alpha[x]) % mod;
      alpha[x] = 1;
    }
  }
}

long long pwr(long long a, long long b) {
  if (b < 0)
    return pwr(a, b + mod - 1);
  if (b == 0)
    return 1;
  long long c = pwr(a, b / 2);
  if (b % 2)
    return c * c % mod * a % mod;
  return c * c % mod;
}

void out() { cout << ans; }


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

const long long mod = 1e9 + 7;
const long long maxn = 1e6 + 10;

long long n;
long long ans;
long long lca;
long long a[maxn];
long long alpha[maxn];

void in();
void solve();
void upd_lca(long long);
long long pwr(long long, long long);
void out();

int main() {
  in();
  solve();
  out();
}

void in() {
  ios::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  cin >> n;
  for (long long i = 0; i < n; i++)
    cin >> a[i];
}

void solve() {
  lca = 1;
  for (long long i = 0; i < n; i++)
    upd_lca(a[i]);
  for (long long i = 0; i < n; i++) {
    ans += lca * pwr(a[i], -1) % mod;
    ans %= mod;
  }
}

void upd_lca(long long x) {
  for (long long i = 2; i * i <= x; i++) {
    long long tmp = 0;
    while (x % i == 0) {
      tmp++;
      x /= i;
    }
    if (tmp > alpha[i]) {
      lca = lca * pwr(i, tmp - alpha[i]) % mod;
      alpha[i] = tmp;
    }
  }
  if (x != 1) {
    if (1 > alpha[x]) {
      lca = lca * pwr(x, 1 - alpha[x]) % mod;
      alpha[x] = 1;
    }
  }
}

long long pwr(long long a, long long b) {
  if (b < 0)
    return pwr(a, b + mod - 1);
  if (b == 0)
    return 1;
  long long c = pwr(a, b / 2);
  if (b % 2)
    return c * c % mod * a % mod;
  return c * c % mod;
}

void out() { cout << ans; }
Problem p02794 (C++)

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

#define ll long long
#define fornum(A, B, C) for (A = B; A < C; ++A)
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;

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

ll N, M;
vector<pll> abv[55];
ll mk[22];
ll dp[50][1 << 21];
ll i, j, k;

bool dfs(ll a, ll p, ll g, ll l) {
  if (g == a) {
    return true;
  }
  ll i = 0, j = 0;
  fornum(i, 0, abv[a].size()) {
    ll b = abv[a][i].first;
    if (b == p) {
      continue;
    }
    if (dfs(b, a, g, l)) {
      mk[l] |= (1ll << abv[a][i].second);
      return true;
    }
  }
  return false;
}
int main() {
  scanf("%lld", &N);
  fornum(i, 1, N) {
    ll a, b;
    scanf("%lld%lld", &a, &b);
    abv[a].push_back({b, i});
    abv[b].push_back({a, i});
  }
  scanf("%lld", &M);
  fornum(i, 0, M) {
    ll u, v;
    scanf("%lld%lld", &u, &v);
    dfs(u, 0, v, i);
  }
  /*
  fornum(i,0,M){
      printf("%lld\n", mk[i]);
  }*/
  dp[1][0] = 1;
  fornum(i, 1, N) {
    ll a = 0;
    fornum(j, 0, M) {
      if (mk[j] & (1ll << i))
        a |= 1ll << j;
    }
    fornum(j, 0, 1 << M) {
      // printf("%lld ", dp[i][j]);
      ll b = j | a;
      dp[i + 1][j] += dp[i][j];
      dp[i + 1][b] += dp[i][j];
    }
    // printf("\n");
  }
  printf("%lld", dp[N][(1ll << M) - 1]);
  return 0;
}

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

#define ll long long
#define fornum(A, B, C) for (A = B; A < C; ++A)
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;

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

ll N, M;
vector<pll> abv[55];
ll mk[55];
ll dp[55][1 << 21];
ll i, j, k;

bool dfs(ll a, ll p, ll g, ll l) {
  if (g == a) {
    return true;
  }
  ll i = 0, j = 0;
  fornum(i, 0, abv[a].size()) {
    ll b = abv[a][i].first;
    if (b == p) {
      continue;
    }
    if (dfs(b, a, g, l)) {
      mk[l] |= (1ll << abv[a][i].second);
      return true;
    }
  }
  return false;
}
int main() {
  scanf("%lld", &N);
  fornum(i, 1, N) {
    ll a, b;
    scanf("%lld%lld", &a, &b);
    abv[a].push_back({b, i});
    abv[b].push_back({a, i});
  }
  scanf("%lld", &M);
  fornum(i, 0, M) {
    ll u, v;
    scanf("%lld%lld", &u, &v);
    dfs(u, 0, v, i);
  }
  /*
  fornum(i,0,M){
      printf("%lld\n", mk[i]);
  }*/
  dp[1][0] = 1;
  fornum(i, 1, N) {
    ll a = 0;
    fornum(j, 0, M) {
      if (mk[j] & (1ll << i))
        a |= 1ll << j;
    }
    fornum(j, 0, 1 << M) {
      // printf("%lld ", dp[i][j]);
      ll b = j | a;
      dp[i + 1][j] += dp[i][j];
      dp[i + 1][b] += dp[i][j];
    }
    // printf("\n");
  }
  printf("%lld", dp[N][(1ll << M) - 1]);
  return 0;
}
Problem p02794 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

struct Edge {
  int id;
  int to;
};

using Graph = vector<vector<Edge>>;

Graph g;
int mask[20];
int dp[1 << 20];
bool dfs(int v, int pre, int dest, int bit) {
  if (v == dest)
    return true;
  for (auto &e : g[v]) {
    if (e.to == pre)
      continue;
    bool ret = dfs(e.to, v, dest, bit);
    if (ret) {
      mask[e.id] |= bit;
      return true;
    }
  }
  return false;
}

int main() {
  int n;
  cin >> n;
  g.resize(n);
  for (int i = 0; i + 1 < n; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g[a].push_back({i, b});
    g[b].push_back({i, a});
  }

  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    dfs(u, -1, v, 1 << i);
  }
  dp[0] = 1;
  for (int i = 0; i < n - 1; i++) {
    for (int j = (1 << m) - 1; j >= 0; j--) {
      dp[j | mask[i]] += dp[j];
    }
  }
  cout << dp[(1 << m) - 1] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

struct Edge {
  int id;
  int to;
};

using Graph = vector<vector<Edge>>;

Graph g;
int mask[50];
ll dp[1 << 20];
bool dfs(int v, int pre, int dest, int bit) {
  if (v == dest)
    return true;
  for (auto &e : g[v]) {
    if (e.to == pre)
      continue;
    bool ret = dfs(e.to, v, dest, bit);
    if (ret) {
      mask[e.id] |= bit;
      return true;
    }
  }
  return false;
}

int main() {
  int n;
  cin >> n;
  g.resize(n);
  for (int i = 0; i + 1 < n; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g[a].push_back({i, b});
    g[b].push_back({i, a});
  }

  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    dfs(u, -1, v, 1 << i);
  }
  dp[0] = 1;
  for (int i = 0; i < n - 1; i++) {
    for (int j = (1 << m) - 1; j >= 0; j--) {
      dp[j | mask[i]] += dp[j];
    }
  }
  cout << dp[(1 << m) - 1] << endl;
  return 0;
}
Problem p02794 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long N;
  cin >> N;
  vector<vector<pair<long, long>>> A(N);
  for (long i = 0; i < N - 1; i++) {
    long a, b;
    cin >> a >> b;
    A[a - 1].push_back(pair<long, long>(b - 1, i));
    A[b - 1].push_back(pair<long, long>(a - 1, i));
  }
  long M;
  cin >> M;
  vector<long> U(M), V(M);
  for (long i = 0; i < M; i++)
    cin >> U[i] >> V[i];

  vector<bitset<50>> B(M);
  for (long i = 0; i < M; i++) {
    queue<long> q;
    q.push(U[i] - 1);
    vector<bitset<50>> P(N);
    while (q.size()) {
      long f = q.front();
      q.pop();
      for (auto n : A[f])
        if (P[n.first].none() && n.first != U[i] - 1) {
          P[n.first] = P[f];
          P[n.first].set(n.second);
          q.push(n.first);
        }
    }
    B[i] = P[V[i] - 1];
  }
  long ans = 0;
  for (long i = 0; i < (1 << M); i++) {
    bitset<50> b(i), c;
    for (long j = 0; j < M; j++)
      if (b.test(j))
        c |= B[j];
    ans += (1 - ((long)b.count() % 2) * 2) * (1 << (N - 1 - c.count()));
  }
  cout << ans;
  assert(ans >= 0);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long N;
  cin >> N;
  vector<vector<pair<long, long>>> A(N);
  for (long i = 0; i < N - 1; i++) {
    long a, b;
    cin >> a >> b;
    A[a - 1].push_back(pair<long, long>(b - 1, i));
    A[b - 1].push_back(pair<long, long>(a - 1, i));
  }
  long M;
  cin >> M;
  vector<long> U(M), V(M);
  for (long i = 0; i < M; i++)
    cin >> U[i] >> V[i];

  vector<bitset<50>> B(M);
  for (long i = 0; i < M; i++) {
    queue<long> q;
    q.push(U[i] - 1);
    vector<bitset<50>> P(N);
    while (q.size()) {
      long f = q.front();
      q.pop();
      for (auto n : A[f])
        if (P[n.first].none() && n.first != U[i] - 1) {
          P[n.first] = P[f];
          P[n.first].set(n.second);
          q.push(n.first);
        }
    }
    B[i] = P[V[i] - 1];
  }
  long ans = 0;
  for (long i = 0; i < (1 << M); i++) {
    bitset<50> b(i), c;
    for (long j = 0; j < M; j++)
      if (b.test(j))
        c |= B[j];
    ans += (1L - ((long)b.count() % 2L) * 2L) * (1L << (N - 1L - c.count()));
    assert(1L << (N - 1L - c.count()) > 0);
  }
  cout << ans;
  assert(ans >= 0);
}
Problem p02794 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

struct Edge {
  int to, id;
  Edge(int to, int id) : to(to), id(id) {}
};

vector<Edge> g[55];

vector<int> es;
bool dfs(int v, int tv, int p = -1) {
  if (v == tv)
    return true;
  for (Edge e : g[v]) {
    if (e.to == p)
      continue;
    if (dfs(e.to, tv, v)) {
      es.push_back(e.id);
      return true;
    }
  }
  return false;
}

int main() {
  int n, m;
  cin >> n >> m;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g[a].emplace_back(b, i);
    g[b].emplace_back(a, i);
  }

  cin >> m;
  vector<ll> eset(m);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    es = vector<int>();
    dfs(a, b);
    for (int e : es) {
      eset[i] |= 1ll << e;
    }
  }

  ll ans = 0;
  rep(i, 1 << m) {
    ll mask = 0;
    rep(j, m) {
      if (i >> j & 1)
        mask |= eset[j];
    }
    int white = __builtin_popcountll(mask);
    ll now = 1ll << (n - 1 - white);
    if (__builtin_popcountll(i) % 2 == 0)
      ans += now;
    else
      ans -= now;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

struct Edge {
  int to, id;
  Edge(int to, int id) : to(to), id(id) {}
};

vector<Edge> g[55];

vector<int> es;
bool dfs(int v, int tv, int p = -1) {
  if (v == tv)
    return true;
  for (Edge e : g[v]) {
    if (e.to == p)
      continue;
    if (dfs(e.to, tv, v)) {
      es.push_back(e.id);
      return true;
    }
  }
  return false;
}

int main() {
  int n, m;
  cin >> n;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g[a].emplace_back(b, i);
    g[b].emplace_back(a, i);
  }

  cin >> m;
  vector<ll> eset(m);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    es = vector<int>();
    dfs(a, b);
    for (int e : es) {
      eset[i] |= 1ll << e;
    }
  }

  ll ans = 0;
  rep(i, 1 << m) {
    ll mask = 0;
    rep(j, m) {
      if (i >> j & 1)
        mask |= eset[j];
    }
    int white = __builtin_popcountll(mask);
    ll now = 1ll << (n - 1 - white);
    if (__builtin_popcountll(i) % 2 == 0)
      ans += now;
    else
      ans -= now;
  }
  cout << ans << endl;
}
Problem p02794 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 50;
const int BIT = (1 << 20);
int n, m, d[N];
vector<int> g[N];
ll bit[BIT];
vector<int> v;
void dfs_pre(int fa, int u, int src, int dest, int idx) {
  v.push_back(u);
  if (u == dest) {
    for (int i = 1; i < v.size(); i++) {
      int t = v[i];
      if (d[v[i]] < d[v[i - 1]])
        t = v[i - 1];
      bit[1 << idx] |= (1LL << t);
    }
  }
  for (int v : g[u]) {
    if (v == fa)
      continue;
    if (dest == -1)
      d[v] = d[u] + 1;
    dfs_pre(u, v, src, dest, idx);
  }
  v.pop_back();
}
int get_count(ll x) {
  int t = 0;
  while (x) {
    if (x & 1)
      t++;
    x /= 2;
  }
  return t;
}
int main() {
  cin >> n;
  for (int i = 1, u, v; i < n; i++) {
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  cin >> m;
  dfs_pre(-1, 1, 1, -1, 0);
  for (int i = 0, u, v; i < m; i++) {
    cin >> u >> v;
    dfs_pre(-1, u, u, v, i);
  }
  ll ans = (1LL << (n - 1));
  for (int i = 1; i < (1 << m); i++) {
    bit[i] = bit[i & -i] | bit[i - (i & -i)];
    int c = get_count(bit[i]);
    ll res = (1LL << (n - 1 - c)) * ((get_count(i) & 1) ? 1 : -1);
    ans -= res;
  }
  cout << ans << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 52;
const int BIT = (1 << 20);
int n, m, d[N];
vector<int> g[N];
ll bit[BIT];
vector<int> v;
void dfs_pre(int fa, int u, int src, int dest, int idx) {
  v.push_back(u);
  if (u == dest) {
    for (int i = 1; i < v.size(); i++) {
      int t = v[i];
      if (d[v[i]] < d[v[i - 1]])
        t = v[i - 1];
      bit[1 << idx] |= (1LL << t);
    }
  }
  for (int v : g[u]) {
    if (v == fa)
      continue;
    if (dest == -1)
      d[v] = d[u] + 1;
    dfs_pre(u, v, src, dest, idx);
  }
  v.pop_back();
}
int get_count(ll x) {
  int t = 0;
  while (x) {
    if (x & 1)
      t++;
    x /= 2;
  }
  return t;
}
int main() {
  cin >> n;
  for (int i = 1, u, v; i < n; i++) {
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  cin >> m;
  dfs_pre(-1, 1, 1, -1, 0);
  for (int i = 0, u, v; i < m; i++) {
    cin >> u >> v;
    dfs_pre(-1, u, u, v, i);
  }
  ll ans = (1LL << (n - 1));
  for (int i = 1; i < (1 << m); i++) {
    bit[i] = bit[i & -i] | bit[i - (i & -i)];
    int c = get_count(bit[i]);
    ll res = (1LL << (n - 1 - c)) * ((get_count(i) & 1) ? 1 : -1);
    ans -= res;
  }
  cout << ans << '\n';
  return 0;
}
Problem p02794 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
using namespace std;

typedef long long ll;
typedef double db;
typedef long double ldb;

const int inf = 1e9 + 10;
const int maxn = 55;

ll dp[maxn];
int xx[maxn], n, m;
vector<pair<int, int>> g[maxn];

bool dfs(int x, int fa, int ed, int id) {
  if (x == ed)
    return true;
  for (int i = 0; i < g[x].size(); i++) {
    int to = g[x][i].fst;
    if (to != fa)
      if (dfs(to, x, ed, id)) {
        xx[g[x][i].snd] |= 1 << id;
        return true;
      }
  }
  return false;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--;
    v--;
    g[u].pb(mp(v, i));
    g[v].pb(mp(u, i));
  }
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--;
    v--;
    dfs(u, -1, v, i);
  }
  dp[0] = 1ll;
  for (int i = 0; i < n - 1; i++)
    for (int mask = (1 << m) - 1; mask >= 0; mask--)
      dp[mask | xx[i]] += dp[mask];
  printf("%lld\n", dp[(1 << m) - 1]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
using namespace std;

typedef long long ll;
typedef double db;
typedef long double ldb;

const int inf = 1e9 + 10;
const int maxn = 55;

ll dp[1 << 21];
int xx[maxn], n, m;
vector<pair<int, int>> g[maxn];

bool dfs(int x, int fa, int ed, int id) {
  if (x == ed)
    return true;
  for (int i = 0; i < g[x].size(); i++) {
    int to = g[x][i].fst;
    if (to != fa)
      if (dfs(to, x, ed, id)) {
        xx[g[x][i].snd] |= 1 << id;
        return true;
      }
  }
  return false;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--;
    v--;
    g[u].pb(mp(v, i));
    g[v].pb(mp(u, i));
  }
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--;
    v--;
    dfs(u, -1, v, i);
  }
  dp[0] = 1ll;
  for (int i = 0; i < n - 1; i++)
    for (int mask = (1 << m) - 1; mask >= 0; mask--)
      dp[mask | xx[i]] += dp[mask];
  printf("%lld\n", dp[(1 << m) - 1]);
  return 0;
}