alexjercan/bugnet huggingface dataset:
Problem p02715 (C++) — buggy submission, verdict: Time Limit Exceeded

//
// Created by chuan on 2020-04-11.
//

//
// Created by chuan on 2020-04-11.
//

#include <algorithm>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

using ll = long long;

using namespace std;

const ll MOD = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  ll N, K;
  cin >> N >> K;

  ll total = 0;
  // expand with gcd vals, then count how many possible ways we can construct
  // such array, the rest only has gcd of 1.
  vector<ll> powN(K + 1, 0);
  auto power_ = [](ll a, ll b) {
    ll r = 1;
    while (b) {
      if (b & 1)
        r = (r * a) % MOD;
      a = ((a * a) % MOD);
      b >>= 1;
    }
    return r;
  };
  for (int i = K; i >= 1; --i) {
    auto q = K / i;
    powN[i] = power_(q, N);

    for (int j = 2; j * i <= K; ++j) {
      powN[i] = (powN[i] + MOD - powN[j * i]) % MOD;
    }
  }
  for (int j = 1; j <= K; ++j) {
    total = (total + powN[j] * j) % MOD;
  }
  cout << total << endl;

  return 0;
}


Error: TLE

Fixed submission:

//
// Created by chuan on 2020-04-11.
//

//
// Created by chuan on 2020-04-11.
//

#include <algorithm>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

using ll = long long;

using namespace std;

const ll MOD = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  ll N, K;
  cin >> N >> K;

  ll total = 0;
  // expand with gcd vals, then count how many possible ways we can construct
  // such array, the rest only has gcd of 1.
  vector<ll> gcd_cnts(K + 1);
  vector<ll> powN(K + 1, 0);

  auto power_ = [](const ll a, const ll b) {
    ll p = 1;
    for (int i = 0; i < b; ++i) {
      p = (p * a) % MOD;
    }
    return p;
  };
  for (int i = 1; i <= K; ++i) {
    powN[i] = power_(i, N);
  }

  for (int i = K; i >= 1; --i) {
    auto q = K / i;
    auto cnt = powN[q];
    for (int j = 2; j * i <= K; ++j) {
      cnt = (cnt + MOD - gcd_cnts[j * i]) % MOD;
    }
    gcd_cnts[i] = cnt;
  }
  for (int j = 1; j <= K; ++j) {
    total = (total + gcd_cnts[j] * j) % MOD;
  }
  cout << total << endl;

  return 0;
}
///    3^{N} = 6^{n} = 3^{n}
Problem p02715 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
typedef long long int ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
const int MOD = 1000000007;
const int MAX_N = 500010;

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

