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:
import numpy as np

n, k = map(int, input().split())
MOD = 10 ** 9 + 7
ref = np.array(list(range(n + 1)), dtype='int64')

ans = 0

for i in range(k, n + 2):
    min_sum = (ref[i - 1] * (ref[i - 1] + 1)) // 2
    max_sum = (n + ref[-i]) * (n - ref[-i] + 1) // 2
    ans += max_sum - min_sum + 1
    ans %= MOD

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
MOD = 10**9 + 7
def solve(n, k):
    v = list(range(n+1))
    l = [0] * (n+1)
    u = [0] * (n+1)
    u[0] = n
    for i in range(1, n+1):
        l[i] = l[i-1] + v[i]
    for i in range(1, n+1):
        u[i] = u[i-1] + v[n-i]
    return sum((u[i] - l[i] + 1)for i in range(k-1, n+1)) % MOD

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

Submission status: Accepted

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

s = [0]
for i in range(N + 1):
  s.append(s[-1] + i)

ans = 0
for i in range(K, N + 2):
  ans += s[-1] - s[-(i + 1)] - s[i] + 1
  ans %= MOD
print(ans % MOD)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
ans=0
kari=[i for i in range(N+1)]
for i in range(K,N+2):
    saidai=(N+N-i+1)*i//2
    saisyou=(i-1)*i//2
    ans+=saidai-saisyou+1

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

c = 0
for i in range(k, n + 2):
  c += ((n - i + 1) * i + 1) % (10 ** 9 + 7)

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split(' '))
count = 0
for n in range(K, N+2):
    count += n*(N-n+1) + 1
count %= 1000000007
print(count)

Submission status: Accepted

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

total = 0
for k in range(K, N + 1 + 1):
    # 大きいほうから k 個選ぶ
    sum_max = ((N + N - (k - 1)) * k) / 2

    # 小さいほうから k 個選ぶときに最小値をとる
    sum_min = ((0 + k - 1) * k) / 2

    # 最大値と最小値の間の値はすべてとることができる
    # 最大値 ~ 最小値 の数を数えれば良い
    total += int(sum_max - sum_min + 1)

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def i1str():
    return input()


def istrs(n=None):
    if n is None:
        return input().split()
    a = []
    for _ in range(n):
        a.append(istrs())
    return a


def i1num():
    return int(input())


def inums(n=None):
    if n is None:
        return list(map(int, input().split()))
    a = []
    for _ in range(n):
        a.append(inums())
    return a


def ostrs(l, sp=" "):
    print(sp.join(l))


def onums(l, sp=" "):
    print(sp.join(map(str, l)))


def solve(n, k):
    m = 10**9+7
    res = 0
    for i in range(k, n+2):
        res += n*i - i*i + i + 1
    return res % m


if __name__ == "__main__":
    n, k = inums()
    print(solve(n, k))

Submission status: Accepted

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

# 10^100 = A
# [A, A+1, ..., A + N] (N+1個の数)
# K個以上選ぶときの和として，あり得るものの個数 (mod 10^9 + 7)
MOD = int(10 ** 9 + 7)

la = list(range(N + 1))

def counter(k):
    # laからk個取った場合の話としてあり得る個数
    # 最小のk個 (0, 1, ..., k - 1)
    # 最大のk個 (N - k + 1, ..., N)
    # print(k, "[0,{}]".format(k - 1), "[{},{}]".format(N - k + 1, N))

    # Too slow
    # ft = sum(la[:k])
    # fl = sum(la[-k:])

    # 1からk-1までの和 ((k - 1) * (k - 2)// 2)
    # print(sum(la[:k]), (k - 1) * k // 2)
    # print(sum(la[-k:]), (N + 1) * N // 2 - (N - k + 1) * (N - k) // 2)
    ft = (k - 1) * k // 2
    fl = (N + 1) * N // 2 - (N - k + 1) * (N - k) // 2

    # ftからflまで+1ずつ全部作れる，この個数
    # print(fl - ft + 1)
    return fl - ft + 1

