Problem:


Score : 400 points

Problem Statement

We have N+1 integers: 10^{100}, 10^{100}+1, ..., 10^{100}+N.

We will choose K or more of these integers. Find the number of possible values of the sum of the chosen numbers, modulo (10^9+7).

Constraints

1 \leq N \leq 2\times 10^5

1 \leq K \leq N+1

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of possible values of the sum, modulo (10^9+7).

Sample Input 1

3 2

Sample Output 1

10

The sum can take 10 values, as follows:

(10^{100})+(10^{100}+1)=2\times 10^{100}+1

(10^{100})+(10^{100}+2)=2\times 10^{100}+2

(10^{100})+(10^{100}+3)=(10^{100}+1)+(10^{100}+2)=2\times 10^{100}+3

(10^{100}+1)+(10^{100}+3)=2\times 10^{100}+4

(10^{100}+2)+(10^{100}+3)=2\times 10^{100}+5

(10^{100})+(10^{100}+1)+(10^{100}+2)=3\times 10^{100}+3

(10^{100})+(10^{100}+1)+(10^{100}+3)=3\times 10^{100}+4

(10^{100})+(10^{100}+2)+(10^{100}+3)=3\times 10^{100}+5

(10^{100}+1)+(10^{100}+2)+(10^{100}+3)=3\times 10^{100}+6

(10^{100})+(10^{100}+1)+(10^{100}+2)+(10^{100}+3)=4\times 10^{100}+6

Sample Input 2

200000 200001

Sample Output 2

1

We must choose all of the integers, so the sum can take just 1 value.

Sample Input 3

141421 35623

Sample Output 3

220280457



Sample input:
3 2


Sample output:
10

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
mod = pow(10, 9)+7
Cmax = ((N+N-K+1)*K)//2
Cmin = ((K-1)*K)//2
ans = 1
for i in range(K, N+1):
    ans = (ans+Cmax-Cmin+1)
    Cmin += i
    Cmax += (N-i)

print(ans % mod)

Submission status: Accepted

Solution code:
N,K = map(int, input().split())
div = 10**9 + 7
ans = 0
for p in range(K,N+2):
    ans += p * (N - p + 1) + 1
    
print(ans%div)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
M = 10**9 + 7

def f(a,b):
    return (a+b)*(b-a+1)//2

ans = 0
for s in range(k, n+2):
    l = f(0,s-1) % M
    r = f(n-s+1,n) % M
    ans += r-l+1

print(ans%M)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
MOD = 10 ** 9 + 7

# low, high それぞれ取りうる最小の値と最大の値
low, high = 0, 0
ans = 0
# x: 任意の数をx個取った時を想定してループを回すための変数
for x in range(1, N + 2):
    # lowは 0 + 1 +... K-1までの値を足すことになる(そうすればK個任意の数を選んだ時の最小値となる)
    # highは K + K+1 + ... + Nまでの値を足すことになる(そうすればK個任意の数を選んだ時の最大値となる)
    low += x - 1
    high += N - x + 1
    if x >= K:
        ans += (high - low + 1)
        ans %= MOD

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import itertools
N, K = map(int, input().split())
a = [0] * (N+1)
for i in range(N+1):
  a[i] = i

max_total = 0
min_total = 0
ans = 0
for k in range(K, N+2):
  # 和の最大(10**100 は無視): 大きい順にk個選んだ場合
  max_total = k * ((N-k+1) + N) // 2

  # 和の最小(10**100 は無視): 小さい順にk個選んだ場合
  min_total = k * (0 + (k-1)) // 2

  # 最小と最大の間は全パターン取り得るので差が個数になる
  ans += (max_total - min_total + 1)

print(ans % (10**9 + 7))

Submission status: Accepted

Solution code:
#import numpy as np
#import math
#from decimal import *
#from numba import njit