int main() {
  ll n, k, ans = 0;
  cin >> n >> k;
  vector<ll> g(k + 2);
  for (ll m = k; m >= 1; m--) {
    ll kata = 1;
    while (m * (kata + 1) <= k)
      kata++;
    g[m] = modpow(kata, n, MOD) % MOD;
    for (ll i = 2; m * i <= k; i++) {
      g[m] -= g[m * i];
      g[m] %= MOD;
    }
  }
  for (int i = 1; i <= k; i++) {
    // cout<<i<<" "<<g[i]<<endl;
    ans += i * g[i];
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
typedef long long int ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
const int MOD = 1000000007;
const int MAX_N = 500010;

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

int main() {
  ll n, k, ans = 0;
  cin >> n >> k;
  vector<ll> g(n + 2);
  for (ll m = k; m >= 1; m--) {
    ll kata = 1;
    while (m * (kata + 1) <= k)
      kata++;
    g[m] = modpow(kata, n, MOD) % MOD;
    for (ll i = 2; m * i <= k; i++) {
      g[m] -= g[m * i];
      g[m] %= MOD;
    }
  }
  for (int i = 1; i <= k; i++) {
    // cout<<i<<" "<<g[i]<<endl;
    ans += i * g[i];
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}
Problem p02715 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;
using i64 = int64_t;
using i32 = int32_t;

const i64 mod = 1e9 + 7;
i64 ipow(i64 a, i64 b) {
  i64 x = a, y = 1;
  for (; b > 0; b >>= 1) {
    if (b & 1)
      (y *= x) %= mod;
    (x *= x) %= mod;
  }
  return y;
}
int main() {
  i64 n, k;
  cin >> n >> k;
  i64 ans = 0;
  vector<i64> tab(k + 1);
  for (i64 i = 1; i <= k; ++i) {
    ans += i * ipow(k / i, n) % mod;
    ans -= tab[i] * ipow(k / i, n) % mod;
    // cerr << tab[i] << endl;
    ans += 2 * mod;
    ans %= mod;
    for (i64 j = 2 * i; j <= k; j += i) {
      tab[j] += i - tab[i];
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;
using i64 = int64_t;
using i32 = int32_t;

const i64 mod = 1e9 + 7;
i64 ipow(i64 a, i64 b) {
  i64 x = a, y = 1;
  for (; b > 0; b >>= 1) {
    if (b & 1)
      (y *= x) %= mod;
    (x *= x) %= mod;
  }
  return y;
}
int main() {
  i64 n, k;
  cin >> n >> k;
  i64 ans = 0;
  vector<i64> tab(n + 1);
  for (i64 i = 1; i <= k; ++i) {
    ans += i * ipow(k / i, n) % mod;
    ans -= tab[i] * ipow(k / i, n) % mod;
    ans += 2 * mod;
    ans %= mod;
    for (i64 j = 2 * i; j <= k; j += i) {
      tab[j] += i - tab[i];
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02715 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(v) v.begin(), v.end()
ll N, K;

map<ll, ll> M;
ll zyou(ll x) {
  if (M.count(x)) {
    return M[x];
  }

  ll i = 1;
  ll k = x;
  ll num = 1000000007;
  while (i < N) {
    x *= k;
    i++;
    x %= num;
  }
  M[k] = x;
  return x;
}
int main() {

  cin >> N >> K;

  vector<ll> vec(K + 1);
  for (ll i = 0; i < K; i++) {
    ll bai = K / (K - i);
    ll a = zyou(bai);

    ll j = 2;
    while (bai - 1 > 0) {
      a -= vec[(K - i) * j];
      bai--;
      j++;
    }
    vec[K - i] = a;
  }
  ll ans = 0;
  for (ll i = 1; i <= K; i++) {
    ans += vec[i] * i;
    if (ans > 1000000007) {
      ans %= 1000000007;
    }
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(v) v.begin(), v.end()
ll N, K;
ll zyou(ll x) {
  ll i = 1;
  ll k = x;
  ll num = 1000000007;
  while (i < N) {
    x *= k;
    i++;
    x %= num;
  }
  return x;
}
int main() {

  cin >> N >> K;
  vector<ll> vec(K + 1);
  for (ll i = 0; i < K; i++) {
    ll bai = K / (K - i);
    ll a = zyou(bai);

    ll j = 2;
    while (bai - 1 > 0) {
      a -= vec[(K - i) * j];
      bai--;
      j++;
    }
    vec[K - i] = a;
  }
  ll ans = 0;
  for (ll i = 1; i <= K; i++) {
    ans += vec[i] * i;
    if (ans > 1000000007) {
      ans %= 1000000007;
    }
  }

  cout << ans << endl;
}
Problem p02715 (C++) — buggy submission, verdict: Time Limit Exceeded

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

constexpr long MOD = 1000000007;

long mpow(long a, long b) {
  long res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}

int main() {
  long N, K;
  cin >> N >> K;
  vector<long> V(K + 1);
  for (long i = 1; i <= K; i++) {
    long tmp = K / i;
    if (tmp == 1) {
      V.at(i) = 1;
      continue;
    }
    V.at(i) = mpow(tmp, N);
  }
  for (long i = K; i >= 1; i--) {
    for (long j = i + i; j <= K; j += i) {
      V.at(i) -= V.at(j);
    }
  }
  long ans = 0;
  for (long i = 1; i <= K; i++) {
    ans += V.at(i) * i % MOD;
    ans %= MOD;
  }
  cout << ans << "\\n";
}

Error: TLE

Fixed submission:

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

constexpr long MOD = 1000000007;

int main() {
  long N, K;
  cin >> N >> K;
  vector<long> V(K + 1);
  for (long i = 1; i <= K; i++) {
    long tmp = K / i;
    if (tmp == 1) {
      V.at(i) = 1;
      continue;
    }
    long tmp2 = 1;
    for (long i = 0; i < N; i++) {
      tmp2 *= tmp;
      tmp2 %= MOD;
    }
    V.at(i) = tmp2;
  }
  for (long i = K; i >= 1; i--) {
    for (long j = i + i; j <= K; j += i) {
      V.at(i) -= V.at(j);
    }
  }
  long ans = 0;
  for (long i = 1; i <= K; i++) {
    ans += V.at(i) * i % MOD;
    ans %= MOD;
  }
  cout << ans << "\\n";
}
Problem p02715 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int inf = 1000000000;
const ll mod = 1e9 + 7;

ll powmod(ll x, ll y) {
  if (y == 0) {
    return 1;
  }
  if (y == 1) {
    return x % mod;
  }
  if (y % 2 == 0) {
    return (powmod(x, y / 2) * powmod(x, y / 2)) % mod;
  } else {
    return (x * ((powmod(x, y / 2) * powmod(x, y / 2) % mod))) % mod;
  }
}

int main() {
  ll n, k;
  cin >> n >> k;
  ll d[k + 1];
  for (ll i = k; i > 0; i--) {
    ll a = k / i;
    d[i] = powmod(a, n);
    for (ll j = 2 * i; j <= k; j += i) {
      d[i] -= d[j];
      if (d[i] < 0) {
        d[i] += ll(1e9 + 7);
      }
    }
  }
  ll ans = 0;
  for (ll i = 1; i <= k; i++) {
    // cout << d[i] << endl;
    ans += d[i] * i;
    ans %= ll(1e9 + 7);
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int inf = 1000000000;

ll powll(ll a, ll b) {
  ll aa = a;
  for (int i = 0; i < b - 1; i++) {
    a *= aa;
    a %= ll(1e9 + 7);
  }
  return a;
}

int main() {
  ll n, k;
  cin >> n >> k;
  ll d[k + 1];
  for (ll i = k; i > 0; i--) {
    ll a = k / i;
    d[i] = powll(a, n);
    for (ll j = 2 * i; j <= k; j += i) {
      d[i] -= d[j];
      if (d[i] < 0) {
        d[i] += ll(1e9 + 7);
      }
    }
  }
  ll ans = 0;
  for (ll i = 1; i <= k; i++) {
    // cout << d[i] << endl;
    ans += d[i] * i;
    ans %= ll(1e9 + 7);
  }
  cout << ans << endl;
}
Problem p02715 (C++) — buggy submission, verdict: Runtime Error

/*|In The Name Of Allah|*/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 2e5 + 5;
const int mod = 1e9 + 7;

ll n;
ll k;
ll ways[N];
ll ans;

int fastP(int b, int e) {
  if (!e)
    return 1;
  if (e & 1)
    return b * 1ll * fastP(b * 1ll * b % mod, e >> 1) % mod;
  return fastP(b * 1ll * b % mod, e >> 1) % mod;
}

int main() {
  scanf("%d%d", &n, &k);
  for (int i = k; i >= 1; i--) {
    ll all = k / i;
    ways[i] = fastP(all, n);
    for (int j = i + i; j <= k; j += i) {
      ways[i] -= ways[j];
    }
    assert(ways >= 0);
    ans += (((1LL * ways[i] * i) % mod));
  }
  cout << ans % mod << endl;
}


Error: 0

Fixed submission:

/*|In The Name Of Allah|*/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 3e3 + 5;
const int mod = 1e9 + 7;

int n;
int k;
ll ways[N];
ll ans;

int fastP(int b, int e) {
  if (!e)
    return 1;
  if (e & 1)
    return b * 1ll * fastP(b * 1ll * b % mod, e >> 1) % mod;
  return fastP(b * 1ll * b % mod, e >> 1) % mod;
}

int main() {
  scanf("%d%d", &n, &k);
  for (int i = k; i >= 1; i--) {
    ll all = k / i;
    ways[i] = fastP(all, n);
    for (int j = i + i; j <= k; j += i) {
      ways[i] -= ways[j];
    }
    // cout << i << " " << ways[i] << endl;
    ans += ways[i] * i;
    ans %= mod;
  }
  printf("%d\\n", ans);
}
Problem p02715 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
const double PI = 3.14159265358979;
const ll INF = pow(10, 18);
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

ll pow_mod(ll a, ll b) {
  if (b == 0) {
    return 1;
  } else if (b % 2 == 1) {
    return (a * pow_mod(a, b - 1)) % MOD;
  } else {
    ll t = pow_mod(a, b / 2);
    return t * t % MOD;
  }
}

vector<ll> memo(200001, MOD + 1);
ll cnt_gcd(ll a, ll k, ll n) {
  if (memo[a] != MOD + 1) {
    return memo[a];
  }
  ll b = pow_mod((k / a), n);
  ll i = 1;
  while (a + a * i <= k) {
    b = (b - cnt_gcd(a + a * i, k, n)) % MOD;
    i++;
  }
  memo[a] = b % MOD;
  return b % MOD;
}

int main() {
  ll n, k;
  cin >> n >> k;
  ll ans = 0;
  for (ll i = k; i >= 1; i--) {
    ans = (ans + i * (cnt_gcd(i, k, n))) % MOD;
  }
  cout << (ans + MOD) % MOD << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
const double PI = 3.14159265358979;
const ll INF = pow(10, 18);
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

ll pow_mod(ll a, ll b) {
  if (b == 0) {
    return 1;
  } else if (b % 2 == 1) {
    return (a * pow_mod(a, b - 1)) % MOD;
  } else {
    ll t = pow_mod(a, b / 2);
    return t * t % MOD;
  }
}

ll cnt_gcd(ll a, ll k, ll n) {
  ll b = pow_mod((k / a), n);
  ll i = 1;
  while (a + a * i <= k) {
    b = (b - cnt_gcd(a + a * i, k, n)) % MOD;
    i++;
  }
  return b % MOD;
}

int main() {
  ll n, k;
  cin >> n >> k;
  ll ans = 0;
  for (ll i = k; i >= 1; i--) {
    ans = (ans + i * (cnt_gcd(i, k, n))) % MOD;
  }
  cout << (ans + MOD) % MOD << endl;
}
Problem p02715 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define endl '\\n'
#define maxn 100010
const int MOD = 1000000007;

int pw(int a, int b, int m = MOD) {
  if (b == 0)
    return 1;
  if (b & 1)
    return a * pw(a, b - 1) % m;
  return pw(a * a % m, b / 2);
}

void solve() {
  int n, k;
  cin >> n >> k;

  vector<int> ans(k + 1);
  int pans = 0;
  for (int i = k; i >= 1; i--) {
    ans[i] = pw(k / i, n);
    for (int j = 2; j * i <= k; j++) {
      ans[i] = (ans[i] - ans[i * j] + MOD) % MOD;
    }
    pans = (pans + ans[i] * i % MOD) % MOD;
  }
  cout << pans << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int t = 1;
  //    cin >> t;

  while (t--)
    solve();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define endl '\\n'
#define maxn 100010
const int MOD = 1000000007;

int pw(int a, int b, int m = MOD) {
  if (b == 0)
    return 1;
  if (b & 1)
    return a * pw(a, b - 1) % m;
  return pw(a * a % m, b / 2);
}

void solve() {
  int n, k;
  cin >> n >> k;

  vector<int> ans(n + 1);
  int pans = 0;
  for (int i = k; i >= 1; i--) {
    ans[i] = pw(k / i, n);
    for (int j = 2; j * i <= k; j++) {
      ans[i] = (ans[i] - ans[i * j] + MOD) % MOD;
    }
    pans = (pans + ans[i] * i % MOD) % MOD;
  }
  cout << pans << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int t = 1;
  //    cin >> t;

  while (t--)
    solve();
  return 0;
}
Problem p02715 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <type_traits>
#include <vector>

#include <list>
using namespace std;

#define REP(i, n) for (long long i = 0; i < (n); ++i)
#define rep(i, a, b) for (long long i = a; i < (b); ++i)
#define YES(j) cout << (j ? "YES" : "NO") << endl;
#define Yes(j) std::cout << (j ? "Yes" : "No") << endl;

typedef long long ll;
const ll MOD = 1'000'000'007;
ll mod_powe(ll x, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}
int main(void) {
  ll n, k;
  ll ans = 0;

  cin >> n >> k;

  map<ll, ll> mp;
  // gcd が 1 ~ k のものを全部合算する
  // gcdがkのもの (k / i) ^ n 個存在する
  for (ll i = k; i >= 1; i--) {
    ll tmp = 1;
    /*
    REP(j, n)
    {
        tmp = (tmp * (k / i)) % MOD;
    }
    */
    tmp = mod_powe(k / i, n, MOD);

    // x
    mp[i] = tmp;
    // - 2x - 3x ...
    for (ll j = i * 2; j <= k; j += i) {
      if (mp.count(j) > 0) {
        mp[i] = (mp[i] + MOD - mp[j]) % MOD;
      }
    }

    ans = (ans + mp[i] * i) % MOD;
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <type_traits>
#include <vector>

#include <list>
using namespace std;

#define REP(i, n) for (long long i = 0; i < (n); ++i)
#define rep(i, a, b) for (long long i = a; i < (b); ++i)
#define YES(j) cout << (j ? "YES" : "NO") << endl;
#define Yes(j) std::cout << (j ? "Yes" : "No") << endl;

typedef long long ll;
const ll MOD = 1000000007;

int main(void) {
  ll n, k;
  ll ans = 0;

  cin >> n >> k;

  map<ll, ll> mp;
  // gcd が 1 ~ k のものを全部合算する
  // gcdがkのもの (k / i) ^ n 個存在する
  for (ll i = k; i >= 1; i--) {
    ll tmp = 1;
    REP(j, n) { tmp = (tmp * (k / i)) % MOD; }
    // 個数tmp 値i
    ll tmpans = (tmp * i) % MOD;

    // x
    mp[i] = tmp;
    // - 2x - 3x ...
    for (ll j = i * 2; j <= k; j += i) {
      if (mp.count(j) > 0) {
        mp[i] = (mp[i] + MOD - mp[j]) % MOD;
      }
    }

    ans = (ans + mp[i] * i) % MOD;
  }

  cout << ans << endl;
}
Problem p02715 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(v) v.begin(), v.end()
#define ll long long
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<long long>;
using vvll = vector<vll>;

constexpr ll mod = 1000000007;

ll pow(ll a, ll b) {
  ll r = 1;
  while (b != 0) {
    if (b % 2 != 0) {
      r = r * a % mod;
    }
    a = a * a % mod;
    b /= 2;
  }
  return r;
}

int main() {
  ll n, k;
  cin >> n >> k;
  ll gcd = k;
  ll count = 0;
  vll d(k + 1);
  while (gcd > 0) {
    ll c = k / gcd;
    d[gcd] = pow(c, n) % mod;
    for (ll i = c; i > 1; i--) {
      d[gcd] -= d[gcd * i];
    }
    count += d[gcd] * gcd % mod;
    gcd--;
  }
  count %= mod;
  if (count < 0)
    count += 1000000007;
  cout << count << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(v) v.begin(), v.end()
#define ll long long
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<long long>;
using vvll = vector<vll>;

const ll mod = 1000000007;

ll modPow(long long a, long long n, long long p) {
  if (n == 1)
    return a % p;
  if (n % 2 == 1)
    return (a * modPow(a, n - 1, p)) % p;
  long long t = modPow(a, n / 2, p);
  return (t * t) % p;
}

int main() {
  ll n, k;
  cin >> n >> k;
  ll gcd = k;
  ll count = 0;
  vll d(k);
  while (gcd > 0) {
    int c = k / gcd;
    d[gcd] = modPow(c, n, mod);
    count += d[gcd] * gcd;
    while (c > 1) {
      count -= d[gcd * c] * gcd;
      c--;
    }
    count %= mod;
    gcd--;
  }
  if (count < 0)
    count += 1000000007;
  cout << count << endl;
}
Problem p02715 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define ll long long
int in() {
  int x;
  cin >> x;
  return x;
}
ll lin() {
  ll x;
  cin >> x;
  return x;
}
string sin() {
  string x;
  cin >> x;
  return x;
}
using graph = vector<vector<int>>;
using graph2 = vector<vector<pair<int, int>>>;

bool ifprime(int x) {
  if (x == 1)
    return false;
  if (x == 2)
    return true;
  if (x % 2 == 0)
    return false;
  for (int i = 2; i * i <= x; i++) {
    if (x % i == 0)
      return false;
  }
  return true;
}
ll mod(ll x, ll y) {
  if (x >= 0 || x % y == 0)
    return x % y;
  return y + x % y;
} // mod including minus
ll dv0(ll x, ll y) {
  if (x >= 0 || x % y == 0)
    return x / y;
  return x / y - 1;
} // rnd down
ll dv1(ll x, ll y) {
  if (x % y == 0)
    return dv0(x, y);
  return dv0(x, y) + 1;
} // rnd up
ll M = 1e9 + 7;
int quick_pow(int x, int p) {
  int an = 1, po = x;
  while (p) {
    if (p & 1)
      an = 1ll * an * po % M;
    po = 1ll * po * po % M;
    p >>= 1;
  }
  return an;
}
// Start from Here
int main() {
  ll N, K;
  cin >> N >> K;
  ll a[K + 1];
  ll r;
  for (ll i = K; i > 0; i--) {
    rep2(j, K / i) {
      if (j == 1) {
        a[i] = quick_pow(K / i, N);
      } else {
        a[i] -= a[j * i];
        a[i] = mod(a[i], M);
      }
    }
  }

  ll sum = 0;
  rep2(i, K) {
    sum += a[i] * i;
    sum %= M;
  }

  cout << sum << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define ll long long
int in() {
  int x;
  cin >> x;
  return x;
}
ll lin() {
  ll x;
  cin >> x;
  return x;
}
string sin() {
  string x;
  cin >> x;
  return x;
}
using graph = vector<vector<int>>;
using graph2 = vector<vector<pair<int, int>>>;

bool ifprime(int x) {
  if (x == 1)
    return false;
  if (x == 2)
    return true;
  if (x % 2 == 0)
    return false;
  for (int i = 2; i * i <= x; i++) {
    if (x % i == 0)
      return false;
  }
  return true;
}
ll mod(ll x, ll y) {
  if (x >= 0 || x % y == 0)
    return x % y;
  return y + x % y;
} // mod including minus
ll dv0(ll x, ll y) {
  if (x >= 0 || x % y == 0)
    return x / y;
  return x / y - 1;
} // rnd down
ll dv1(ll x, ll y) {
  if (x % y == 0)
    return dv0(x, y);
  return dv0(x, y) + 1;
} // rnd up

// Start from Here
int main() {
  ll N, K;
  cin >> N >> K;
  ll M = 1e09 + 7;
  ll a[K + 1];
  ll r;
  for (ll i = K; i > 0; i--) {
    rep2(j, K / i) {
      if (j == 1) {
        r = K / i;
        rep(k, N - 1) {
          r *= K / i;
          r %= M;
        }
        a[i] = r;
      } else {
        a[i] -= a[j * i];
        a[i] = mod(a[i], M);
      }
    }
  }

  ll sum = 0;
  rep2(i, K) {
    sum += a[i] * i;
    sum %= M;
  }

  cout << sum << endl;
}