Problem p02624 (C++) — buggy submission, verdict: Runtime Error



#include <bits/stdc++.h>
#define ll long long
#define PB push_back
#define MP make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, l, h) for (int i = (int)(l); i <= (int)(h); ++i)
#define FORD(i, h, l) for (int i = (int)(h); i >= (int)(l); --i)

// #define max(a,b) a>b?a:b
// #define min(a,b) a>b?b:a

ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

#define dump(x) cerr << #x << " = " << (x) << endl
#define ALL(t) (t).begin(), (t).end()

using namespace std;

#define error(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}

int sum() { return 0; }

template <typename T, typename... Args> T sum(T a, Args... args) {
  return a + sum(args...);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll int n;
  cin >> n;
  std::vector<int> v(n + 1, 0);
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j <= n; j += i) {
      v[j]++;
    }
  }
  ll int sum = 0;
  for (int i = 1; i <= n; ++i) {
    sum += (ll)i * v[i];
  }
  cout << sum;
  return 0;
}


Error: WA

Fixed submission:


#include <bits/stdc++.h>
#define ll long long
#define PB push_back
#define MP make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, l, h) for (int i = (int)(l); i <= (int)(h); ++i)
#define FORD(i, h, l) for (int i = (int)(h); i >= (int)(l); --i)

#define max(a, b) a > b ? a : b
#define min(a, b) a > b ? b : a

ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

#define dump(x) cerr << #x << " = " << (x) << endl
#define ALL(t) (t).begin(), (t).end()

using namespace std;

#define error(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}

int sum() { return 0; }

template <typename T, typename... Args> T sum(T a, Args... args) {
  return a + sum(args...);
}

ll int prime[10000002];
void sieve() {
  prime[0] = 1;
  prime[1] = 1;
  for (long long int i = 2; i <= 1000000; i++)
    prime[i] = i;
  for (long long int i = 2; i * i <= (1000000); i++) {
    if (prime[i] == i) {
      for (long long int j = i * i; j <= 1000000; j += i) {
        if (prime[j] == j)
          prime[j] = i;
      }
    }
  }
}

ll int count(int n) {
  unordered_map<ll, ll> m;
  while (n > 1) {
    m[prime[n]]++;
    n /= prime[n];
  }
  ll int ans = 1;
  for (auto k : m)
    ans *=
        (k.second +
         1); // using the divisor property (p+1)*(q+1) if num. n = (x^p)*(y^q)
  return ans;
}

vector<ll> solve(vector<ll> &A) {
  sieve();
  vector<ll> ans(A.size(), 0);
  for (int i = 0; i < A.size(); i++) {
    ans[i] = count(A[i]);
  }
  return ans;
}

int main() {
  ll int n;
  cin >> n;
  vector<ll> v(n);
  for (int i = 0; i < n; i++)
    v[i] = (i + 1);
  vector<ll> a = solve(v);
  ll int sum = 0;
  for (int i = 0; i < a.size(); i++)
    sum += (ll)(i + 1) * a[i];
  // cout<<a[i]<<" ";
  cout << sum;
  // cout<<endl;
}