count = 0
for k in range(K, N + 2):
    ans_k = counter(k)
    count = (count + ans_k) % MOD
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
from itertools import accumulate
a = [i for i in range(n+1)]
b = [i for i in reversed(range(n+1))]

ac_a = list(accumulate(a))
ac_b = list(accumulate(b))
ans = 0

for i in range(k-1,n+1):
    ans += ac_b[i]-ac_a[i]+1

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

Submission status: Accepted

Solution code:
import time
startTimeProblem=time.time()

import fileinput, sys, itertools, functools
# from math import *
import math
from bisect import *
from heapq import *
from collections import *

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

class InputHelper:
    def __init__(self):
        self.myinput = fileinput.input()

    def isLocal(self):
        return not fileinput.isstdin()

    def int(self):
        return int(self.myinput.readline().rstrip())

    def ints(self):
        return [int(_) for _ in self.myinput.readline().rstrip().split()]

    def str(self):
        return self.myinput.readline().rstrip()

    def strs(self):
        return [_ for _ in self.myinput.readline().rstrip().split()]

class OutputHelper:
    def int(self, a):
        print(a)    

    def ints(self, a):  
        print(" ".join([str(_) for _ in a]))
    
    def intsNL(self, a):
        for _ in a:
            print(_)
    
    def str(self, s):
        print(s)

    def strs(self, s):
        print(" ".join([_ for _ in s]))

    def strsNL(self, s):
        for st in s:
            print(st)

class ListNode:
    def __init__(self, val):
        self.val = val
        self.next = None
        self.prev = None

In = InputHelper()
Out = OutputHelper()

######################################

N, K = In.ints()
MOD = 10**9+7

ans = 0
for k in range(K, N+2):
    firstRow = (N-(k-1))+1
    restRows = (k-1)*(firstRow-1)
    
    ans += firstRow+restRows
    ans %= MOD

Out.int(ans)

######################################

if len(sys.argv)>2 and sys.argv[2]=="TIMEIT":
    fin = (time.time()-startTimeProblem)*1000
    print("{:.2f}".format(fin) + "ms")

Submission status: Accepted

Solution code:
import math

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

def combination(n,r):
    return(math.factorial(n) // (math.factorial(n-r) * math.factorial(r)))

for i in range(K,N+2):
    low = (i-1)*i // 2
    high = (N+N-i+1)*i // 2
    ans += (high-low+1)
    ans %= 10**9+7

print(ans)

Submission status: Accepted

Solution code:
import sys
import math
from collections import deque
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def S(): return sys.stdin.readline()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h,w): return [[i for j in range(w)] for k in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**6+10)
input = sys.stdin.readline

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

Submission status: Accepted

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

def ssum(i):
    return i*(i+1)//2

ans = 0
for i in range(k, n+2):
    l,m = ssum(i-1), ssum(n)-ssum(n-i)
    ans += (m - l + 1)
    ans %= mod

print(ans)

Submission status: Accepted

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

MOD = 10**9+7


def sum_range(l, r):
    return (l+r)*(r-l+1)//2


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

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


main()

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
if k>n:
    print(1)
else:
    r=0
    s=0
    sum=0
    for i in range(k):
        r=(r+i)%1000000007
  #      print(r)
        s=(s+n-i)%1000000007
   #     print(s)
    for j in range(k,n+2):
        sum=(sum+s-r+1)%1000000007
 #       print(sum)
        r=(r+j)%1000000007
 #       print(r)
        s=(s+n-j)%1000000007
 #       print(s)
    print(sum)

Submission status: Accepted

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

MOD = 10 ** 9 + 7


def f(l, r):
    return (l + r) * (r - l + 1) // 2


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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
NK = list(map(int, input().split()))
N = NK[0]; K = NK[1]
 
result = 0
for i in range(K,N+2):
    result += i*(N-i+1) +1
    result = result%1000000007
 
print(result)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
def sum_wa(a):
  seki = 1/2 * a * (a+1)
  return int(seki)

