alexjercan/bugnet huggingface dataset:
Problem p02715 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
using ll = long long int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define rep(i, N) for (ll i = 0; i < (N); i++)
#define For(i, a, b) for (ll i = (a); i < (b); i++)
#define all(A) (A).begin(), (A).end()

const ll MOD = 1000000007;

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

int main() {
  ll ans = 0;

  ll N, K;
  cin >> N >> K;

  ll lst[K + 1];
  rep(i, K + 1) lst[i] = 0;

  for (ll k = K; k > 0; --k) {
    ll a = K / k;
    ll tmp = modpow(a, N);
    for (ll j = k; j <= K; j += k) {
      tmp = (MOD + tmp - lst[j]) % MOD;
    }
    ans = (ans + k * tmp) % MOD;
    lst[k] = tmp;
  }
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define rep(i, N) for (ll i = 0; i < (N); i++)
#define For(i, a, b) for (ll i = (a); i < (b); i++)
#define all(A) (A).begin(), (A).end()

const ll MOD = 1e9 + 7;

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

int main() {
  ll N, K;
  cin >> N >> K;

  ll lst[K + 1];
  rep(i, N + 1) lst[i] = 0;

  for (ll i = K; i > 0; i--) {
    ll a;
    a = K / i;
    ll tmp;
    tmp = modpow(a, N);
    for (int j = i; j <= K; j += i) {
      tmp = (MOD + tmp - lst[j]) % MOD;
    }
    lst[i] = tmp;
  }
  ll ans = 0;
  for (int i = 1; i <= K; i++) {
    ans += (i * lst[i]) % MOD;
    ans %= MOD;
  }
  cout << ans << endl;

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

#include <stdio.h>

const long long mod = 1000000007;

long long fpow(long long a, long long p) {
  long long r = 1;
  while (p) {
    if (p & 1)
      r = r * a % mod;
    a = a * a % mod;
    p /= 2;
  }
  return r;
}

int n, k;
long long phi[100100];

int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i < 100100; i++) {
    phi[i] = fpow(i, n);
    int s = 2;
    while (s <= i) {
      int e = i / (i / s) + 1;
      phi[i] = (phi[i] + mod - (e - s) * phi[i / s] % mod) % mod;
      s = e;
    }
  }
  long long ans = 0;
  for (int i = 1; i <= k; i++)
    ans = (ans + i * phi[k / i]) % mod;
  printf("%lld\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>

const long long mod = 1000000007;

long long fpow(int n, int k) {
  long long r = 1;
  while (k) {
    if (k & 1)
      r = r * n % mod;
    n = n * n % mod;
    k /= 2;
  }
}

int n, k;
long long phi[100100];

int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i < 100100; i++)
    phi[i] = fpow(i, n);
  for (int i = 1; i < 100100; i++)
    for (int j = i * 2; j < 100100; j += i)
      phi[j] = (phi[j] + mod - phi[i]) % mod;
  long long ans = 0;
  printf("%lld\\n", ans);
  return 0;
}
Problem p02715 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define pb push_back
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define INF (1 << 10)
#define MOD 1000000007
typedef long long ll;
using namespace std;

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

