Problem p02715 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

const ll mod = 1000000007;
const int N = 200010;
ll fact[N], invf[N];

ll add(ll x, ll y) { return (x + y) % mod; }

ll mul(ll x, ll y) { return (x % mod) * (y % mod) % mod; }

ll powll(ll x, ll y) {
  x %= mod;
  ll res = 1LL;
  while (y) {
    if (y & 1LL)
      res *= x;
    res %= mod;
    x = (x * x) % mod;
    y >>= 1LL;
  }
  return res;
}

ll divll(ll x, ll y) {
  x %= mod;
  return (x * powll(y, mod - 2)) % mod;
}

ll nPr(ll n, ll r) {
  if (n < r || r < 0)
    return 0;
  return mul(fact[n], invf[n - r]);
}

ll nCr(ll n, ll r) {
  if (n < r || r < 0)
    return 0;
  return mul(mul(fact[n], invf[r]), invf[n - r]);
}

void init_f() {
  fact[0] = 1;
  FOR(i, 1, N - 1) { fact[i] = (fact[i - 1] * i) % mod; }
  invf[N - 1] = divll(1, fact[N - 1]);
  FORR(i, N - 1, 1) { invf[i - 1] = (invf[i] * i) % mod; }
}

int main(void) {
  ll n, k;
  cin >> n >> k;
  VL cnt(n + 1);
  FORR(d, k, 1) {
    ll c = k / d;
    ll all = powll(c, n);
    for (ll e = 2 * d; e <= k; e += d) {
      all = (all + mod - cnt[e]) % mod;
    }
    cnt[d] = all;
  }
  ll ans = 0;
  FOR(d, 1, k) ans = (ans + cnt[d] * d) % mod;
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

const ll mod = 1000000007;
const int N = 200010;
ll fact[N], invf[N];

ll add(ll x, ll y) { return (x + y) % mod; }

ll mul(ll x, ll y) { return (x % mod) * (y % mod) % mod; }

ll powll(ll x, ll y) {
  x %= mod;
  ll res = 1LL;
  while (y) {
    if (y & 1LL)
      res *= x;
    res %= mod;
    x = (x * x) % mod;
    y >>= 1LL;
  }
  return res;
}

ll divll(ll x, ll y) {
  x %= mod;
  return (x * powll(y, mod - 2)) % mod;
}

ll nPr(ll n, ll r) {
  if (n < r || r < 0)
    return 0;
  return mul(fact[n], invf[n - r]);
}

ll nCr(ll n, ll r) {
  if (n < r || r < 0)
    return 0;
  return mul(mul(fact[n], invf[r]), invf[n - r]);
}

void init_f() {
  fact[0] = 1;
  FOR(i, 1, N - 1) { fact[i] = (fact[i - 1] * i) % mod; }
  invf[N - 1] = divll(1, fact[N - 1]);
  FORR(i, N - 1, 1) { invf[i - 1] = (invf[i] * i) % mod; }
}

int main(void) {
  ll n, k;
  cin >> n >> k;
  VL cnt(k + 1);
  FORR(d, k, 1) {
    ll c = k / d;
    ll all = powll(c, n);
    for (ll e = 2 * d; e <= k; e += d) {
      all = (all + mod - cnt[e]) % mod;
    }
    cnt[d] = all;
  }
  ll ans = 0;
  FOR(d, 1, k) ans = (ans + cnt[d] * d) % mod;
  cout << ans << endl;
  return 0;
}