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

#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
const int mod = 1e9 + 7;
int main() {
  int64 n, k;
  cin >> n >> k;
  int64 ans = 0;
  for (int64 x = k; x <= n + 1; x++) {
    int64 l = x * (x - 1) / 2;
    int64 r = x * (2 * (n) + (x - 1) * (-1)) / 2;
    ans = (ans + r - l + 1) % mod;
  }
  cout << ans << endl;
  return 0;
}


Error: 249

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
const int mod = 1e9 + 7;
int main() {
  int64 n, k;
  cin >> n >> k;
  int64 ans = 0;
  for (int64 x = k; x <= n + 1; x++) {
    int64 l = x * (x - 1) / 2;
    l %= mod;
    int64 r = x * (2 * (n) + (x - 1) * (-1)) % 2;
    r %= mod;
    ans = (ans + r - l + 1) % mod;
  }
  return ans;
}
Problem p02708 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll n, k;
  cin >> n >> k;

  ll max = 0;
  ll min = 0;
  ll maxc = 0;
  ll minc = 0;

  for (ll i = n; i >= n - k + 1; i--) {
    max += i;
    maxc = i;
  }

  for (ll i = 0; i <= k - 1; i++) {
    min += i;
    minc = i;
  }

  ll ans = 0;

  for (ll i = 1; i <= n - k + 2; i++) {
    ans += max - min + 1;

    maxc--;
    minc++;

    max += maxc;
    min += minc;
  }

  cout << ans % (1000000000 + 7) << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  ll n, k;
  cin >> n >> k;

  ll max = 0;
  ll min = 0;
  ll maxc = 0;
  ll minc = 0;

  for (ll i = n; i >= n - k + 1; i++) {
    max += i;
    maxc = i;
  }

  for (ll i = 0; i <= k - 1; i++) {
    min += i;
    minc = i;
  }

  ll ans = 0;

  for (ll i = 1; i <= n - k + 2; i++) {
    ans += max - min + 1;

    maxc--;
    minc++;

    max += maxc;
    min = minc;
  }

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

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