int main() {
  int n, k;
  ll num[100005];
  ll ans = 0;

  cin >> n >> k;

  for (int i = k; i >= 1; i--) {
    ll t = beki(k / i, n);
    num[i] = t;
  }

  for (int i = k; i >= 1; i--) {
    int t = 2;
    while (i * t <= k) {
      num[i] = (num[i] + MOD - num[i * t]) % MOD;
      t++;
    }
    ans += i * num[i];
    ans %= MOD;
  }

  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define pb push_back
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define INF (1 << 10)
#define MOD 1000000007
typedef long long ll;
using namespace std;

int main() {
  int n, k;
  ll num[100005];
  ll ans = 0;

  cin >> n >> k;

  for (int i = k; i >= 1; i--) {
    ll t = 1;
    for (int j = 0; j < n; j++) {
      t *= (k / i);
      t %= MOD;
    }
    num[i] = t;
  }

  for (int i = k; i >= 1; i--) {
    int t = 2;
    while (i * t <= k) {
      num[i] = (num[i] + MOD - num[i * t]) % MOD;
      t++;
    }
    ans += i * num[i];
    ans %= MOD;
  }

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

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

int64_t beki(int64_t n, int64_t x) {
  vector<int64_t> vec(50);
  int64_t i, s, ans;
  i = 0;
  ans = 1;
  cin >> n >> x;
  while (x > 0) {
    vec.at(i) = x % 2;
    x = x / 2;
    i++;
  }
  s = n;
  for (int64_t j = 0; j < 50; j++) {
    if (vec.at(j) == 1) {
      ans = (ans * s) % 1000000007;
    }
    s = (s * s) % 1000000007;
  }
  return ans;
}

int main() {
  int64_t n, k, a;
  a = 0;
  cin >> n >> k;
  vector<int64_t> ans(k);
  ans.at(k - 1) = 1;
  for (int64_t i = k - 2; i >= 0; i--) {
    ans.at(i) = beki(k / (i + 1), n);
    for (int64_t j = 1; (i + 1) * j <= k; j++) {
      ans.at(i) = ans.at(i) - ans.at((i + 1) * j - 1);
      if (ans.at(i) < 0) {
        ans.at(i) = ans.at(i) + 1000000007;
      }
    }
    ans.at(i) = ans.at(i) + beki(k / (i + 1), n);
  }
  for (int64_t i = 0; i < k; i++) {
    a = a + (i + 1) * ans.at(i);
    while (a >= 1000000007) {
      a = a - 1000000007;
    }
  }
  cout << a << endl;
}

Error: TLE

Fixed submission:

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

int64_t beki(int64_t n, int64_t x) {
  vector<int64_t> vec(50);
  int64_t i, s, ans;
  i = 0;
  ans = 1;
  cin >> n >> x;
  while (x > 0) {
    vec.at(i) = x % 2;
    x = x / 2;
    i++;
  }
  s = n;
  for (int64_t j = 0; j < 50; j++) {
    if (vec.at(j) == 1) {
      ans = (ans * s) % 1000000007;
    }
    s = (s * s) % 1000000007;
  }
  return ans;
}

int main() {
  int64_t n, k, a;
  a = 0;
  cin >> n >> k;
  vector<int64_t> dp(k);
  dp.at(0) = 1;
  for (int64_t i = 1; i < k; i++) {
    dp.at(i) = beki(i + 1, n);
    if (i >= 2) {
      for (int64_t j = 2; j < i + 1; j++) {
        dp.at(i) = dp.at(i) - dp.at(((i + 1) / j) - 1);
        if (dp.at(i) < 0) {
          dp.at(i) += 1000000007;
        }
      }
    }
    if (i == 1) {
      dp.at(i) = beki(2, n);
    }
    dp.at(i)--;
    if (dp.at(i) < 0) {
      dp.at(i) += 1000000007;
    }
  }
  for (int64_t i = 0; i < k; i++) {
    a += dp.at((k / (i + 1)) - 1) * (i + 1);
    while (a >= 1000000007) {
      a = a - 1000000007;
    }
  }
  cout << a << endl;
}
Problem p02715 (C++) — buggy submission, verdict: Runtime Error

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

// a^n mod を計算する
int64_t modpow(int64_t a, int64_t n, int64_t mod) {
  int64_t res = 1;
  while (n > 0) {
    if (n % 2 == 1)
      res = res * a % mod;
    a = a * a % mod;
    n = n / 2;
  }
  return res;
}
int main() {
#define rep(i, n) for (int64_t i = 1; i <= (int64_t)(n); i++)
  int64_t N, K;
  long long p;
  int64_t answer = 0;
  cin >> N >> K;
  vector<int64_t> r(K + 1);
  for (int i = 1; i <= K; i++) {
    p = K / i;
    r[i] = modpow(p, N, 1000000007);
  }
  //[K/i]のN乗のリスト
  // i番目にiが乗ってる
  // rep(i,K)
  // cout<<r[i]<<endl;
  for (int i = K; i >= 1; i--) {
    // if(K>=2){
    for (int j = 2; j <= K; j++) {
      if (j * i > K)
        break;
      r[i] -= r[j * i];
    }
  }
  //}
  for (int i = 1; i <= K; i++) {
    answer += r[i] * (i) % 1000000007;
    answer = answer % 1000000007;
  }
  if (answer < 0)
    cout << answer + 1000000007;
  else
    cout << answer;
}

Error: WA

Fixed submission:

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

// a^n mod を計算する
int64_t modpow(int64_t a, int64_t n, int64_t mod) {
  int64_t res = 1;
  while (n > 0) {
    if (n % 2 == 1)
      res = res * a % mod;
    a = a * a % mod;
    n = n / 2;
  }
  return res;
}
int main() {
#define rep(i, n) for (int i = 1; i <= (int)(n); i++)
  int64_t N, K;
  int64_t answer = 0;
  cin >> N >> K;
  vector<int64_t> r(K), s(K);
  rep(i, K) { r[i] = modpow(K / i, N, 1000000007); }
  //[K/i]のN乗のリスト
  // i番目にiが乗ってる
  // rep(i,K)
  // cout<<r[i]<<endl;
  for (int i = K; i >= 1; i--) {
    // if(K>=2){
    for (int j = 2; j <= K; j++) {
      if (i * j > K)
        break;
      r[i] -= r[j * i];
    }
  }
  //}
  for (int i = 1; i <= K; i++) {
    answer += r[i] * (i) % 1000000007;
    answer = answer % 1000000007;
  }
  if (answer < 0)
    cout << answer + 1000000007;
  else
    cout << answer;
}
Problem p02715 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const ll mo = 1000000007;

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;
}

ll MOD(ll a, ll mod) {
  ll temp = a % mod;
  if (temp < 0)
    temp += mod;
  return temp;
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> dp(K + 1, 0);

  for (ll i = K; i >= 1; i--) {
    ll temp = modpow(K / i, N, mo);
    ll b = 2 * i;
    while (b <= K) {
      temp = MOD(temp - dp[b] % mo, mo);
      b += i;
    }
    dp[i] = temp;
  }

  ll sum = 0;
  for (ll i = 0; i <= K; i++) {
    sum = (sum + dp[i] * i % mo) % mo;
  }

  cout << sum << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const ll mo = 1000000007;

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;
}

ll MOD(ll a, ll mod) {
  ll temp = a % mod;
  if (temp < 0)
    temp += mod;
  return temp;
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> dp(N + 1, 0);

  for (ll i = K; i >= 1; i--) {
    ll temp = modpow(K / i, N, mo);
    ll b = 2 * i;
    while (b <= K) {
      temp = MOD(temp - dp[b] % mo, mo);
      b += i;
    }
    dp[i] = temp;
  }

  ll sum = 0;
  for (ll i = 0; i <= K; i++) {
    sum = (sum + dp[i] * i % mo) % mo;
  }

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

#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define pb push_back
#define mp make_pair
#define clr(v, d) memset(v, d, sizeof(v))
typedef long long ll;
typedef unsigned long long ull;
const double EPS = (1e-9);
using namespace std;
int getBit(int num, int idx) { return ((num >> idx) & 1) == 1; }
int setBit1(int num, int idx) { return num | (1 << idx); }
ll setBit0(ll num, int idx) { return num & ~(1ll << idx); }
ll flipBit(int num, int idx) { return num ^ (1 << idx); }
void GO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
const int N = 100000 + 9, mod = 1000000000 + 7;
ll cnt[N], fres[N];
void go(int n) {

  for (int i = 1; i * i <= n; i++) {

    if (n % i == 0) {
      cnt[i]++;
      if (n / i != i)
        cnt[n / i]++;
    }
  }
}
ll power(ll b, ll p) {

  if (p == 0)
    return 1;
  if ((p & (1ll))) {
    return b % mod * power((b * b) % mod, p / 2) % mod;
  } else {
    return power((b * b) % mod, p / 2);
  }
}
int main() {
  GO();
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= k; i++) {
    go(i);
  }
  ll ans = 0;
  for (int i = k; i >= 1; i--) {
    fres[i] = power(cnt[i], n);
    for (int j = i * 2; j <= k; j += i) {
      fres[i] = ((fres[i] - fres[j]) + mod) % mod;
    }
    ans += (1ll * fres[i] * i) % mod;
    if (ans >= mod)
      ans -= mod;
  }
  cout << ans << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define pb push_back
#define mp make_pair
#define clr(v, d) memset(v, d, sizeof(v))
typedef long long ll;
typedef unsigned long long ull;
const double EPS = (1e-9);
using namespace std;
int getBit(int num, int idx) { return ((num >> idx) & 1) == 1; }
int setBit1(int num, int idx) { return num | (1 << idx); }
ll setBit0(ll num, int idx) { return num & ~(1ll << idx); }
ll flipBit(int num, int idx) { return num ^ (1 << idx); }
void GO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
const int N = 100000 + 9, mod = 1000000000 + 7;
ll cnt[N], fres[N];
int go(int n) {

  for (int i = 1; i * i <= n; i++) {

    if (n % i == 0) {
      cnt[i]++;
      if (n / i != i)
        cnt[n / i]++;
    }
  }
}
ll power(ll b, ll p) {

  if (p == 0)
    return 1;
  if ((p & (1ll))) {
    return b % mod * power((b * b) % mod, p / 2) % mod;
  } else {
    return power((b * b) % mod, p / 2);
  }
}
int main() {
  GO();
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= k; i++) {
    go(i);
  }
  ll ans = 0;
  for (int i = k; i >= 1; i--) {
    fres[i] = power(cnt[i], n);
    for (int j = i * 2; j <= k; j += i) {
      fres[i] = ((fres[i] - fres[j]) + mod) % mod;
    }
    ans += (1ll * fres[i] * i) % mod;
    if (ans >= mod)
      ans -= mod;
  }
  cout << ans << "\\n";
}
Problem p02715 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep1(i, n) for (ll i = 1; i <= (n); i++)
using namespace std;
typedef long long ll;
const static ll MOD = (1e+9) + 7;

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

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> lst(K + 1, 0);
  ll ans = 0;
  for (ll i = K; i > 0; i--) {
    lst[i] = modpow(K / i, N);
    for (ll j = 2 * i; j <= K; j += i) {
      lst[i] = (lst[i] - lst[j] + MOD) % MOD;
    }
  }
  rep1(i, K) { ans += lst[i] % MOD * i % MOD; }
  cout << ans % MOD << endl;
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

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

const ll MAX = (1e+5) + 5;
const ll MOD = (1e+9) + 7;

vector<vector<ll>> lst(MAX, vector<ll>(MAX, -1));

// index i,jに、gcd(i,j)をメモしておく.

ll gcd(ll a, ll b) {
  if (lst[a][b] != -1)
    return lst[a][b];
  ll r = a % b;
  if (r == 0)
    return lst[a][b] = b;
  return lst[a][b] = gcd(b, r);
}

int main() {
  ll N, K;
  cin >> N >> K;
  map<ll, ll> mp;
  set<ll> s;
  rep(i, K) {
    rep(j, K) {
      lst[i + 1][j + 1] = gcd(i + 1, j + 1);
      mp[lst[i + 1][j + 1]]++;
      s.insert(lst[i + 1][j + 1]);
    }
  }
  ll ans = 0;
  rep(i, N) {
    for (ll j = 1; j <= K; j++) {
      for (ll x : s) {
        ans += gcd(j, x) * mp[x] % MOD;
        ans %= MOD;
      }
    }
  }
  cout << ans % MOD << endl;
  return 0;
}
Problem p02715 (C++) — buggy submission, verdict: Runtime Error

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

const ll mod = 1000000007;

ll modpow(ll a, ll n) {
  ll r = 1;
  while (n)
    r = r * ((n % 2) ? a : 1) % mod, a = a * a % mod, n >>= 1;
  return r;
}

int main() {
  ll n, k;
  cin >> n >> k;
  ll sum = 0;
  vector<ll> d(k + 1);
  for (int i = k; i > 0; i--) {
    d[i] = modpow(k / i, n);
    for (int j = i * 2; j <= k; j += i)
      d[i] -= d[j];
    d[i] = (d[i] % mod + mod) % mod;
    sum += i * d[i];
  }
  cout << sum % mod << endl;
}


Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::at: __n (which is 1) >= this->size() (which is 1)


Fixed submission:

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

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> r, b, g;
  for (int i = 0; i < n; i++) {
    if (s.at(i) == 'R')
      r.push_back(i);
    else if (s.at(i) == 'B')
      b.push_back(i);
    else
      g.push_back(i);
  }
  int cnt = 0;
  for (int i = 0; i < r.size(); i++) {
    for (int j = 0; j < b.size(); j++) {
      for (int k = 0; k < g.size(); k++) {
        if ((r[i] + b[j]) == 2 * g[k] || (g[k] + b[j]) == 2 * r[i] ||
            (r[i] + g[k]) == 2 * b[j]) {
          continue;
        } else
          cnt++;
      }
    }
  }
  cout << cnt << endl;
}
Problem p02715 (C++) — buggy submission, verdict: Time Limit Exceeded

