abc163_d - Sum Of Large Numbers

I’m kinda lazy to explain this problem, so here’s the editorial

Anyhow, the key points are:

  • Fix the quantity of numbers to sample, $i$, $k \leq i \leq n + 1$.
  • Note that we can produce any number in between the minimum and maximum sum of $i$ numbers.
  • For each desired $i$ compute the size of the range described above.
  • Note that those ranges do not overlap.

Time complexity: $O(n)$

Memory complexity: $O(1)$

Click to show code.


using namespace std;
using ll = long long;
int const MOD = 1e9 + 7;
ll sq(ll n) { return (n * (n + 1)) / 2; }
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int n, k;
    cin >> n >> k;
    ll ans = 0;
    for (int i = k; i <= n + 1; ++i)
    {
        ans += sq(n) - sq(n - i) - sq(i - 1) + 1;
        ans %= MOD;
    }
    cout << ans << endl;
    return 0;
}