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

// #include <bits/stdc++.h>
#include <iostream>
int read() {
  int x = 0, f = 1;
  char c;
  do {
    c = getchar();
    if (c == '-')
      f = -1;
  } while (!isdigit(c));
  do {
    x = x * 10 + c - '0';
    c = getchar();
  } while (isdigit(c));
  return x * f;
}
using Int = signed long long int;
const int N = 100000 + 10;
const Int mod = Int(1e9) + 7LL;

int vis[N], phi[N], prime[N], pcnt;
Int qpow(Int a, Int p) {
  Int r = 1;
  while (p) {
    if (p & 1)
      r = r * a % mod;
    a = a * a % mod;
    p >>= 1;
  }
  return r;
}

void sieve() {
  phi[1] = 1;
  for (int i = 2; i < N; i++) {
    if (!vis[i]) {
      prime[pcnt++] = i;
      phi[i] = i - 1;
    }
    for (int j = 0; j < pcnt && 1LL * i * prime[j] < N; j++) {
      int t = i * prime[j];
      vis[t] = 1;
      if (i % prime[j] == 0) {
        phi[t] = phi[i] * prime[j] % mod;
        break;
      }
      phi[t] = phi[i] * (prime[j] - 1) % mod;
    }
  }
}

int main() {
  int n = read(), k = read();
  sieve();

  Int ans = 0;
  for (int i = 1; i <= k; i++)
    ans = (ans + qpow(k / i, n) * phi[i] % mod) % mod;
  std::cout << ans << std::endl;
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize(2)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
const int mod = 1000000007;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
int k, n;
long long ans, ans2;
int t, a[100100];
void dfs(int x) {
  if (x == n) {
    ans2 = a[0];
    for (int i = 1; i < t; ++i) {
      ans2 = (gcd(ans2, a[i])) % mod;
    }
    ans = (ans + ans2) % mod;
    return;
  }
  for (int i = 1; i <= k; ++i) {
    a[t] = i;
    t++;
    dfs(x + 1);
    t--;
  }
}
int main() {
  cin >> n >> k;
  dfs(0);
  cout << ans << endl;
  //    system("pause");
  return 0;
}
Problem p02715 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
ll power(ll a, ll b) {
  return b ? power(a * a % mod, b / 2) * (b % 2 ? a : 1) % mod : 1;
}
int main() {
  ll n, k;
  cin >> n >> k;
  ll dp[k + 1], ans = 0;
  for (ll i = k; i >= 1; i--) {
    dp[i] = power(k / i, n);
    for (ll j = 2 * i; j <= k; j += i)
      dp[i] -= dp[j];
    ans = (ans + i * dp[i] % mod) % mod;
  }
  cout << (ans + mod) % mod;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define elif else if
typedef long long ll;
typedef unsigned long long ull;
#define DEBUG(x) cout << (x) << endl

#define MODULE 1000000007

int n, k;
int arr[200005] = {0};
int ans = 0;
map<int, int> record;
ll cnt[200005] = {0};

void dfs(int pos) {
  if (pos == n) {
    int tmp = arr[0];
    for (int i = 1; i < n; i++) {
      tmp = __gcd(tmp, arr[i]);
    }
    ans += tmp;
    record[tmp] += 1;
    return;
  }
  for (int i = 1; i <= k; i++) {
    arr[pos] = i;
    dfs(pos + 1);
  }
}

void test_db() {
  record.clear();
  ans = 0;
  dfs(0);
}

ll quick_pow(ll a, ll b) {
  ll base = a;
  ll ans = 1;
  while (b) {
    if (b & 1) {
      ans *= base;
      ans %= MODULE;
    }
    base *= base;
    base %= MODULE;
    b >>= 1;
  }
  return ans % MODULE;
}

void solve() {
  for (int i = k; i >= 1; i--) {
    cnt[i] = quick_pow(k / i, n);
    for (int j = 2; j * i <= k; j++) {
      cnt[i] -= cnt[j * i];
    }
    ll tmp = (cnt[i] * i) % MODULE;
    ans += tmp;
    ans %= MODULE;
    while (ans < 0) {
      ans += MODULE;
    }
  }
}

int main() {
  for (int _ = 0;; _++) {
    scanf("%d %d", &n, &k);
    // test_db();
    // printf("%d\\n", ans);
    // for (map<int, int>::iterator it = record.begin(); it !=
    // record.end();it++)
    // {
    //     printf("%d:%d\\n", it->first, it->second);
    // }
    solve();
    printf("%d\\n", ans % MODULE);
  }
  return 0;
}
Problem p02715 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int INF = 0x3f3f3f3f;
const int N = 100000 + 5;
const ll mod = 1e9 + 7;
ll vis[N];
ll mod_pow(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() {
  ll n, k;
  cin >> n >> k;
  ll sum = 0;
  for (ll i = k; i >= 1; i--) {
    vis[i] = mod_pow(k / i, n, mod);
    for (ll j = 2 * i; j <= k; j += i) {
      vis[i] = (vis[i] - vis[j] + mod) % mod;
    }
  }
  for (int i = 1; i <= k; i++) {
    sum = (sum + (i * vis[i]) % mod) % mod;
  }
  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int INF = 0x3f3f3f3f;
const int N = 100000 + 5;
const ll mod = 1e9 + 7;
ll vis[N];
ll mod_pow(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() {
  int n, k;
  cin >> n >> k;
  vis[1] = 1;
  ll sum = 0;
  for (int i = 2; i <= k; i++) {
    vis[i] = mod_pow(i * 1ll, n * 1ll, mod);
    for (int j = 2; j <= i; j++) {
      vis[i] -= vis[i / j];
    }
  }
  for (int i = 1; i <= k; i++) {
    sum += (i * vis[k / i]);
    sum %= mod;
  }
  cout << sum << endl;
  return 0;
}
Problem p02715 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#define ll long long
using namespace std;

const ll mod = 1e9 + 7;

ll ksm(ll b, ll p) {
  ll ans = 1;
  while (p > 0) {
    if (p % 2 == 1)
      ans = (ans * b) % mod;
    b = (b * b) % mod;
    p /= 2;
  }
  return ans;
}

ll f[500010], anss;

int main() {
  int n, k;
  cin >> n >> k;
  for (ll i = k; i >= 1; i--) {
    ll cnt = k / i;
    f[i] = ksm(cnt, n);
    for (ll j = 2 * i; j <= k; j += i) {
      f[i] = (f[i] + mod - f[j]) % mod;
    }
  }
  for (ll i = 1; i <= k; i++) {
    anss += f[i] * i;
    anss %= mod;
  }
  cout << anss;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
const int mod = 1000000007;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
int k, n;
long long ans, ans2;
int t, a[100100];
void dfs(int x) {
  if (x == n) {
    ans2 = a[0];
    for (int i = 1; i < t; ++i) {
      ans2 = (gcd(ans2, a[i])) % mod;
    }
    ans = (ans + ans2) % mod;
    return;
  }
  for (int i = 1; i <= k; ++i) {
    a[t] = i;
    t++;
    dfs(x + 1);
    t--;
  }
}
int main() {
  cin >> n >> k;
  dfs(0);
  cout << ans << endl;
  // system("pause");
  return 0;
}
Problem p02715 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)

typedef int long long ll;
using namespace std;
typedef pair<int, int> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const ll MOD = 1e9 + 7;
static const int MAX = 100;
static const int INF = (1 << 23);
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

ll modpow(ll a, ll b) {
  ll ret = 1;
  while (b > 0) {
    if (b & 1)
      ret = (ret * a) % MOD;
    a = (a * a) % MOD;
    b = b >> 1;
  }
  return ret;
}
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> nk(k + 1, 0);
  ll ans = 0;
  for (ll i = k; i >= 1; i--) {
    ll s = k / i;
    ll gcds = modpow(s, n);
    ll snext = i * 2;
    ll next = 3;

    while (snext <= k) {
      gcds -= nk[snext];
      if (gcds < 0)
        gcds += MOD;
      snext = next * i;
      next++;
    }

    ans += gcds * i;
    ans %= MOD;
    nk[i] = gcds;
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)

typedef int long long ll;
using namespace std;
typedef pair<int, int> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const ll MOD = 1e9 + 7;
static const int MAX = 100;
static const int INF = (1 << 23);
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

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

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> nk(n + 1, 0);
  ll ans = 0;
  for (ll i = k; i >= 1; i--) {
    ll s = k / i;
    ll gcds = modpow(s, n);
    ll snext = i * 2;
    ll next = 3;

    while (snext <= k) {
      gcds -= nk[snext];
      if (gcds < 0)
        gcds += MOD;
      snext = next * i;
      next++;
    }

    ans += gcds * i;
    ans %= MOD;
    nk[i] = gcds;
  }

  cout << ans << endl;

  return 0;
}
Problem p02715 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, x) for (int i = 0, i##_len = (x); i < i##_len; ++i)
#define rep(i, a, b) for (int i = (a), i##_len(b); i < i##_len; ++i)
#define All(x) begin(x), end(x)
#define rAll(x) rbegin(x), rend(x)
using namespace std;
using ll = long long;

constexpr ll mod = 1e9 + 7;
ll mpow(ll a, ll n) {
  ll ans = 1;
  while (n) {
    if (n & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    n /= 2;
  }
  return ans;
}

int main() {
  int N, K;
  cin >> N >> K;
  // gcdがjであるものを上から求める
  // gcdがjの倍数になるのは全てがjの倍数になったとき
  vector<ll> dp(K + 1);
  ll ans = 0;
  for (int i = K; i > 0; --i) {
    dp[i] = mpow(K / i, N);
    for (int j = 2 * i; j <= K; j += i) {
      dp[i] -= dp[j];
      dp[i] = (dp[i] + mod) % mod;
    }
    ans += dp[i] * i;
    ans %= mod;
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, x) for (int i = 0, i##_len = (x); i < i##_len; ++i)
#define rep(i, a, b) for (int i = (a), i##_len(b); i < i##_len; ++i)
#define All(x) begin(x), end(x)
#define rAll(x) rbegin(x), rend(x)
using namespace std;
using ll = long long;

constexpr ll mod = 1e9 + 7;
ll mpow(ll a, ll n) {
  ll ans = 1;
  while (n) {
    if (n & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    n /= 2;
  }
  return ans;
}

int main() {

  int N, K;
  cin >> N >> K;
  // gcdがjであるものを上から求める
  // gcdがjの倍数になるのは全てがjの倍数になったとき
  vector<ll> dp(K);
  ll ans = 0;
  for (int i = K; i > 0; --i) {
    dp[i] = mpow(K / i, N);
    for (int j = 2 * i; j <= K; j += i) {
      dp[i] -= dp[j];
      dp[i] = (dp[i] + mod) % mod;
    }
    ans += dp[i] * i;
    ans %= mod;
  }
  cout << ans << 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 < (n); ++i)
#define drep(i, n) for (int i = (n - 1); i >= 0; i--)
#define all(v) (v).begin(), (v).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
const ll MOD = 1e9 + 7;

ll mod_pow(ll n, ll p, ll mod = MOD) {
  if (p == 0)
    return 1;
  ll res = mod_pow(n * n % mod, p / 2, mod);
  if (p % 2 == 1)
    res = res * n % mod;
  return res;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<ll> d(k + 1);
  for (int i = 1; i <= k; i++) {
    d[i] = mod_pow(k / i, n, MOD);
  }
  for (int i = k; i > 0; i--) {
    for (int j = 2 * i; j <= k; j += i) {
      d[i] -= d[j];
      d[i] %= MOD;
    }
  }
  ll ans = 0;
  for (int i = 1; i <= k; i++) {
    ans += d[i] * i;
    ans %= MOD;
  }
  cout << ans << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n - 1); i >= 0; i--)
#define all(v) (v).begin(), (v).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
const ll MOD = 1e9 + 7;

int n, k;
ll ans = 0;

void dfs(int num, ll x) {
  x %= MOD;
  if (num == n) {
    ans += x;
    ans %= MOD;
    return;
  }
  for (ll i = 1; i <= k; i++) {
    dfs(num + 1, gcd(x, i));
  }
  return;
}

int main() {
  cin >> n >> k;
  for (ll i = 1; i <= k; i++) {
    dfs(1, i);
  }
  cout << ans << "\\n";
  return 0;
}
Problem p02715 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
         ██╗ ██████╗ ██╗███╗   ██╗    ██╗   ██╗███████╗
         ██║██╔═══██╗██║████╗  ██║    ██║   ██║██╔════╝
         ██║██║   ██║██║██╔██╗ ██║    ██║   ██║███████╗
    ██   ██║██║   ██║██║██║╚██╗██║    ██║   ██║╚════██║
    ╚█████╔╝╚██████╔╝██║██║ ╚████║    ╚██████╔╝███████║
     ╚════╝  ╚═════╝ ╚═╝╚═╝  ╚═══╝     ╚═════╝ ╚══════╝
*/
// password:+BdCHpO8dQvpo5fnV3/u
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn = 5e5 + 5;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
ll v[maxn], phi[maxn], prime[maxn];
ll m = 0;
inline void euler(int n = 400000) {
  memset(v, 0, sizeof(v));
  m = 0;
  for (int i = 2; i <= n; i++) {
    if (!v[i])
      v[i] = i, prime[++m] = i, phi[i] = i - 1;
    for (int j = 1; j <= m; j++) {
      if (prime[j] > v[i] || prime[j] > n / i)
        break;
      v[i * prime[j]] = prime[j];
      phi[i * prime[j]] = phi[i] * (i % prime[j] ? prime[j] - 1 : prime[j]);
    }
  }
}
ll n, k;
inline ll mpow(ll a, ll b) {
  ll ans = 1 % mod;
  while (b) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
int main() {
  phi[1] = 1;
  euler();
  scanf("%lld %lld", &n, &k);
  ll ans = 0, res = 0;
  for (int i = 1; i <= k; i++) {
    res = mpow(k / i, n);
    // cout<<res<<" "<<phi[i]<<"\\n";
    res = (res * phi[i]) % mod;
    ans = (ans + res) % mod;
  }
  printf("%lld", ans);
  return 0;
}

Error: TLE

Fixed submission:

/*
         ██╗ ██████╗ ██╗███╗   ██╗    ██╗   ██╗███████╗
         ██║██╔═══██╗██║████╗  ██║    ██║   ██║██╔════╝
         ██║██║   ██║██║██╔██╗ ██║    ██║   ██║███████╗
    ██   ██║██║   ██║██║██║╚██╗██║    ██║   ██║╚════██║
    ╚█████╔╝╚██████╔╝██║██║ ╚████║    ╚██████╔╝███████║
     ╚════╝  ╚═════╝ ╚═╝╚═╝  ╚═══╝     ╚═════╝ ╚══════╝
*/
// password:+BdCHpO8dQvpo5fnV3/u
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn = 5e5 + 5;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
int n, k;
ll ans = 0;
vector<ll> qaq;
void dfs(int x) {
  if (x == n + 1) {
    ll gcd = qaq[0];
    for (auto it : qaq)
      gcd = __gcd(it, gcd);
    ans = (ans + gcd) % mod;
    return;
  }
  for (int i = 1; i <= k; i++) {
    qaq.push_back(i);
    dfs(x + 1);
    qaq.pop_back();
  }
}
int main() {
  cin >> n >> k;
  dfs(1);
  cout << ans;
  return 0;
}
Problem p02715 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <iterator>
#include <math.h>
#include <numeric>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define endl ("\\n")
#define pi (3.141592653589)
#define mod 1000000007
#define int long long
#define float double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(c) c.begin(), c.end()
#define min3(a, b, c) min(c, min(a, b))
#define min4(a, b, c, d) min(d, min(c, min(a, b)))
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rep(i, n) for (int i = 0; i < n; i++)
#define fast                                                                   \\
  ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)

struct debugger {
  template <typename T> debugger &operator,(const T &v) {
    cerr << v << " ";
    return *this;
  }
} dbg;

int dp[100005];

double power(int a, int b) {
  if (b == 0)
    return 1LL;
  if (b == 1)
    return a * 1LL;
  if (b % 2 == 0) {
    int x = power(a, b / 2);
    x = (x * x) % mod;
    return x;
  } else {
    int x = power(a, (b - 1) / 2);
    x = (x * x) % mod;
    return (x * a) % mod;
  }
}

int32_t main() {
  fast;

  int n, k;
  cin >> n >> k;
  for (int i = k; i >= 1; i--) {
    dp[i] = power(k / i, n);
    for (int j = 2 * i; j <= k; j += i) {
      dp[i] -= dp[j];
    }
  }
  int res = 0;
  for (int i = 1; i <= k; i++) {
    res = (res + (dp[i] * i) % mod) % mod;
  }
  cout << res % mod;

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <iterator>
#include <math.h>
#include <numeric>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define endl ("\\n")
#define pi (3.141592653589)
#define mod 1000000007
#define int long long
#define float double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(c) c.begin(), c.end()
#define min3(a, b, c) min(c, min(a, b))
#define min4(a, b, c, d) min(d, min(c, min(a, b)))
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rep(i, n) for (int i = 0; i < n; i++)
#define fast                                                                   \\
  ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)

struct debugger {
  template <typename T> debugger &operator,(const T &v) {
    cerr << v << " ";
    return *this;
  }
} dbg;

int dp[INT16_MAX];

double power(int a, int b) { return pow(a, b); }

int32_t main() {
  fast;

  int n, k;
  cin >> n >> k;
  for (int i = k; i >= 1; i--) {
    dp[i] = power(k / i, n);
    for (int j = 2 * i; j <= k; j += i) {
      dp[i] -= dp[j];
    }
  }
  int res = 0;
  for (int i = 1; i <= k; i++) {
    res = (res + (dp[i] * i) % mod) % mod;
  }
  cout << res % mod;
}
Problem p02715 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
const int N = 1e5 + 5, MOD = 1e9 + 7;
int dp[N];

int mul(int a, int b) { return 1ll * a * b % MOD; }
int sum(int a, int b) { return (a + b) % MOD; }
int Pow(int a, int b) {
  int res = 1;
  for (; b; b >>= 1, a = mul(a, a))
    if (b & 1)
      res = mul(res, a);
  return res;
}
void calc(int n) {
  for (int i = 2; i <= n;) {
    int j = n / (n / i) + 1;
    dp[n] = sum(dp[n], MOD - mul(dp[n / i], j - i));
    i = j;
  }
  return;
}
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= k; i++) {
    dp[i] = Pow(i, n);
    calc(i);
  }
  int ans = 0;
  for (int i = 1; i <= k; i++)
    ans = sum(ans, mul(i, dp[k / i]));
  cout << ans << "\\n";

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
const int N = 1e5 + 5, MOD = 1e9 + 7;
int dp[N];

int mul(int a, int b) { return 1ll * a * b % MOD; }
int sum(int a, int b) { return (a + b) % MOD; }
int Pow(int a, int b) {
  int res = 1;
  for (; b; b >>= 1, a = mul(a, a))
    if (b & 1)
      res = mul(res, a);
  return res;
}
int calc(int n) {
  int res = 0;
  for (int i = 2; i <= n;) {
    int j = n / (n / i) + 1;
    dp[n] = sum(dp[n], MOD - mul(dp[n / i], j - i));
    i = j;
  }
}
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= k; i++) {
    dp[i] = Pow(i, n);
    calc(i);
  }
  int ans = 0;
  for (int i = 1; i <= k; i++)
    ans = sum(ans, mul(i, dp[k / i]));
  cout << ans << "\\n";

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

#include <bits/stdc++.h>

#define mod 1000000007

using namespace std;
using ll = long long;

ll mypow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % mod;
    }
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

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

  ll res = 0;
  ll cnt[100001] = {};
  for (ll i = K; i > 0; --i) {
    cnt[i] += mypow((K / i), N);
    for (ll j = 2; j <= K; ++j) {
      if (i * j <= K) {
        cnt[i] -= cnt[i * j];
      } else {
        break;
      }
    }
    res += (cnt[i] * i) % mod;
  }
  res = res % mod;
  cout << res;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define mod 1000000007

using namespace std;
using ll = long long;

ll mypow(ll a, ll b) {
  ll res = a;
  for (int i = 1; i < b; ++i) {
    res = (res * a) % mod;
  }
  return res;
}

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

  ll res = 0;
  ll cnt[100001] = {};
  for (ll i = K; i > 0; --i) {
    cnt[i] += mypow((K / i), N);
    for (ll j = 2; j <= K; ++j) {
      if (i * j <= K) {
        cnt[i] -= cnt[i * j];
      } else {
        break;
      }
    }
    res += (cnt[i] * i) % mod;
  }
  res = res % mod;
  cout << res;
  return 0;
}
Problem p02715 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int64_t P, K, Z = 0, Q = 1000000007;
  cin >> P >> K;
  vector<int64_t> p(K + 1);
  for (int64_t j = K; j >= 1; j--) {
    int64_t i = K / j;
    int64_t N = P; // ここから
    int64_t a = 1, b = 0, c = 1;
    while (N > 0) {
      a *= 2;
      if (N % a != 0) {
        N -= a / 2;
        int64_t d = i;
        for (int j = 0; j < b; j++) {
          d = d * d;
          d = d % Q;
        }
        c = c * d;
        c = c % Q;
      }
      b++;
    } // ここまで

    for (int64_t k = 2 * j; k <= K; k += j) {
      c = (c - p.at(k)) % Q;
    }
    p.at(j) = c;
    Z = (Z + c * j) % Q;
  }
  if (Z >= 0) {
    cout << Z << endl;
  } else {
    cout << Q + Z << endl;
  }
}


Error: 0

Fixed submission:

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

int main() {
  int64_t P, K, Z = 0, Q = 1000000007;
  cin >> P >> K;
  vector<int64_t> p(P + 1);
  for (int64_t j = K; j >= 1; j--) {
    int64_t i = K / j;
    int64_t N = P; // ここから
    int64_t a = 1, b = 0, c = 1;
    while (N > 0) {
      a *= 2;
      if (N % a != 0) {
        N -= a / 2;
        int64_t d = i;
        for (int j = 0; j < b; j++) {
          d = d * d;
          d = d % Q;
        }
        c = c * d;
        c = c % Q;
      }
      b++;
    } // ここまで

    for (int64_t k = 2 * j; k <= P; k += j) {
      c = (c - p.at(k)) % Q;
    }
    p.at(j) = c;
    Z = (Z + c * j) % Q;
  }
  if (Z >= 0) {
    cout << Z << endl;
  } else {
    cout << Q + Z << endl;
  }
}