signed main() {
  ll n, k;
  cin >> n >> k;
  ll max = 0, min = 0, ans = 0;
  for (ll i = k; i <= n + 1; i++) {
    min = (i * (i - 1)) / 2;
    max = ((n + 1) * n) / 2;
    max -= ((n - i + 1) * (n - i)) / 2;
    ans += (max - min + 1);
  }
  ans %= MOD;
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

signed main() {
  ll n, k;
  cin >> n >> k;
  ll max = 0, min = 0, ans = 0, tmp;
  for (int i = k; i <= n + 1; i++) {
    rep(j, i) min += j;
    rep(j, n - i + 1) tmp += j;
    rep(j, n + 1) max += j;
    max -= tmp;
    //      	cout<<min<<":"<<max<<endl;
    ans += (max - min + 1);
    max = 0;
    min = 0;
    tmp = 0;
  }
  ans %= MOD;
  cout << ans << endl;
  return 0;
}
Problem p02708 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#include <cstring>

#define pi 2 * acos(0.00)
#define ll long long
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define nl '\\n'
#define null '\\0'
#define mod 1000000007

using namespace std;
int main() {
  fastio;
  ll n, k, x, ans = 0;
  cin >> n >> k;
  for (x = k; x <= n + 1; x++) {
    ans += x * (n + 1 - x) + 1;
    ans %= mod;
  }
  cout << ans << nl;
  return 0;
}


Error: 0

Fixed submission:

#include <stdio.h>
#define m 1000000007
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  unsigned long long a[200001] = {}, i, j, s = 0;
  for (i = 1; i <= n + 1; i++) {
    a[i] = 1 + i * (n + 1 - i);
    a[i] = a[i] % m;
  }
  for (j = k; j <= n + 1; j++) {
    s += a[j];
  }
  printf("%llu\\n", s % m);

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007

int main() {

  ll n, k;
  cin >> n >> k;

  ll total = 0;
  for (ll i = k; i < n + 1; i++) {
    ll max = (n + n - i + 1) * i / 2;
    ll min = (i - 1) * i / 2;
    ll val = max - min + 1;

    total += val;
    total %= MOD;
  }

  if (total < 0)
    total += MOD;

  total += 1;
  total %= MOD;
  cout << total << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int myplus(int s) { return s - 1 + ((s == 1) ? 0 : myplus(s - 1)); }
int myminus(int s, int k) { return s + ((k == 1) ? 0 : myminus(s - 1, k - 1)); }
int func(int N, int K) {
  return myminus(N, K) - myplus(K) + 1 + ((K - N - 1) ? func(N, K++) : 1);
}
int main() {
  int N, K;
  cin >> N >> K;
  long As = ((K - N - 1) ? func(N, K) : 1);
  cout << As;
}
Problem p02708 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define MOD 1000000007

int main() {
  int n, k;

  scanf("%d", &n);
  scanf("%d", &k);

  int_fast64_t s = 0;

  for (int i = k; i <= n + 1; i++) {
    // (2 * n - i + 1) * i / 2 - ((i - 1) * i / 2)
    s += (int_fast64_t)(n - i + 1) * i + 1;
  }

  printf("%d\\n", s % MOD);

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define MOD 1000000007

int main() {
  int_fast64_t n, k;

  scanf("%d", &n);
  scanf("%d", &k);

  int_fast64_t s = 0;

  for (int i = k; i <= n + 1; i++) {
    // (2 * n - i + 1) * i / 2 - ((i - 1) * i / 2)
    s += (n - i + 1) * i + 1;
    s %= MOD;
  }

  printf("%d\\n", s);

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

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define ll long long

using namespace std;
long long int N, K, L, R, MOD = 1e9 + 7;

int main() {

  cin >> N >> K;
  long long int ans = 0;
  for (long long int i = K; i <= N + 1; i++) {
    L = i * (i - 1) / 2;
    R = N * i - L;
    ans += R - L + 1;
  }
  cout << ans % MOD << endl;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define ll long long

using namespace std;
int n, k;
int a[200005];
unsigned long long res, sum, tmp, fx, fxk, fk, base;

unsigned long long fac(int n) {
  unsigned long long factorial = 1;
  for (int i = 1; i <= n; ++i) {
    factorial *= i;
  }
  return factorial;
}
int main() {

  cin >> n >> k;
  if (k == n + 1) {
    cout << 1;
    exit(0);
  }
  res = 0, sum = 0;
  int x = n + 1;
  fx = fac(x);
  fxk = fac(x - k);
  fk = fac(k);
  res += (fx / (fxk * fk)) - ((n - k + 1) / 2);
  base = 1e9 + 7;
  FOR(i, k + 1, n + 1) {
    int m = i - 2;
    fxk = fxk / (n + 1 - i + 1);
    fk = fk * i;
    res += (fx / (fxk * fk)) - ((n - m - 1) / 2);
  }
  cout << res % base;
}
Problem p02708 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main(void) {
  ll n, k;
  cin >> n >> k;
  ll sum1[n + 1], sum2[n + 1];
  for (int i = 0; i < n + 1; i++) {
    sum1[i] = 0;
    sum2[i] = 0;
  }
  sum1[0] = 0;
  sum2[n] = n;
  for (int i = 1; i < n + 1; i++) {
    sum1[i] = sum1[i - 1] + i;
    sum2[n - i] = sum2[n - i + 1] + n - i;
  }
  ll sum = 1;
  for (int i = k; i < n + 1; i++) {
    sum += sum2[n - i + 1] - sum1[i - 1] + 1;
    sum %= 1000000007;
  }
  cout << sum % 1000000007 << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main(void) {
  ll n, k;
  cin >> n >> k;
  ll ans = 0;
  for (ll i = k; i <= n + 1; i++) {
    ll sum = 0, sum2 = 0;
    for (ll j = 0; j < i; j++) {
      sum += j;
      sum2 += n - j;
    }
    ans += (sum2 - sum + 1) % 1000000007;
  }
  cout << ans % 1000000007 << endl;
  return 0;
}
Problem p02708 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ALL(a) (a).begin(), (a).end()
#define SORT(c) sort(ALL((c)))
#define debug(x)                                                               \\
  do {                                                                         \\
    std::cout << #x << ": ";                                                   \\
    view(x);                                                                   \\
  } while (0)

template <typename T> void view(T e) { std::cout << e << std::endl; }
template <typename T> void view(const std::vector<T> &v) {
  for (const auto &e : v) {
    std::cout << e << ", ";
  }
  std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}

int main() {
  ll N, K;
  cin >> N >> K;
  ll ans = 0;
  for (ll i = K; i <= (N + 1); i++) {
    ll mi = i * (i - 1) / 2;
    ll ma = 0;
    rep(j, i) { ma += N - j; }
    ma = i * (2 * (N - i + 1) + (i - 1)) / 2;
    ans += (ma - mi + 1) % (1000000000 + 7);
  }
  cout << ans % (1000000000 + 7) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ALL(a) (a).begin(), (a).end()
#define SORT(c) sort(ALL((c)))
#define debug(x)                                                               \\
  do {                                                                         \\
    std::cout << #x << ": ";                                                   \\
    view(x);                                                                   \\
  } while (0)

template <typename T> void view(T e) { std::cout << e << std::endl; }
template <typename T> void view(const std::vector<T> &v) {
  for (const auto &e : v) {
    std::cout << e << ", ";
  }
  std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}

int main() {
  ll N, K;
  cin >> N >> K;
  ll ans = 0;
  for (ll i = K; i <= (N + 1); i++) {
    ll mi = i * (i - 1) / 2;
    ll ma = 0;
    rep(j, i) { ma += N - j; }
    ans += (ma - mi + 1) % (1000000000 + 7);
  }
  cout << ans % (1000000000 + 7) << endl;
  return 0;
}
Problem p02708 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;
  long long ans = 0;

  long long max = 0;
  for (long long i = n; i > n - k + 1; i--) {
    max += i;
  }
  long long min = 0;
  for (long long i = 0; i < k - 1; i++) {
    min += i;
  }
  long long ko = max - min + 1;

  for (long long s = k; k < n + 1; k++) {
    ko += n + 2 - 2 * k;
    ans += ko;
  }

  long long ss = (10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 + 7);
  cout << ans % ss + 1 << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int main() {
  long long n, k;
  cin >> n >> k;
  long long ans = 0;
  for (long long s = k; k < n + 1; k++) {

    long long max = 0;
    for (long long i = n; i > n - k; i--) {
      max += i;
    }
    long long min = 0;
    for (long long i = 0; i < k; i++) {
      min += i;
    }
    long long ko = max - min + 1;
    ans += ko;
  }
  long long ss = (10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 + 7);
  cout << ans % ss + 1 << endl;

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

#include <algorithm>
#include <bits/stdc++.h>
#include <cassert>
#include <stdio.h>

using namespace std;

unsigned long long a(unsigned long long N, unsigned long long K) {
  return (N - K + 1) * K + 1;
}

unsigned long long solve(unsigned long long N, unsigned long long K) {
  unsigned long long ans = 0;
  for (int i = K; i <= N + 1; i++) {
    unsigned long long temp = a(N, i) % 1000000007;
    ans += temp;
    ans %= 1000000007;
    // if(temp<0){
    // cerr << N << " " << K << " " << i << " " << ans << endl;
    // }
  }
  ans %= 1000000007;
  cout << ans << "\\n";
  return ans;
}

// class input{
//     public:
//     input(int _A, int _B, int _C, int _X, int _ans){
//         A = _A;
//         B = _B;
//         C = _C;
//         X = _X;
//         ans = _ans;
//     }
//     int A;
//     int B;
//     int C;
//     int X;
//     int ans;
// };

// int solve(input key){
//     return solve(key.A, key.B, key.C, key.X);
// }

int main() {
  int N, K;
  bool use_stdin = true;
  // use_stdin = false;
  if (use_stdin) {
    cin >> N >> K;
    solve(N, K);
  } else {
    // input keys[] = {
    //     {30,40,50,6000,213},
    //     {5,1,0,150,0},
    //     {2,2,2,100,2}
    // };
    // for(auto key:keys){
    //     assert(solve(key)==key.ans);
    // }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cassert>
#include <stdio.h>

using namespace std;

unsigned long long a(unsigned long long N, unsigned long long K) {
  return (N - K + 1) * K + 1;
}

unsigned long long solve(unsigned long long N, unsigned long long K) {
  unsigned long long ans = 0;
  for (int i = K; i <= N + 1; i++) {
    unsigned long long temp = a(N, i) % 1000000007;
    ans += temp;
    ans %= 1000000007;
    // if(temp<0){
    cerr << N << " " << K << " " << i << " " << ans << endl;
    // }
  }
  ans %= 1000000007;
  cout << ans << "\\n";
  return ans;
}

// class input{
//     public:
//     input(int _A, int _B, int _C, int _X, int _ans){
//         A = _A;
//         B = _B;
//         C = _C;
//         X = _X;
//         ans = _ans;
//     }
//     int A;
//     int B;
//     int C;
//     int X;
//     int ans;
// };

// int solve(input key){
//     return solve(key.A, key.B, key.C, key.X);
// }

int main() {
  int N, K;
  bool use_stdin = true;
  // use_stdin = false;
  if (use_stdin) {
    cin >> N >> K;
    solve(N, K);
  } else {
    // input keys[] = {
    //     {30,40,50,6000,213},
    //     {5,1,0,150,0},
    //     {2,2,2,100,2}
    // };
    // for(auto key:keys){
    //     assert(solve(key)==key.ans);
    // }
  }
  return 0;
}
Problem p02708 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <math.h>
using namespace std;
typedef long long ll;

int main() {
  ll mod = (ll)pow(10, 9) + 7;
  ll n, k;
  cin >> n >> k;
  ll min = 0;
  ll max = 0;
  ll count = 0;
  for (k; k <= n + 1; k++) {
    count += (k * (n - k + 1) + 1) % mod;
  }

  cout << count % mod << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>
using namespace std;
typedef long long ll;

int main() {
  ll n, k;
  cin >> n >> k;
  ll min = 0;
  ll max = 0;
  ll count = 0;
  for (k; k <= n + 1; k++) {

    for (int j = 0; j < k; j++) {
      min += j;
      max += (n - j);
    }
    count += (max - min + 1);
    min = 0;
    max = 0;
  }
  ll mod = (ll)pow(10, 9) + 7;
  cout << count % mod << endl;
  return 0;
}
Problem p02708 (C++) — buggy submission, verdict: Runtime Error

// Winners don't quit and Quitters don't win
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<int>
#define vll vector<ll>
#define mod 1000000007
#define MAX 100005
ll sum[2 * MAX];
void solve_() {
  ll i;
  for (i = 1; i < 2 * MAX - 2; ++i) {
    sum[i] = sum[i - 1] + i;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve_();
  ll n, k, i, ans = 1;
  cin >> n >> k;
  for (i = k; i <= n; ++i) {
    ans = (ans + (sum[n] - sum[n - i]) - sum[i - 1] + 1 + mod) % mod;
  }
  cout << ans % mod;
}

Error: WA

Fixed submission:

// Winners don't quit and Quitters don't win
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<int>
#define vll vector<ll>
#define mod 1000000007
#define MAX 100005
ll sum[2 * MAX];
ll solve_() {
  ll i;
  for (i = 1; i < 2 * MAX - 2; ++i) {
    sum[i] = sum[i - 1] + i;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve_();
  ll n, k, i, ans = 1;
  cin >> n >> k;
  for (i = k; i <= n; ++i) {
    ans = (ans + (sum[n] - sum[n - i]) - sum[i - 1] + 1 + mod) % mod;
  }
  cout << ans % mod;
}
Problem p02708 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
const int mod = 1e9 + 7;

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

  // パターンの数は Σ N+1_C_i, i= 0 ~ N-K+1 だが重複を考慮
  // i個を選んだ時の和は SUMmax - SUMmin + 1 通り (i = 0 ~ N-K+1)
  ll sm = 0;
  ll Smx = 0, Smn = 1; // 初回補正のため1から
  for (ll i = 0; i <= N - K + 1; ++i) {
    Smn += i - 1;
    Smx = N * i - Smn;
    sm = (sm + Smx - Smn + 1) % mod;
    // cout << i << ":" << Smx << "," << Smn << ":" << Smx - Smn + 1 << endl;
  }
  cout << sm << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
const int mod = 1e9 + 7;

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

  // パターンの数は Σ N+1_C_i, i= 0 ~ N-K+1 だが重複を考慮
  // i個を選んだ時の和は SUMmax - SUMmin + 1 通り (i = 0 ~ N-K+1)
  ll sm = 0;
  for (ll i = 0; i <= N - K + 1; ++i) {
    ll Smx = 0, Smn = 0;
    for (ll j = 0; j < i; ++j) {
      Smx += N - j;
      Smn += j;
    }
    sm = (sm + Smx - Smn + 1) % mod;
  }
  cout << sm << endl;
}
Problem p02708 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long lint;
typedef long long llint;

static const llint MAX = 2e5 + 5;
// static const int NIL = -1;
// static const ll INF = 1<<21;
static const llint MOD = 1e9 + 7;

bool compPair(const pair<int, int> &arg1, const pair<int, int> &arg2) {
  return arg1.first > arg2.first;
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

int main(void) {
  llint n, k;
  cin >> n >> k;

  llint ans = 0, tmp, s, g;
  for (llint in = k; in <= n + 1; in++) {
    s = 0;
    g = 0;
    s = (in) * (in - 1) / 2;
    g = n * in - (in) * (in - 1) / 2;
    g = min(n * (n + 1) / 2, g);
    tmp = g - s + 1;
    tmp %= MOD;
    ans += tmp;
    // cout << s << g <<tmp << endl;
    ans %= MOD;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

typedef long lint;
typedef long long llint;

static const llint MAX = 2e5 + 5;
// static const int NIL = -1;
// static const ll INF = 1<<21;
static const llint MOD = 1e9 + 7;

bool compPair(const pair<int, int> &arg1, const pair<int, int> &arg2) {
  return arg1.first > arg2.first;
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

int main(void) {
  llint n, k;
  cin >> n >> k;

  llint ans = 0, tmp, s, g;
  for (llint in = k; in <= n + 1; in++) {
    s = 0;
    g = 0;
    for (llint ip = 0; ip < in; ip++) {
      s += ip;
      g += n - ip;
    }
    g = min(n * (n + 1) / 2, g);
    tmp = g - s + 1;
    tmp %= MOD;
    ans += tmp;
    // cout << s << g <<tmp << endl;
    ans %= MOD;
  }

  cout << ans << endl;

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

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

const int mod = 1000000007;

struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

ll patan(ll _i, ll _j) {
  ll num = (_i + _j) * (_j - _i + 1) / 2;

  return num;
}

int main() {
  mint ans = 0;
  int n, k;
  cin >> n >> k;

  for (int i = k; i <= n + 1; i++) {
    ll min, max;
    min = patan(0, i - 1);
    max = patan(n - i + 1, n);
    ans += max - min + 1;
  }

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


Error: TLE

Fixed submission:

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

int n;
const int mod = 1000000007;

int patan(int _i) {
  int max = 0, min = 0;

  for (int i = 0; i < _i; i++) {
    min += i;
    min %= mod;
  }

  for (int i = n; i >= n + 1 - _i; i--) {
    max += i;
    max %= mod;
  }
  return (max - min + 1) % mod;
}

int main() {
  int ans = 0;
  int k;
  cin >> n >> k;

  for (ll i = k; i <= n + 1; i++) {
    ans += patan(i);
    ans %= mod;
  }

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

#include <iostream>
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, s, n) for (ll i = s; i < (n); i++)
using namespace std;

int main() {
  ll n, k;
  cin >> n >> k;

  ll min = 0;
  ll max = 0;
  ll ans = 0;
  ll num = 0;
  rep(i, k - 1) {
    min += num;
    max += n - num;
    num++;
  }
  rep2(i, k, n + 2) {
    min += num;
    max += n - num;
    ans += max - min + 1;
    ans %= (1000000000 + 7);
    num++;
  }

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

Error: TLE

Fixed submission:

#include <iostream>
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, s, n) for (ll i = s; i < (n); i++)
using namespace std;

int main() {
  ll n, k;
  cin >> n >> k;

  ll min, max;
  ll ans = 0;
  rep2(i, k, n + 2) {
    min = 0;
    max = 0;
    rep(j, i) {
      min += j;
      max += n - j;
    }
    ans += max - min + 1;
    ans %= (1000000000 + 7);
  }

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