#define rep(i, n) for (ll i = 0; i < n; i++)
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using P = pair<int, int>;

ll p = 1e9 + 7;

// a^n mod p
ll f(ll a, ll n) {
  if (n == 0)
    return 1;
  else if (n == 1)
    return a;
  else {
    ll x = f(a, n / 2);
    ll ans = ((x * x) % p) * f(a, n - 2 * (n / 2)) % p;
    return ans;
  }
}

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> cnt(k + 1, 0);
  for (ll i = k; i >= 1; i--) {
    cnt[i] = f(k / i, n);
    for (ll j = 2; i * j <= k; j++)
      cnt[i] = (cnt[i] + p - cnt[j * i]) % p;
    // printf("%lld %lld\\n", i, cnt[i]);
  }
  ll ans = 0;
  rep(i, k + 1) ans = (ans + i * cnt[i]) % p;
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#define rep(i, n) for (ll i = 0; i < n; i++)
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using P = pair<int, int>;

ll p = 1e9 + 7;

ll f(ll k, ll x, ll n) {
  ll ans = 1;
  ll b = k / x;
  rep(i, n) ans = (ans * b) % p;
  return ans;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> cnt(k + 1, 0);
  for (ll i = k; i >= 1; i--) {
    cnt[i] = f(k, i, n);
    for (ll j = 2; i * j <= k; j++)
      cnt[i] = (cnt[i] + p - cnt[j * i]) % p;
    // printf("%lld %lld\\n", i, cnt[i]);
  }
  ll ans = 0;
  rep(i, k + 1) ans = (ans + i * cnt[i]) % p;
  cout << ans << endl;

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

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i <= n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define VSORTR(v) sort(v.rbegin(), v.rend());
#define ALL(v) (v).begin(), (v).end()
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;
const ll mod = 1e9 + 7;

// E
ll n, k;
ll power(ll a, ll b) {
  return b ? power(a * a % mod, b / 2) * (b % 2 ? a : 1) % mod : 1;
}

int main() {

  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  cin >> n >> k;

  vll a(k + 1);
  FORR(i, k, 1) {
    a[i] = power(k / i, n);
    ll j = 2 * i;
    while (j <= k) {
      a[i] -= a[j];
      j += i;
    }
  }

  ll ans = 0;
  FOR(i, 1, k) {
    ans += i * a[i];
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i <= n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define VSORTR(v) sort(v.rbegin(), v.rend());
#define ALL(v) (v).begin(), (v).end()
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;
const ll mod = 1e9 + 7;

// E
ll n, k;
ll power(ll a, ll b) {
  return b ? power(a * a % mod, b / 2) * (b % 2 ? a : 1) % mod : 1;
}

ll so(ll m) {
  if (m == 1)
    return 1;
  ll asn = 0;
  asn += power(m, n);
  FOR(i, 2, m) {

    asn -= so(m / i);
    asn %= mod;
  }
  return asn % mod;
}
int main() {

  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  cin >> n >> k;
  ll ans = 0;
  FOR(i, 1, k) {
    ans += i * so(k / i);
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}
Problem p02715 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long LL;

const LL MOD = 1000000007LL;

LL modpow(LL x, LL n) {
  LL r = 1;
  while (n != 0) {
    if (n & 1)
      r = r * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  return r;
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<LL> count(K + 1);
  LL ret = 0;
  for (int i = K; i >= 1; --i) {
    LL n = modpow(K / i, N);
    for (int j = i * 2; j <= K; j += i) {
      n = (n + MOD - count[j]) % MOD;
    }
    count[i] = n;
    ret = (ret + n * i) % MOD;
  }
  cout << ret << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long LL;

const LL MOD = 1000000007LL;

LL modpow(LL x, LL n) {
  LL r = 1;
  while (n != 0) {
    if (n & 1)
      r = r * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  return r;
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<LL> count(N + 1);
  LL ret = 0;
  for (int i = K; i >= 1; --i) {
    LL n = modpow(K / i, N);
    for (int j = i * 2; j <= K; j += i) {
      n = (n + MOD - count[j]) % MOD;
    }
    count[i] = n;
    ret = (ret + n * i) % MOD;
  }
  cout << ret << endl;
}
Problem p02715 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

static const int64_t MOD = 1000000007;

int64_t modpow(int64_t base, int64_t k, int64_t mod) {
  base %= mod;
  int64_t answer = 1;
  while (k > 0) {
    if (k & 1)
      answer = (answer * base) % mod;
    base = (base * base) % mod;
    k >>= 1;
  }
  return answer;
}

int main() {
  int64_t N, K;
  cin >> N >> K;

  vector<int64_t> counts(K + 1);

  int64_t answer = 0;
  for (int64_t gcd = K; gcd >= 1; gcd--) {
    int64_t multiples = K / gcd;
    int64_t count = modpow(multiples, N, MOD);
    for (int k = 2; gcd * k <= K; k++) {
      count = (count + MOD - counts[gcd * k]) % MOD;
    }
    counts[gcd] = count;
    answer = (answer + gcd * counts[gcd]) % MOD;
  }
  cout << answer << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

static const int64_t MOD = 1000000007;

int64_t modpow(int64_t base, int64_t k, int64_t mod) {
  base %= mod;
  int64_t answer = 1;
  while (k > 0) {
    if (k & 1)
      answer = (answer * base) % mod;
    base = (base * base) % mod;
    k >>= 1;
  }
  return answer;
}

int main() {
  int64_t N, K;
  cin >> N >> K;

  vector<int64_t> counts(K);

  int64_t answer = 0;
  for (int64_t gcd = K; gcd >= 1; gcd--) {
    int64_t multiples = K / gcd;
    int64_t count = modpow(multiples, N, MOD);
    for (int k = 2; gcd * k <= K; k++) {
      count = (count + MOD - counts[gcd * k]) % MOD;
    }
    counts[gcd] = count;
    answer = (answer + gcd * counts[gcd]) % MOD;
  }
  cout << answer << endl;

  return 0;
}