#@njit
def main():
    N,K = map(int, input().split())
    MOD = 10**9+7

    count = 0
    for k in range(K,N+2):
        count += ((2*N-k+1)*k//2 - (k-1)*k//2 + 1)%MOD
    print(count%MOD)
main()

Submission status: Accepted

Solution code:
import numpy

MOD = 10**9+7
N, K = [int(x) for x in input().split()]

cum = numpy.cumsum(range(N+1), dtype='int64')
count = 0

for i in range(K, N+1):
    min_num = cum[i-1]
    max_num = cum[-1] - cum[-i-1]

    count += (max_num - min_num + 1)
    count %= MOD

print(count + 1)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
sm=0
for i in range(k,n+2):
  k2=i*(n-i+1)+1
  sm=(sm+k2)%(10**9+7)
print(sm)

Submission status: Accepted

Solution code:
from itertools import accumulate

n, k = map(int, input().split())
MOD = 10 ** 9 + 7
acc = [0] + list(accumulate(range(n + 1)))

ans = 0
for i in range(k, n + 2):
    mi = acc[i]
    ma = acc[-1] - acc[-i - 1]
    ans += ma - mi + 1
    ans %= MOD

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def f(n):
    s = (n*(n+1))//2
    return s


N,K = map(int,input().split())
m= 10**9+7
ln = list(range(N+1))
num = 0
for i in range(K,N+2):
    num += f(N+1)-f(N+1-i)-f(i)+1
print(num%m)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

n,k = map(int, input().split())
mod = 10**9+7

ans = 0
for i in range(k, n + 2):
    mi = (i - 1) * i // 2
    ma = (n + n - i + 1) * i // 2
    ans += ma - mi + 1
print(ans % mod)

Submission status: Accepted

Solution code:
def su(n):
    if n < 0:
        return 0
    return n*(n+1)//2
MOD = 10 ** 9 + 7
N, K = map(int, input().split())
num = [i for i in range(N+1)]
ans = 0
su_N = su(N)
for i in range(K, N+2):
    min = su(i-1)
    max = su_N - su(N-i)
    ans += max - min + 1
    ans %= MOD
print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())

sum_pattern_num = 0

for num_cnt in range(K, N+2):
    if num_cnt == K:
        min_sum_frac = sum([i for i in range(0, K)])
        max_sum_frac = sum([i for i in range(N - K + 1, N + 1)])
    else:
        min_sum_frac = min_sum_frac + (num_cnt - 1)
        max_sum_frac = max_sum_frac + (N - num_cnt + 1)
    sum_pattern_num = \
        (sum_pattern_num + (max_sum_frac - min_sum_frac + 1)) % 1000000007

print(sum_pattern_num)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, K = map(int, readline().split())

    ans = 0
    for i in range(K, N + 2):
        left = i * (i - 1) // 2
        right = i * (2 * N - i + 1) // 2
        ans = (ans + right - left + 1) % MOD

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())

mod = 1000000007

ans = 0
for i in range(K, N+1+1):
    num_min = i*(i-1)//2
    num_max = i*(N+N-i+1)//2
#    print(num_min, num_max)
    ans += (num_max - num_min + 1)%mod
print(ans%mod)

Submission status: Accepted

Solution code:
mod = 10**9+7
n,k = map(int,input().split())
ans = 0
for i in range(k,n+2):
    ans += (i*n-i*(i-1)+1)%mod
ans %=mod
print(ans)

Submission status: Accepted

Solution code:
MOD = 1000000007
n, k = map(int, input().split())
rsw = [0]*(n+2)
for i in range(1,n+2):
    rsw[i] = (rsw[i-1]+i-1)%MOD
rsw_e = [0]*(n+2)
for i in range(1,n+2):
    rsw_e[i] = (rsw_e[i-1]+n-(i-1))%MOD
res = 0
for i in range(k,n+2):
    start = rsw[i]
    end = rsw_e[i]
    res += (end-start+1)%MOD
print(res%MOD)

Submission status: Accepted

Solution code:
from itertools import accumulate
N, K = map(int, input().split())
MOD = 10**9 + 7
cum = list(accumulate([0]+ list(range(N+1))))
print((sum(cum[N+1] - cum[N+1-k] - cum[k] + 1 for k in range(K,N+2)))%MOD)

Submission status: Accepted

Solution code:
n,k = map(int, open(0).read().split())

left = right = 0
ans = 0
MOD = 10**9+7

for c,(i,j) in enumerate(zip(range(n+1), range(n,-1,-1))):
    left += i
    right += j
