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

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

typedef int64_t Int;
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()

const double EPS = 1e-10;
const Int INF = 1e18;
const int inf = 1e9;
const Int mod = 1e9 + 7;

bool print_space_enable = false;
void print() {
  cout << endl;
  print_space_enable = false;
}

template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
  if (print_space_enable)
    cout << " ";
  cout << fixed << setprecision(15) << head;
  print_space_enable = true;
  print(std::forward<Tail>(tail)...);
}

template <typename T> void print(vector<T> v) {
  for (size_t i = 0; i < v.size(); i++) {
    if (i > 0)
      std::cout << " ";
    std::cout << v[i];
  }
  std::cout << std::endl;
}

using ll = long long int;
ll RepeatSquaring(ll N, ll P, ll M) {
  if (P == 0)
    return 1;
  if (P % 2 == 0) {
    ll t = RepeatSquaring(N, P / 2, M);
    return t * t % M;
  }
  return N * RepeatSquaring(N, P - 1, M) % M;
}

Int pow(Int x, Int n) { // x^n 計算量O(logn)
  Int ans = 1;
  while (n > 0) {
    if ((n & 1) == 1) {
      ans = ans * x % mod;
    }
    x = x * x; // 一周する度にx, x^2, x^4, x^8となる
    n >>= 1;   // 桁をずらす n = n >> 1
  }
  return ans;
}

int main() {
  Int n, k;
  cin >> n >> k;
  vector<Int> a(k + 1, 0);
  Int res = 0;
  for (Int i = k; (Int)i >= 1; i--) {
    Int add = RepeatSquaring(k / i, n, mod);
    for (Int j = 2; i * j <= k; j++) {
      add = (add - a[i * j] + mod) % mod;
    }
    a[i] = add;
  }
  for (Int i = 1; i <= (Int)k; i++) {
    Int add = a[i] * i % mod;
    res = (res + add) % mod;
  }
  print(res);
  return 0;
}


Error: TLE

Fixed submission:

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

typedef int64_t Int;
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()

const double EPS = 1e-10;
const Int INF = 1e18;
const int inf = 1e9;
const Int mod = 1e9 + 7;

bool print_space_enable = false;
void print() {
  cout << endl;
  print_space_enable = false;
}

template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
  if (print_space_enable)
    cout << " ";
  cout << fixed << setprecision(15) << head;
  print_space_enable = true;
  print(std::forward<Tail>(tail)...);
}

template <typename T> void print(vector<T> v) {
  for (size_t i = 0; i < v.size(); i++) {
    if (i > 0)
      std::cout << " ";
    std::cout << v[i];
  }
  std::cout << std::endl;
}

using ll = long long int;
ll PowMod(ll N, ll P, ll M) {
  ll ans = 1;
  for (ll i = 0; i < P; ++i) {
    ans *= N;
    ans %= M;
  }
  return ans;
}

vector<int64_t> divisor(int64_t n) {
  vector<int64_t> ret;
  for (int64_t i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return (ret);
}

int main() {
  Int n, k;
  cin >> n >> k;
  vector<Int> a(k + 1, 0);
  Int res = 0;
  for (Int i = k; (Int)i >= 1; i--) {
    Int add = (PowMod(k / i, n, mod) - a[i] + mod) % mod;
    a[i] = add;
    vector<Int> dv = divisor(i);
    for (Int j = 0; j < (Int)dv.size() - 1; j++) {
      a[dv[j]] += add;
      a[dv[j]] %= mod;
    }
    res = (res + add * i) % mod;
  }
  print(res);
  return 0;
}