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

#include <algorithm>
#include <bitset>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdint.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using ldouble = long double;

#define repi(i, a, b) for (ll i = (a); i < (b); i++)
#define rep(i, n) for (ll i = 0; i < (n); ++i)

const int MAX = 510000;
const long long MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

// 素因数分解
vector<pair<long long, int>> factorize(long long n) {
  vector<pair<long long, int>> res;
  for (long long i = 2; i * i <= n; ++i) {
    if (n % i != 0)
      continue;
    res.emplace_back(i, 0);
    while (n % i == 0) {
      n /= i;
      res.back().second++;
    }
  }
  if (n != 1)
    res.emplace_back(n, 1);
  return res;
}
vector<int> c;
void f(const vector<pair<ll, int>> &v, int ind, int x) {
  if (ind == v.size()) {
    c[x]++;
    return;
  }
  int b = 1;
  rep(i, v[ind].second + 1) {
    f(v, ind + 1, x * b);
    b *= v[ind].first;
  }
}

// 正剰余
long long mod(long long a, long long b) {
  return a - (long long)std::floor((double)a / (double)b) * b;
}
int main() {
  COMinit();
  int N, K;
  cin >> N >> K;
  c.resize(K + 1);
  rep(i, K + 1) c[i] = 0;
  repi(i, 1, K + 1) { f(factorize(i), 0, 1); }
  vector<ll> sum(K + 1);
  rep(i, K + 1) sum[i] = 0;
  for (int i = K; i >= 1; --i) {
    sum[i] = 1;
    ll b = c[i];
    for (int j = 0; (N >> j) > 0; j++) {
      if (((N >> j) & 1) == 1)
        sum[i] = (sum[i] * b) % MOD;
      b = (b * b) % MOD;
    }
    for (int j = 2; i * j <= K; ++j)
      sum[i] = mod(sum[i] - sum[i * j], MOD);
  }
  ll res = 0;
  rep(i, K + 1) {
    res += sum[i] * i;
    res %= MOD;
  }
  cout << res << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdint.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using ldouble = long double;

#define repi(i, a, b) for (ll i = (a); i < (b); i++)
#define rep(i, n) for (ll i = 0; i < (n); ++i)

const int MAX = 510000;
const long long MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

// 素因数分解
vector<pair<long long, int>> factorize(long long n) {
  vector<pair<long long, int>> res;
  for (long long i = 2; i * i <= n; ++i) {
    if (n % i != 0)
      continue;
    res.emplace_back(i, 0);
    while (n % i == 0) {
      n /= i;
      res.back().second++;
    }
  }
  if (n != 1)
    res.emplace_back(n, 1);
  return res;
}
vector<int> c;
void f(const vector<pair<ll, int>> &v, int ind, int x) {
  if (ind == v.size()) {
    c[x]++;
    return;
  }
  int b = 1;
  rep(i, v[ind].second + 1) {
    f(v, ind + 1, x * b);
    b *= v[ind].first;
  }
}

// 正剰余
long long mod(long long a, long long b) {
  return a - (long long)std::floor((double)a / (double)b) * b;
}
int main() {
  COMinit();
  int N, K;
  cin >> N >> K;
  c.resize(K + 1);
  rep(i, K + 1) c[i] = 0;
  repi(i, 1, K + 1) { f(factorize(i), 0, 1); }
  vector<ll> sum(K + 1);
  rep(i, K + 1) sum[i] = 0;
  for (int i = K; i >= 1; --i) {
    sum[i] = 1;
    rep(j, N) sum[i] = mod(sum[i] * c[i], MOD);
    for (int j = 2; i * j <= K; ++j)
      sum[i] = mod(sum[i] - sum[i * j], MOD);
  }
  ll res = 0;
  rep(i, K + 1) {
    res += sum[i] * i;
    res %= MOD;
  }
  cout << res << endl;
  return 0;
}