#     print(i,j, right, left,c)
    if c >= k-1:
        ans += (right-left+1)
        ans %= MOD

print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
mod=10**9+7
c=0
for i in range(k,n+2):
  min=(1/2)*i*(i-1)
  max=(1/2)*i*(2*n+1-i)
  c+=max-min+1
print(int(c%mod))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = list(n for n in range(N+1))
ans = 0
for i in range(K,N+2):
  L = i*(i-1)//2
  R = i*(2*N+1-i)//2
  ans += (R-L+1)
print(ans%((10**9)+7))

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, k = map(int, input().split())

    res = 0
    for i in range(k, n + 2):
        mi = (i - 1) * i // 2
        ma = (n + (n - i + 1)) * i // 2
        res += ma - mi + 1
        res %= mod
    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
N, K = map(int,input().split())
ans = 0
MOD = 10**9 + 7
for k in range(K, N+2):
  ans += -k*(k-1)//2+(2*N-k+1)*k//2+1
  ans %= MOD
print(ans)

Submission status: Accepted

Solution code:

n,k = map(int,input().split())
a = list(range(n))

ans = 0

for i in range(k,n+2):
    min1 = 0.5*i*(i-1)
    max1 = 0.5*(n*(n+1)-(n-i)*(n-i+1))
    sub1 = max1 - min1+1
    ans += sub1


print(int(ans%(10**9+7)))

Submission status: Accepted

Solution code:
import sys
MOD = 10**9 + 7


def sum_from_to(fr, to):
    return (to - fr + 1) * (fr + to) // 2 % MOD


def main():
    input = sys.stdin.buffer.readline
    n, k = map(int, input().split())
    ans = 0
    for i in range(k, n + 2):
        ans += sum_from_to(n - i + 1, n) - sum_from_to(0, i - 1) + 1
        ans %= MOD
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    n, k = (int(i) for i in input().split())
    num_sums = sum((n-j+1)*j + 1 for j in range(k, n+2)) 
    print(num_sums % (10**9 + 7))

main()

Submission status: Accepted

Solution code:
N, K =map(int, input().split())
mod = 10**9 + 7
A = int(-(N+1)*(N+2)*(2*N+3)/6)%mod
B = int((N+1)*(N+1)*(N+2)/2)%mod
C = N+1
a = int(-(K-1)*K*(2*K-1)/6)%mod
b = int((N+1)*(K-1)*K/2)%mod
c = K-1
print((A+B+C-a-b-c)%mod)

Submission status: Accepted

Solution code:
def main():
    n, k = (int(i) for i in input().split())

    # min_sum = sum([0, k)) = (k-1)(k-2) // 2
    # max_sun = sum([n-k+1, n+1))  = min_sum + (n-k+1)*k
    # total_sums = (n-k+1)*k + 1

    num_sums = sum((n-j+1)*j + 1 for j in range(k, n+2)) 
    print(num_sums % (10**9 + 7))


main()

Submission status: Accepted

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n,k = list(map(int, input().split()))
ans = 0
M = 10**9+7
for i in range(k, n+2):
    ans += (n-i+1+n)*(i)//2 - i*(i-1)//2 + 1
    ans %= M
#     print(i,ans)
print(ans%M)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
mod = pow(10,9)+7
ans = 0
l, h = 0, 0
for i in range(1,n+2):
    l += i-1
    h += n-i+1
    if i>=k: ans += (h-l+1)%mod
print(ans%mod)

Submission status: Accepted

Solution code:
def main():
    N, K = map(int, input().split())
    MOD = 1_000_000_007
    l = 0
    r = 0
    ans = 0
    for i in range(N + 1):
        l += i
        r += N - i
        if i + 1 >= K:
            ans += r - l + 1
            ans %= MOD
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
N,K=map(int,input().split())