def return_wa(n,a):
  g = 1/2 * n * (n+1)
  a = 1/2 * a * (a-1)
  return int(g -a)
  
    
z = 10**9 + 7    
ans = 0    

for i in range(k,n+2):
      seki = return_wa(n,n+1-i)  - sum_wa(i-1) + 1
      seki = seki % z 
      ans += seki
      
print(ans%z)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline
from collections import defaultdict
from heapq import heappop, heappush

from decimal import *

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

##### 割り算の切り上げ #####
# tmp = -(-4 // 3)

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	N,K = inputMap()

	dekki_tmp = [i for i in range(N+1)]
	#print(dekki_tmp)

	dekki_min = [0]
	for i in dekki_tmp:
		dekki_min.append(dekki_min[-1] + i)
	dekki_min = dekki_min[2:]
	#print(dekki_min)

	dekki_tmp = dekki_tmp[::-1]

	dekki_max = [0]
	for i in dekki_tmp:
		dekki_max.append(dekki_max[-1] + i)
	dekki_max = dekki_max[2:]
	#print(dekki_max)

	ans = 0
	if K == 1:
		ans += N
	for i in range(K,N+2):
		mins = dekki_min[i-2]
		maxs = dekki_max[i-2]

		ans = (ans + maxs - mins + 1) % mod
		#print(maxs-mins+1)

	print(ans)

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

Submission status: Accepted

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

mod = 10**9+7
res = 1
l = K*(K-1)//2
r = l+(N-K+1)*K
for i in range(K, N+1):
    res += r-l+1
    res %= mod
    l += i
    r += N-i
print(res)

Submission status: Accepted

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

ans = 0

n = [i for i in range(N+1)]

l_sum = sum(n[:K])
r_sum = sum(n[-K:])

l = K
r = N-K

for i in range(K, N+1):
    ans = (ans + r_sum - l_sum + 1) % MOD
    
    l_sum += n[l]
    r_sum += n[r]
    
    l += 1
    r -= 1
    
print((ans+1)%MOD)

Submission status: Accepted

Solution code:
import itertools

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

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

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

Submission status: Accepted

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

ans = 0

for i in range(n - k + 2):
    ans += (2 * n - k - i + 1) * (k + i) / 2 - (k + i - 1) * (k + i) / 2 + 1
    ans %= 1e9 + 7

print(int(ans))

Submission status: Accepted

