Problem p02624 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#include <chrono>
#include <complex>
#define endl "\\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define mod2 998244353
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define sz(v) ((ll)(v).size())
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define pb push_back
#define f first
#define s second
#define dbg(a) cout << #a << " --> " << (a) << "\\n";
#define forn(i, n) for (ll i = 0; i < (n); ++i)
#define fornd(i, n) for (ll i = (n)-1; i >= 0; --i)
#define forab(i, a, b) for (ll i = (a); i <= (b); ++i)
#define fast_io ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
using namespace std;
bool isPowerOfTwo(int x) { return x && (!(x & (x - 1))); }
int takemod(int x) { return (x % mod + mod) % mod; }

int fpow(int base, int power, int MOD) {
  int result = 1;
  base = base % MOD;
  while (power) {
    if (power & 1)
      result = (result * base) % MOD;
    base = (base * base) % MOD;
    power = power >> 1;
  }
  return result;
}

int modinv(int x) { return fpow(x, mod - 2, mod); }
ll prime[int(1e7 + 10)] = {0};
#define N 10000010
void Sieve(ll n) {
  // Create a boolean array "prime[0..n]" and initialize
  // all entries it as true. A value in prime[i] will
  // finally be false if i is Not a prime, else true.
  // memset(prime, true, sizeof(prime));

  for (ll p = 1; p <= N + 10; p++) {
    // If prime[p] is not changed, then it is a prime

    // Update all multiples of p greater than or
    // equal to the square of it
    // numbers which are multiple of p and are
    // less than p^2 are already been marked.
    for (int i = p; i <= N + 10; i += p)
      prime[i]++;
  }

  // Print all prime numbers
}
void solve() {
  ll n;
  cin >> n;
  unsigned long long ans = 0;
  Sieve(n);
  // forab(i,1,n+3)
  // 	cout<<prime[i]<<" ";
  // 	cout<<endl;
  forab(i, 1, n) { ans += i * (prime[i]); }
  cout << ans << endl;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast_io;
  int T = 1;
  // cin>>T;
  //  cin.ignore(); must be there when using getline(cin, s)
  while (T--) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:


#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#include <chrono>
#include <complex>
#define endl "\\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define mod2 998244353
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define sz(v) ((ll)(v).size())
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define pb push_back
#define f first
#define s second
#define dbg(a) cout << #a << " --> " << (a) << "\\n";
#define forn(i, n) for (ll i = 0; i < (n); ++i)
#define fornd(i, n) for (ll i = (n)-1; i >= 0; --i)
#define forab(i, a, b) for (ll i = (a); i <= (b); ++i)
#define fast_io ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
using namespace std;
bool isPowerOfTwo(int x) { return x && (!(x & (x - 1))); }
int takemod(int x) { return (x % mod + mod) % mod; }

int fpow(int base, int power, int MOD) {
  int result = 1;
  base = base % MOD;
  while (power) {
    if (power & 1)
      result = (result * base) % MOD;
    base = (base * base) % MOD;
    power = power >> 1;
  }
  return result;
}

int modinv(int x) { return fpow(x, mod - 2, mod); }

ll count(ll n) {
  ll cnt = 0;
  for (ll i = 1; i <= sqrt(n); i++) {
    if (n % i == 0) {
      // If divisors are equal,
      // count only one
      if (n / i == i)
        cnt++;

      else // Otherwise count both
        cnt = cnt + 2;
    }
  }
  return cnt;
}
void solve() {
  ll n;
  cin >> n;
  ll ans = 0;
  forab(i, 1, n) { ans += i * count(i); }
  cout << ans << endl;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast_io;
  int T = 1;
  // cin>>T;
  //  cin.ignore(); must be there when using getline(cin, s)
  while (T--) {
    solve();
  }
  return 0;
}