sum=0
for i in range(K,N+2):
#  print(N*(N+1)//2-((N-i)*(N-i+1)//2),i*(i-1)//2)
  sum+=(N*(N+1)//2)-((N-i)*(N-i+1)//2)-(i*(i-1)//2)+1
  sum%=(10**9+7)
print(sum%(10**9+7))

Submission status: Accepted

Solution code:
N,K=map(int, input().split())

sum_ = 0
for k in range(K, N+2):
  sum_ += k*(N-k+1)+1
  sum_ = sum_ % (10**9+7)

print(sum_)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
ans = 0
for i in range(K, N+2):
    ans += (N-i+1)*i + 1
print(ans%(10**9+7))

Submission status: Accepted

Solution code:
N, K = [int(_) for _ in input().split()]
mod = 10 ** 9 + 7
ans = 0
for i in range(K, N + 2):
    ans += i * (N - i + 1) + 1
    ans %= mod
print(ans)

Submission status: Accepted

Solution code:
MOD = 10**9 +7
n,k=map(int,input().split())
ans = 0
for i in range(k,n+2):
    ans += ((2*n-i+1)*i - i*(i-1)) //2 + 1
print(ans%MOD)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())

su = 0
for i in range(k, n+2):
    co = (n+(n-i+1))*i/2 - (i-1)*i/2 + 1
    su = (su + int(co)) % (10**9 + 7)
    
print(su)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jul  5 00:28:02 2020

