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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

#define rep(i, N) for (int i = 0; i < (int)N; i++)
#define all(v) (v).begin(), (v).end()

const int INF = 100000002;
const int mod = 1000000007;
// const int mod = 1000000007;
// const int mod = 998244353;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

mint modPow(mint a, int n) {
  // cout << a << endl;
  if (n == 1)
    return a;
  if (n % 2)
    return a * modPow(a, n - 1);
  else
    return modPow(a * a, n / 2);
}

int main() {
  int n, k;
  cin >> n >> k;
  mint ans = 0;
  vector<mint> d(k + 1);
  for (int i = k; i > 0; i--) {
    ll cnt = k / i;
    d[i] = modPow(cnt, n);
    int mult = 2;
    while (k >= mult * i) {
      d[i] -= d[i * mult];

      mult++;
    }
    ans += d[i] * i;
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

#define rep(i, N) for (int i = 0; i < (int)N; i++)
#define all(v) (v).begin(), (v).end()

const int INF = 100000002;
const int mod = 1000000007;
// const int mod = 998244353;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

mint modPow(int b, int p, int mod) {
  mint ret = 1;
  rep(i, p) { ret *= b; }
  return ret;
}

int main() {
  int n, k;
  cin >> n >> k;
  mint ans = 0;
  vector<mint> d(k + 1);
  for (int i = k; i > 0; i--) {
    int cnt = k / i;
    d[i] = modPow(cnt, n, mod);
    int mult = 2;
    while (k >= mult * i) {
      d[i] -= d[i * mult];
      // if(d[i]<0) d[i] += mod;
      mult++;
    }
    ans += d[i] * i;
  }

  cout << ans << endl;
}