Problem p02715 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef vector<vector<int>> v2int;
typedef vector<ll> vll;
typedef vector<vector<ll>> v2ll;
typedef list<int> liint;
typedef pair<int, int> pint;
const int INF = int(2e9);
const ll LINF = ll(2e9) * ll(2e9);
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T, class C> void chmax(T &a, C b) { a > b ?: a = b; }
template <class T, class C> void chmin(T &a, C b) { a < b ?: a = b; }

ll mod(ll val, ll m) {
  ll res = val % m;
  if (res < 0)
    res += m;
  return res;
}

ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

vector<int> prime(int x) {
  vector<int> ans;
  vector<bool> prime(x + 1, true);
  prime[0] = prime[1] = false;
  rep(i, x + 1) {
    if (prime[i]) {
      for (int j = i + i; j < x + 1; j += i) {
        prime[j] = false;
      }
    }
  }

  rep(i, x + 1) {
    if (prime[i] == true)
      ans.push_back(i);
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll N, K;
  cin >> N >> K;

  vll a(K + 1);
  vll pows(K + 1);
  ll p = 1000000007;
  REP(i, 1, K + 1) { pows[i] = modpow(K / i, N, p); }
  auto primes = prime(K);
  ll ans = 0;
  vll subs(K + 1);
  for (int i = K; i >= 0; i--) {
    ll sub = 0;
    REP(j, 1, K + 1) {
      if (ll(i) * ll(j) > K)
        continue;
      sub += a[i * j];
      // sub += pows[ll(i) * ll(prime)]; //a[ll(i) * ll(prime)] + mod(pows[ll(i)
      // * ll(prime)] - a[ll(i) * ll(prime)], p);
      sub %= p;
    }
    a[i] = mod(pows[i] - sub, p);
    ans += i * a[i];
    subs[i] = sub;
    ans %= p;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef vector<vector<int>> v2int;
typedef vector<ll> vll;
typedef vector<vector<ll>> v2ll;
typedef list<int> liint;
typedef pair<int, int> pint;
const int INF = int(2e9);
const ll LINF = ll(2e9) * ll(2e9);
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T, class C> void chmax(T &a, C b) { a > b ?: a = b; }
template <class T, class C> void chmin(T &a, C b) { a < b ?: a = b; }

ll mod(ll val, ll m) {
  ll res = val % m;
  if (res < 0)
    res += m;
  return res;
}

ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

vector<int> prime(int x) {
  vector<int> ans;
  vector<bool> prime(x + 1, true);
  prime[0] = prime[1] = false;
  rep(i, x + 1) {
    if (prime[i]) {
      for (int j = i + i; j < x + 1; j += i) {
        prime[j] = false;
      }
    }
  }

  rep(i, x + 1) {
    if (prime[i] == true)
      ans.push_back(i);
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll N, K;
  cin >> N >> K;

  vll a(K + 1);
  vll pows(K + 1);
  ll p = 1000000007;
  REP(i, 1, K + 1) { pows[i] = modpow(K / i, N, p); }
  auto primes = prime(K);
  ll ans = 0;
  vll subs(K + 1);
  for (int i = K; i >= 0; i--) {
    ll sub = 0;
    REP(j, 1, K + 1) {
      if (ll(i) * ll(j) > K)
        break;
      sub += a[i * j];
      // sub += pows[ll(i) * ll(prime)]; //a[ll(i) * ll(prime)] + mod(pows[ll(i)
      // * ll(prime)] - a[ll(i) * ll(prime)], p);
      sub %= p;
    }
    a[i] = mod(pows[i] - sub, p);
    ans += i * a[i];
    subs[i] = sub;
    ans %= p;
  }
  cout << ans << endl;
  return 0;
}