@author: Aruto Hosaka
"""


def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

mod = 10**9+7
N,K = map(int, input().split())

ans1 = N+1
ans1 *= N+K+1
ans1 %= mod
ans1 *= N+2-K
ans1 %= mod
ans1 *= modinv(2)
ans1 %= mod

ans2 = N+1
ans2 *= N+2
ans2 %= mod
ans2 *= 2*N+3
ans2 %= mod
ans2 *= modinv(6)
ans2 %= mod

ans3 = K-1
ans3 *= K
ans3 %= mod
ans3 *= 2*K-1
ans3 %= mod
ans3 *= modinv(6)
ans3 %= mod

ans4 = N+2-K

ans = ans1 - ans2 + ans3 + ans4
ans %= mod
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
ans=0
max=0
min=0
for i in range(k,n+1):
    ans+=i*(n-i+1)+1
    ans%=1000000007
print(ans+1)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
DIV = 10**9+7

count = 0
for i in range(k,n+2):
  min_k = i*(i-1)/2
  max_k = i*(2*n-i+1)/2
  count += max_k - min_k + 1
print(int(count % DIV))

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7

n, k = map(int, input().split())

ans = 0
for i in range(k, n + 2):
    cnt = i * (n + n - i + 2) // 2 - i * i // 2 + 1
    ans += cnt
    ans %= MOD
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


def solve():
    N, K = map(int, input().split())

    MOD = (10**9) + 7
    ans = 0
    for i in range(K, N + 2):
        ans += sum_of_arithmetic_progression(N, -1, i) - sum_of_arithmetic_progression(0, 1, i) + 1
        ans %= MOD

    print(ans)


def main():
    solve()


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
from numba import jit

n, k = map(int, input().split())

tmp=[]
for i in range(n+1):
    tmp.append(i)
ary = np.array(tmp)
#print(ary)

@jit
def lr(l,r): # 等差数列の'l'から'r'の和_mean*number
    return (l+r)*(r-l+1)//2

ans = 0
for j in range(k,n+2):
    l = lr(0, j-1)
    r = lr(n+1-j, n)
    ans += r-l+1
    ans %= 10**9+7
    
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

n, k = map(int, input().split())

ary = np.array([i for i in range(n+1)])

def lr(l,r): # 等差数列の'l'から'r'の和_mean*number
    return (l+r)*(r-l+1)//2

ans = 0
for j in range(k,n+2):
    l = lr(0, j-1)
    r = lr(n+1-j, n)
    ans += r-l+1
    ans %= 10**9+7
    
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

n, k = map(int, input().split())

ary = np.array([i for i in range(n+1)])
#print(ary)

def lr(l,r): # 等差数列の'l'から'r'の和_mean*number
    return (l+r)*(r-l+1)//2

ans = 0
for j in range(k,n+1):
    l = lr(0, j-1)
    r = lr(n+1-j, n)
    ans += r-l+1
    
ans += 1
ans %= 10**9+7

print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
ans = 0
mod = 10 ** 9 + 7
for k in range(K, N + 2):
    max_range = int(k * (2 * N - k + 1) / 2)
    min_range = int((k-1) * k / 2)
    tmp = (max_range - min_range + 1) % mod
    ans += tmp
    ans %= mod

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

n, k = map(int, input().split())
#print(n,k)
ary = np.array([i for i in range(n+1)])
cum = np.cumsum(ary)
#print(cum)

ans = 0
for j in range(k,n+1):
    ans += (cum[-1] - cum[-j-1]) - cum[j-1] + 1
    
else:
    ans += 1
    ans %= 10**9+7

print(ans)

Submission status: Accepted

Solution code:
N, K = list(map(int,input().split()))
ans = 0
INF = 10 ** 9 + 7
for i in range(K, N + 2):
    mi = i * (i - 1) // 2
    ma = i * (2 * N - i + 1) // 2
    ans += ma - mi + 1
    ans %= INF
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')
# from math import ceil, floor, log2
# from collections import deque
# from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np    # cumsum
# from bisect import bisect_left, bisect_right
def f(l, r, kousa=1):
    n = (r - l) // kousa
    return (r + l) * (n + 1) // 2

def solve():
    N, K = MI()
    ans = 0
    MOD = 1000000007
    for i in range(K, N+2):
        mn = f(0, i-1)
        mx = f(N-i+1, N)
        ans = ans + mx - mn + 1
    print(ans % MOD)

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import numpy as np

n, k = map(int, input().split())
ary = np.array([i for i in range(n+1)])
#print(ary)
cum_1 = np.cumsum(ary)
cum_2 = np.cumsum(ary[::-1])
#print(cum_1, cum_2)

ans = 0
for j in range(k-1,n+1):
    ans += cum_2[j] - cum_1[j] + 1
    ans %= 10**9+7

print(ans)

Submission status: Accepted

Solution code:
def II(): return int(input())
def MI(): return map(int, input().split())
N,K=MI()
ans=0
MOD=10**9+7
for i in range(K,N+2):
  x=(N+1-i)*i+1
  ans+=x
  ans%=MOD
print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
mod = 10 ** 9 + 7
ans = 0

for i in range(K,N+2):
    a = i * (N + 1 - i) + 1
    a %= mod
    ans += a
    ans %= mod

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict as dd
from collections import deque
import bisect
import heapq

def ri():
    return int(input())

def rl():
    return list(map(int, input().split()))

def T(n):
    return n * (n + 1) // 2

def S(a, b):
    return T(b) - T(a - 1)

def solve():
    n, k = rl()
    ans = 1
    for i in range(k, n + 1):
        lo = T(i - 1)
        hi = S(n - i + 1, n)
        ans += hi - lo + 1
        ans %= (10**9 + 7)
        # print (ans)

    print (ans)






mode = 's'

if mode == 'T':
    t = ri()
    for i in range(t):
        solve()
else:
    solve()

Submission status: Accepted

Solution code:
N, K = map(int,input().split())
ans = 0
MOD = 10**9 + 7
for i in range(K,N+2):
    x = (N+1-i) * i + 1
    ans += x
    ans %= MOD
print (ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=0
for i in range(k,n+2):
    l=(i-1)*i//2
    r=(n-i+1+n)*i//2
    a+=(r-l+1)
    a%=10**9+7
print(a)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, K = mapint()
mod = 10**9+7

ans = 0
for i in range(K, N+2):
    ans += (N+(N-i+1))*i//2-(i*(i-1)//2)+1
    ans %= mod
print(ans)

Submission status: Accepted

Solution code:
p = 10**9+7
N,K = map(int,input().split())
a = (((N+1)*(N+2))//2)%p
a = (a*(N+1))%p
b = (((K-1)*K)//2)%p
b = (b*(N+1))%p
c = (((N+1)*(N+2)*(2*N+3))//6)%p
d = (((K-1)*K*(2*K-1))//6)%p
e = (N-K+2)%p
ans = (a-b-c+d+e)%p
print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
print((((K-N-2)*(2*K*K-K*(N+2)-N*(N+1)))//6+N+2-K)%(10**9+7))

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
MOD = 10**9+7

mn = mx = 0
for i in range(K):
    mn += i
    mx += N-i
ans = mx - mn + 1

for i in range(K,N+1):
    mn += i
    mx += N-i
    ans += mx - mn + 1
    ans %= MOD

print(ans%MOD)

Submission status: Accepted

Solution code:
n,k= map(int,input().split())
ans=0
for i in range(k,n+2):
  ans +=i*n-i*(i-1)+1
print(ans%(10**9+7))

Submission status: Accepted

Solution code:
n,k = map(int, input().split())

ans = 0
for i in range(k, n + 2):
  min = i*(0+i-1)/2
  max = i*((n-i)+(n+1))/2
  ans += max - min +1
  
print(int(ans % (10**9 + 7)))

Submission status: Accepted

Solution code:
N,K = map(int, input().split())
div = 10**9 + 7
ans = 0
for i in range(K, N+2):
    ans += i + N*i - i**2 + 1
print(ans%div)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
mod = 10**9+7
from itertools import accumulate
N = list(range(n+1))
N_acc = [0]+list(accumulate(N))
ans = 0
for i in range(k,n+2):
    ans += ((N_acc[-1]-N_acc[-i-1])-N_acc[i]+1)%mod
print(ans%mod)

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest
import os

# 再帰処理上限(dfs作成時に設定するのが面倒なので限度近い値を組み込む)
sys.setrecursionlimit(999999999)


# 実装を行う関数
def resolve(test_def_name=""):
    n, k = map(int, input().split())

    ans = 0

    min_num = 0
    desk_list = list(reversed(range(n + 1)))

    max_num = 0
    ack_list = list(range(n + 1))

    for i in range(1, n + 1):
        min_num = (min_num + desk_list.pop()) % (pow(10, 9) + 7)
        max_num = (max_num + ack_list.pop()) % (pow(10, 9) + 7)
        if k <= i:
            ans = (ans + max_num - min_num + 1) % (pow(10, 9) + 7)

    print(ans+1)


# テストクラス
class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve(sys._getframe().f_back.f_code.co_name)
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """3 2"""
        output = """10"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """200000 200001"""
        output = """1"""
        self.assertIO(test_input, output)

    def test_input_3(self):
        test_input = """141421 35623"""
        output = """220280457"""
        self.assertIO(test_input, output)

    # 自作テストパターンのひな形(利用時は「tes_t」のアンダーバーを削除すること
    def tes_t_1original_1(self):
        test_input = """データ"""
        output = """データ"""
        self.assertIO(test_input, output)


# 実装orテストの呼び出し
if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        # AtCoder提出時の場合
        resolve()

    else:
        # 自PCの場合
        unittest.main()

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
su = 0
mod = 10**9+7
for i in range(k,n+2):
  a = i*(i-1)//2
  b = i*(2*n+1-i)//2
  su += b-a+1
 # print(a,b,su)
  su %= mod
print(su)

Submission status: Accepted

Solution code:
import itertools

N, K = map(int, input().split())

N_acum = list(itertools.accumulate(range(N+1)))
mod = 10**9+7
ans = 0
for i in range(K, N+1):
    ans += (N_acum[-1] - N_acum[-(i+1)]) - N_acum[i-1] + 1
    ans %= mod

ans = (ans+1)%mod

print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())

ans = 0
for i in range(k, n + 2):
    # print(i)
    smin = int(0.5 * i * (2 * 0 + (i - 1) * 1))
    smax = int(0.5 * i * (2 * (n + 1 - i) + (i - 1) * 1))
    s = smax - smin + 1
    # print("s", s)
    ans += s
    ans = ans % (7 + 10 ** 9)
print(ans)

Submission status: Accepted

Solution code:
import math

N,K=map(int,input().split())


sum=0
for s in range(K,N+1):
    sum+=(s*(2*N-s+1))/2-(s*(s-1))/2+1
print(int(sum+1)%1000000007)

Submission status: Accepted

Solution code:
n, k = map(int,input().split())
mod = 10 ** 9 + 7

cnt = 0
for i in range(k, n + 2):
    temp = i * (2 * n - i + 1) // 2 - i * (i - 1) // 2 + 1
    cnt = (cnt + temp) % mod
    #print(i, cnt, temp)
print(cnt)

Submission status: Accepted

Solution code:
N, K = list(map(int,input().split()))

def sum_(i, j):
    return (a[i] + a[j]) * (j - i + 1) // 2

a = [i for i in range(N + 1)]
ans = 0
for i in range(K, N + 2):
    min_ = sum_(0, i - 1)
    max_ = sum_(N - i + 1, N)
    ans += (max_ - min_ + 1)
    ans %= 10 ** 9 + 7
print(ans)

Submission status: Accepted

Solution code:
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


MOD = 10 ** 9 + 7


def calc_cumsum(l):
    cumsum = [l[0]]
    for i in l[1:]:
        cumsum.append(cumsum[-1] + i)
    return cumsum


n, k = LI()
nums = list(range(n + 1))
ans = 0
ruiseki_min = calc_cumsum(nums)
ruiseki_max = calc_cumsum(nums[::-1])
for i in range(k, n + 2):
    min_sum = ruiseki_min[i - 1]
    max_sum = ruiseki_max[i - 1]
    ans += max_sum - min_sum + 1
    ans %= MOD
print(ans)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc163/tasks/abc163_d


def main():
    n, k = map(int, input().split(" "))
    # このやり方だと当然TLE
    # total = 0
    # for i in range(k, n + 2):
    #     a = 0
    #     b = 0
    #     for j in range(i):
    #         a += j
    #         b += n - j
    #     total += b - a + 1
    # print(total)
    max_num = sum(range(n - k + 1, n + 1))
    min_num = sum(range(k))
    c = max_num - min_num + 1
    total = c
    for i in range(k, n + 1):
        max_num += n - i
        min_num += i
        c = max_num - min_num + 1
        total += c
    print(total % 1000000007)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,k = map(int,input().split());
kk = n-k+2
a = [0]*kk

for i in range(kk):
    ii = i + k
    p = ii*(ii-1);
    q = ii*(2*n+1-ii);
    a[i] = (q//2)-(p//2)+1;
con = 0;
for v in range(kk):
    con = (con+a[v]) % 1000000007;
    
print(con);

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
s = 0
for i in range(k, n + 2):
    s += i * (n - i + 1) + 1
    s %= 10 ** 9 + 7
print(s)

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7
n, k = map(int, input().split())
ans = 0
for x in range(k, n + 2):
    p = x * (x - 1) // 2
    q = x * (x - 1) // 2 + (n - x + 1) * x
    ans += q - p + 1
print(ans % MOD)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
mod = 10**9 + 7

res = 0
z = n*(n+1)//2
for x in range(k, n+2):
    min_ = (x-1)*x//2
    max_ = z - (n-x)*(n+1-x)//2
    res += max_ - min_ + 1
    res %= mod

print(res)

Submission status: Accepted

Solution code:
import math
N,K=map(int,input().split())

A=[i for i in range(N+1)]
wa=0
N=N+1
for i in range(K,N+1):

    mi=(i*(0+(i-1)))/(2)
    ma=(i*(N-i+(N-1)))/(2)

    wa=ma-mi+1+wa


wa=wa%(10**9+7)
print(int(wa))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())

a = 0
for i in range(k, n+2):
    a += (n*i + i - i**2 + 1)

ans = a % (10**9+7)
print(ans)

Submission status: Accepted

Solution code:
import math
N, K = map(int, input().split())
mod = 10**9+7

ans = 0
for k in range(K, N+2):
    #個数が異なると合計も変わるので毎度計算する
    #数列の小さい方からk個選ぶ
    min_part = (k-1)*k//2
    #数列の大きい方からk個選ぶ
    max_part = (N*(N+1)-(N-k)*(N-k+1))//2
    #差分を足す
    ans += max_part-min_part+1
    
    ans %= mod


print(ans)

Submission status: Accepted

Solution code:
import math
N, K = map(int,input().split())
MOD = 10**9 + 7
ans = 0
for k in range(K, N+2):
  min_part = (k-1)*k // 2
  max_part = (N*(N+1) - (N-k)*(N-k+1)) // 2
  # print(min_part, max_part)
  ans += max_part - min_part + 1
  ans %= MOD
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
res = 0
divnum = 10 ** 9 + 7
for i in range(k, n + 2):
  res += i * (n - i + 1) + 1
  res %= divnum
print(res)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())

# from scipy.special import comb
# a = comb(n, r)

MOD = 10**9 + 7
ans = 0
for k in range(K,N+2):
    # print(k)
    right = (N+N-k+1)*k // 2
    left = (k-1)*k // 2 
    ans += right - left + 1
    ans %= MOD
    # print(ans)
    # ans += comb(N+1, k, exact=True)
    # print(ans)


print(ans)

Submission status: Accepted

Solution code:
import sys 
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
mod = 10 ** 9 +7
N, K = map(int, readline().split())
# s = [0] * (N+1)
# g = [0] * (N+1)
# g[0] = N % mod
# for i in range(1,N+1):
#     s[i] += (s[i-1] + i ) % mod
#     g[i] += (g[i-1] + N - i) % mod
# ans = (sum(g[K-1:]) % mod  - sum(s[K-1:]) % mod + N-K+2) % mod
# print(ans)
k = np.arange(K,N+2,dtype = np.int64)
low = k*(k-1)//2
high = k*(2*N - k + 1) //2
cnt = high-low+1
print(sum(cnt)%mod)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())

m = 1000000007

result = 0
for i in range(K, N + 2):
    # max: N, N -1, ..., N - i + 1
    a = (N + (N - i + 1)) * i // 2
    # min: 0, 1, .., i - 1
    b = (0 + (i - 1)) * i // 2
    result += a - b + 1
    result %= m
print(result)

Submission status: Accepted

Solution code:
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
mod = 10 ** 9 +7
N, K = map(int, readline().split())
s = [0] * (N+1)
g = [0] * (N+1)
g[0] = N % mod
for i in range(1,N+1):
    s[i] += (s[i-1] + i ) % mod
    g[i] += (g[i-1] + N - i) % mod
ans = (sum(g[K-1:]) % mod  - sum(s[K-1:]) % mod + N-K+2) % mod
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
b = 0
c = 0
for i in range(k,n+2):
  b = n*i-i*(i-1)+1
  c +=b
print(c%(10**9+7))

Submission status: Accepted

Solution code:
# 初期入力
NN,K = (int(x) for x in input().split())

#K個の和、初項と修項
p =10**9 + 7
seq =[i for i in range(NN+1)] #0-Nまでの数列
ans =[0] *(NN+1)
st =[0]*(NN+1)
fi =[0]*(NN+1)
st[0] =sum(seq[  :K])
fi[0] =sum(seq[-K: ])
ans[0] =fi[0] -st[0] +1
if NN==1 and K==1:
    print(3)
else:
    for i in range(NN -K+1):
        st[i+1] =st[i] +seq[K+i]
        fi[i+1] =fi[i] +seq[-K-i-1]
        ans[i+1] =(fi[i+1] -st[i+1] +1)
    print(sum(ans) %p)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
a = list(range(n+1))
b = a[::-1]
ans = 0

min = sum(a[:k-1])
max = sum(b[:k-1])


for i in range(k,n+2):
    min += a[i-1]
    max += b[i-1]
    ele = max - min +1
    ans += ele

M =10**9 + 7
print(ans % M)

Submission status: Accepted

Solution code:
def main():
    n, k = list(map(int, input().split()))
    mod = 1000000007
    N = list(range(n + 1))
    mn = [0] * (n + 1)
    mx = [0] * (n + 1)
    mx[0] = n
    for i in range(1, n + 1):
        mn[i] = mn[i - 1] + N[i]
        mx[i] = mx[i - 1] + N[n - i]
    ans = 0
    for a, b in zip(mn[k - 1:], mx[k - 1:]):
        ans += b - a + 1
        ans %= mod
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    mod = 1e9 + 7
    n, k = list(map(int, input().split()))
    ans = 0
    for i in range(k, n + 2):
        ans += ((n + (n - i + 1)) * i // 2) - ((0 + (i - 1)) * i // 2) + 1
        ans = int(ans % mod)
    print(ans)
    return


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())

ans = 0


for i in range(K, N+2):
    mi = ((i-1)*i)//2
    ma = N*i-mi
    ans += ma-mi+1
    ans %= 10**9+7
    # print(mi, ma)

print(ans)