Solution code:
n,k=map(int,input().split());print(((n+2*k)*(~n+k)-6)*(k-n-2)//6%(10**9+7))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
print(((n+2*k)*(~n+k)-6)*(k-n-2)//6%(10**9+7))

Submission status: Accepted

Solution code:
import sys
import socket

hostname = socket.gethostname()

if hostname == 'F451C':
    sys.stdin = open('d1.in')


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


def read_str_list():
    return input().split()


def read_int():
    return int(input())


def read_str():
    return input()


def main():
    M = 10 ** 9 + 7
    N, K = read_int_list()
    res = 0
    for i in range(K, N + 2):
      d = (i - 1) * i // 2
      f = i * N - d
      res += f - d + 1
      res = res % M
    print(res)


main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin, stdout
import math
import bisect
import queue

N, K = map(int, stdin.readline().strip().split())

ans = 0

for l in range(K, N + 2):
    fi = ((-1 + l) * l) // 2
    se = ((N + (N - l + 1)) * l) // 2
    ans += se - fi + 1
    ans %= 1000000007
    
stdout.writelines(str(ans) + '\n')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def resolve():
    n, k = map(int, input().split())
    Mod = 1000000007
    ans = 0
    for i in range(n - k + 2):
        j = i + k
        start = j * (j - 1) / 2
        final = (n * 2 - j + 1) * j / 2
        add = final - start + 1
        ans = (ans + add) % Mod
    print(int(ans))

resolve()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
MAX = 10 ** 9 + 7
 
res = 0
 
def calc_sum(l, r):
  return ((l+r) * (r-l+1)) // 2
 
for i in range(K, N+2):
  low = calc_sum(0,i-1)
  high = calc_sum(N-i+1,N)
  res = (res + (high-low+1)) % MAX
 
print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit, stdout
#setrecursionlimit(1000000)
from collections import deque
from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin
def ii(): return int(stdin.readline())
def fi(): return float(stdin.readline())
def mi(): return map(int, stdin.readline().split())
def fmi(): return map(float, stdin.readline().split())
def li(): return list(mi())
def lsi():
    x=list(stdin.readline())
    x.pop()
    return x
def si(): return stdin.readline()
mod=1000000007
#res=['YES', 'NO']
############# CODE STARTS HERE #############
def solve(k):
    l=(k*(k-1))//2
    u=(n*(n+1))//2-((n-k)*(n-k+1))//2
    return u-l+1

n, k=mi()
s=0
while k<=n+1:
    s+=solve(k)
    if s>=mod:
        s%=mod
    k+=1
print(s)

Submission status: Accepted

Solution code:
# 入力は1行で[数値 数値]
def return_number(n, k):
    return n * k - k*k + k + 1


il1 = input().split()
N = int(il1[0])
K = int(il1[1])

answer = 0
mod = 10**9 +7

for k in range(K, N+2):
    num  = return_number(N, k)
    answer = answer + num

print(answer%mod)

Submission status: Accepted

Solution code:
# coding: utf-8

# 14:35-14:48


def main():
    N, K = map(int, input().split())
    q = 10**9 + 7

    ans = 0
    for n in range(K, N+2):
        ans += n*(N-n+1) + 1
    
    return ans % q


print(main())

Submission status: Accepted

Solution code:
from itertools import permutations, combinations

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

ans = 0
_list = list(range(N+1))
_list.sort(reverse = True)
h = 0
l = 0
cnt = -1

for i in range(N+1):
    h += _list[i]
    cnt += 1
    l += cnt
    if i >= K-1:
        ans += h - l + 1
        
print(ans%(10**9 + 7))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
 
s = 0
 
for i in range(K,N+2):
  s += i*N +1 - i*(i -1)

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

Submission status: Accepted

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

Submission status: Accepted

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

Narr = []
for i in range(0, N + 1):
    Narr.append(i)
S = []
S.append(0)
for i in range(1, N + 2):
    S.insert(i, Narr[i - 1] + S[i - 1])

sum = 0
mod = pow(10, 9) + 7
last = len(S) - 1
for i in range(K, N + 2):
    sum += S[last] - S[last - i] - S[i] + 1

print(sum % mod)

Submission status: Accepted

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

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
print(s%(10**9+7))

Submission status: Accepted

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

#末尾の数字の和は
#20000100000
#となる。明らかに10**100は超えない。
#頭の数は何個足し合わせたかで決まる
#違いが出るのは末尾の数

#https://drken1215.hatenablog.com/entry/2020/04/20/003900

#最小値から最大値まで連続した数を作れる

ans = 0

for i in range(K,N+2):
    comb_min = (0+i-1)*i//2 #sum(all_num[0:i])
    comb_max = (N+N-i+1)*i//2 #sum(all_num_inv[0:i])
    #print(i,comb_min,comb_max)
    comb_num = comb_max - comb_min + 1
    ans += comb_num
    ans %= MOD_BY
    
print(ans)

Submission status: Accepted

Solution code:
P = 10 ** 9 + 7
N, K = map(int, input().split())
L = N + 1  # 数列の長さ

cusum = [0] * (L + 1)

for i in range(L):
  cusum[i + 1] = cusum[i] + (i + 1)
  
total = 0

for k in range(K, L + 1):
  total += (cusum[L] - cusum[L - k]) - cusum[k] + 1

print(total % P)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
left=right=ways=0
for i in range(n+1):
  left += i
  right += n-i
  if i >= k -1:
    ways += max(right -left +1, 0)
print(int(ways%(1e9+7)))

Submission status: Accepted

Solution code:
def f(n,k):
  return (n+1)*(n+k+1)*(n-k+2)//2+k*(k-1)*(2*k-1)//6-(n+1)*(n+2)*(2*n+3)//6+n-k+2

n,k=map(int,input().split())
print(f(n,k)%1000000007)

Submission status: Accepted

Solution code:
def main():
    ans = 0
    n, k = map(int, input().split())
    for i in range(k, n + 2):
        mi = i * (i - 1) // 2 #和
        ma = n * (n + 1) // 2 - (n - i) * (n - i + 1) // 2
        ans += ma - mi + 1
    print(ans % (10 ** 9 + 7))
if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
cnt = []
minimum = sum([i for i in range(K-1)])
maximum = sum([i for i in range(N-(K-2), N+1)])

for n in range(K-1, N+1):
    minimum += n
    maximum += (N-n)
    diff = maximum - minimum +1
    cnt.append(diff)
ans = sum(cnt) % (10**9+7)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def sum(l, r):
  return (l+r)*(r-l+1)//2

def main():
  N, K = map(int, input().split())
  MOD = 1000000007

  ans = 0
  for i in range(K,N+2):
    min_n = sum(0, i-1)
    max_n = sum(N+1-i, N)
    ans += (max_n-min_n+1)
  print(ans%MOD)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

def main():
  t=0
  for k in range(K,N+2):
    s=k*~-k//2
    m=k*(2*N-k+1)//2
    t=(t+m-s+1)%MOD

  print(t%MOD)

main()

Submission status: Accepted

Solution code:
import sys 
import math
try:
	sys.stdin = open('input.txt', 'r') 
	sys.stdout = open('output.txt', 'w')
except:
	pass

input = sys.stdin.readline
for tt in range(1):
	n,k= map(int,input().split())
	n+=1; ans = 0
	for i in range(k,n+1):
		minSum = (i*(i-1))//2
		maxSum = (n*(n-1))//2 - ((n-i)*(n-i-1))//2
		ans += (maxSum - minSum + 1)%(10**9+7)
	print(ans%(10**9+7))

Submission status: Accepted

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

M=N+1

A=-M*(M+1)*(2*M+1)//6+(N+1)*M*(M+1)//2+M

M=K-1

B=-M*(M+1)*(2*M+1)//6+(N+1)*M*(M+1)//2+M


print((A-B)%(10**9+7))

Submission status: Accepted

Solution code:
n, k=map(int, input().split())
mod=10**9 +7
def calc(num):
    small=num*(num-1)//2
    large=num*(2*n -num+1)//2
    return large-small+1

ans=0
for i in range(k, n+2):
    ans+=calc(i)
    ans%=mod

print(ans)

Submission status: Accepted

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

sum_left = list(range(N+1))
sum_right = list(range(N+1))[::-1]

for i in range(1, N+1):
    sum_left[i] += sum_left[i-1]

for i in range(1, N+1):
    sum_right[i] += sum_right[i-1]

ans = 0
for i in range(K-1, N+1):
    ans += sum_right[i] - sum_left[i] + 1
    ans %= mod

print(ans)

Submission status: Accepted

Solution code:
def sigma(x, d):
    if d == 1:
        return x * (x + 1) // 2
    if d == 2:
        return x * (x + 1) * (2 * x + 1) // 6


def calc(x):
    return -sigma(x, 2) + (n + 1) * sigma(x, 1) + x


# N+1個の数からK個の数を選ぶとき、その和としてあり得る数は-K^2+(N+1)K+1個

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

print((calc(n + 1) - calc(k - 1)) % p)

Submission status: Accepted

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


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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
mod = 10**9+7

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

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
if n < k:
    print(1)
    exit(0)

MOD = pow(10, 9) + 7
count = 0
while n+1 >= k:
    minv = k * (k-1) // 2
    maxv = ((n+1) * n // 2) - ((n-k) * (n-k+1) // 2)

    count += (maxv - minv + 1)
    count %= MOD
    k += 1

print(count)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n,k=map(int,input().split())
INF=10**9+7
ans=0
for i in range(k,n+2):
    a=i*(i-1)//2
    b=(2*n-i+1)*i//2
    temp=b-a+1
    ans=(ans+temp)%INF
print(ans)

Submission status: Accepted

Solution code:
mod = 10**9+7

def min_sum(k):
  return int(k*(k-1)/2)

def max_sum(n,k):
  return int((n+n-k+1)*k/2)

def main():
  N,K = [int(x) for x in input().split()]
  ans = 0
  for k in range(K,N+2):
    ans += max_sum(N,k) - min_sum(k) + 1

  print(ans%mod)

if __name__ == "__main__":
    main()
    pass

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
print(((n+2-k)*(k*n-k*k+k)+(n+2-k)*(n+1-k)*(n+1-2*k)//2-(n-k+1)*(n-k+2)*(2*n-2*k+3)//6+(n+2-k))%1000000007)

Submission status: Accepted

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

sum_N = N*(N+1)//2
ans   = 1
ue    = sum_N
shita = sum_N

for i in range(N-K+1):
    ue    -= i
    shita -= (N-i)
    ans   += (ue-shita+1)

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

Submission status: Accepted

Solution code:
n,k = [int(e) for e in input().split()]

count = 0

for i in range(k,n+2):
    d = (n+n-i+1)*i/2 - (i-1)*i/2 +1
    count += d


mod = 10**9+7
print(int(count%mod))

Submission status: Accepted

Solution code:
from collections import  deque
import sys
import bisect
import math
import itertools
from queue import deque
from math import gcd

INF = float('inf')
mod = 10**9+7
eps = 10**-7

def inp(): return int(sys.stdin.readline())
 
def inp_list(): return list(map(int, sys.stdin.readline().split()))

def lcm(x, y): return (x * y) // gcd(x, y)

N, K = inp_list()
ans = 0
for k in range(K, N + 2):
    # M => 和の最大値, m => 和の最小値, k について和としてありえるものの個数は M - m + 1
    M = ( N * (N + 1) // 2 - (N - k) * (N + 1 - k) // 2 )
    m = k * (k - 1) // 2
    ans += (M - m + 1)

print(ans % mod)

Submission status: Accepted

Solution code:
from collections import  deque
import sys
import bisect
import math
import itertools
from queue import deque
from math import gcd

INF = float('inf')
mod = 10**9+7
eps = 10**-7

def inp(): return int(sys.stdin.readline())
 
def inp_list(): return list(map(int, sys.stdin.readline().split()))

def lcm(x, y): return (x * y) // gcd(x, y)

N, K = inp_list()
ans = 0
for k in range(K, N + 2):
    # M => 和の最大値, m => 和の最小値, k について和としてありえるものの個数は M - m + 1
    M = ( N * (N + 1) // 2 - (N - k) * (N + 1 - k) // 2 ) % mod
    m = k * (k - 1) // 2 % mod
    ans += (M - m + 1) % mod

print(ans % mod)

Submission status: Accepted

Solution code:
from collections import  deque
import sys
import bisect
import math
import itertools
from queue import deque
from math import gcd

INF = float('inf')
mod = 10**9+7
eps = 10**-7

def inp(): return int(sys.stdin.readline())
 
def inp_list(): return list(map(int, sys.stdin.readline().split()))

def lcm(x, y): return (x * y) // gcd(x, y)

N, K = inp_list()
ans = 0
for k in range(K, N + 2):
    # M => 和の最大値, m => 和の最小値, k について和としてありえるものの個数は M - m + 1
    M = ( N * (N + 1) - (N - k) * (N + 1 - k) ) // 2
    m = k * (k - 1) // 2
    ans += (M - m + 1) % mod

print(ans % mod)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=0
sum_i=sum(range(0,k))
m=1000000007
for i in range(k,n+2):
  a=(a+i*n+-2*sum_i+1)%m
  sum_i+=i
print(a)

Submission status: Accepted

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

ans = 0

for i in range(k, n + 2):
    ans += i*(n-i+1)+1
ln = 10 ** 9 + 7
print(ans%ln)