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:
# -*- coding: utf-8 -*-

N,K = list(map(int, input().rstrip().split()))
#-----

def check(n,k):
    sum_min = (k-1)*k/2
    sum_max = n*(n+1)/2 - (n-k)*(n-k+1)/2
    
    sum_pattern = sum_max - sum_min + 1
    return sum_pattern


cnt = 0
for i in range(K,N+1):
    c = check(N,i)
    cnt = (cnt + c) % (10**9+7)
    
print(int(cnt+1))

Submission status: Accepted

Solution code:

def solve():
    # mini = (k - 1) * k // 2
    # maxi = k * (n - k + 1) + mini
    total_comb = 0
    for i in range(k, n+2):
        total_comb = (total_comb + i * (n - i + 1) + 1) % (10**9 + 7)
    print(total_comb)
    return



n, k = [int(i) for i in input().split(" ")]
solve()

Submission status: Accepted

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

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

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+=(n*i - i*(i-1)//2) - i*(i-1)//2 +1
    ans%=mod
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
@author: H_Hoshigi
"""
def f(N, k):
    return  -(k*(k+1)*(2*k+1))//6 + (N+1) * (k*(k+1))//2 + k
def main():
    N, K = map(int, input().split())
    #counter = 0
    #for k in range(K, N+2):
    #    print(k * (N - k +1) + 1)
    #    counter += (k * (N - k +1) + 1)
    counter = f(N, N+1) - f(N, K-1)
    print(counter%(1000000007))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

def cumsum(s):
    n = len(s)
    cs = [0] * (n+1)
    for i in range(n):
        cs[i+1] = cs[i] + s[i]
    return cs

s = list(range(n))
cs = cumsum(s)
ans = 0

for i in range(k, n+1):
    ans += (cs[n] - cs[n-i]) - cs[i] + 1
    ans %= MOD

print(ans)

Submission status: Accepted

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


if k==n+1:
  print(1)
  exit(0)

l=[]
temp=0
for i in range(n+1):
  temp+=i
  l.append(temp)

count=0
for i in range(k,n+2):
  if n-i<0:
    we=l[-1]-l[i-1]+1
  else:
    we=l[-1]-l[i-1]-l[n-i]+1
  count+=we
  
print(count%(10**9+7))

Submission status: Accepted

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

count = 0

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

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
l = []
cmin = 0
cmax = 0
for i in range(K, N+2):
  cmin = 0
  cmax = 0
  cmin = i * (i-1) / 2
  cmax = (2*N - i + 1) * i / 2
  dif = cmax - cmin + 1
  l.append(dif)
print(int(sum(l)%(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):
#  print(i,(i-1)*i//2, (n+1)*n//2)
  sigma1=(i-1)*i//2
  sigma2=(n-i)*(n-i+1)//2
  sigma3=n*(n+1)//2
#  print(i,sigma1,sigma3-sigma2)

  ans+=sigma3-sigma2-sigma1+1
  ans%=mod
print(ans)

Submission status: Accepted

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

tmp = sum(range(k-1))

ans = 0
for x in range(k, n+2):
  ans += x*n + 1
  if x >= 2:
      tmp += x-1
      ans -= 2*tmp
  ans %= mod

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

r = 1

for i in range(K, N+1):
	up = S[N]-S[N-i]
	low = S[i-1]
	r = (r+(up-low+1))%mod

print(r)

Submission status: Accepted

Solution code:
from math import factorial

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

ans=0
i=K
while i<=N+1:

    mni=i*(i+1)//2
    mxi=i*(2*N-i+3)//2
    ans+=mxi-mni+1
    i+=1

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

Submission status: Accepted

Solution code:
n , k = input().split()
n = int(n)
k = int(k)
lmin = list(range(0, k - 1))
lmax = list(range(n - k + 2, n + 1))
min = sum(lmin)
max = sum(lmax)
#print(lmin, min, lmax, max)
ans = 0
for i in range(k, n + 2):
    min += i - 1
    max += n - i + 1
    ans += max - min + 1
    #print(i, min, max, ans)

print(int(ans % (1e9 + 7)))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
R = range(K, N + 2)
A = []
for i in R:
    s_max = (N + (N - i + 1)) * i / 2
    s_min = (i - 1) * i / 2
    n = s_max - s_min + 1
    A.append(n)
S = int(sum(A))
print(S % (10 ** 9 + 7))

Submission status: Accepted

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

#output
mod = pow(10, 9) + 7
answer = 0
for i in range(K, N+2):
    answer += ((N*(N+1)//2)  - ((N-i)*(N-i+1)//2) - (i*(i-1)//2) + 1) % mod

print(answer % mod)

Submission status: Accepted

Solution code:
from itertools import accumulate
import sys

mod = 10 ** 9 + 7

N, K = map(int, sys.stdin.readline().rstrip().split())
if K == N + 1:
    print(1)
    sys.exit()

l = list(accumulate(range(N + 1)))

ans = 0
for k in range(K, N + 2):
    if k == N + 1:
        ans += 1
    else:
        ans += k * N - (2 * l[k-1]) + 1

if ans >= mod:
    print(ans % mod)
else:
    print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
s = [0] * (N + 2)
for i in range(1, N + 1):
    s[i + 1] = s[i] + i
#s
s_rev = [0] * (N + 2)
for i in range(N, -1, -1):
    s_rev[N - i + 1] = s_rev[N - i] + i
#s_rev
ans = 0
mod = 10 ** 9 + 7
for i in range(K, N + 2):
    #print(s_rev[i] - (s[i] - 1))
    ans += (s_rev[i] - (s[i] - 1)) % 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+1)+1

print(ans%1000000007)

Submission status: Accepted

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

Submission status: Accepted

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

c = []
for i in range(K, N+2, 1):
  minimum = 1 / 2 * (i - 1) * i
  maximum = 1 / 2 * N * (N + 1) - 1 / 2 * (N - i) * (N - i + 1)
  c.append(int(maximum - minimum + 1))
else:
  answer = 0
  for i in c:
  	answer += i
  else:
  	print(answer % (10 ** 9 + 7))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
p = 1000000007
N,K = map(int,input().split())
su = 0
if N < K:
    print(1)
    sys.exit()
for i in range(K,N+2):
    su += (i + (N-i)*i+1)
  
  
print(su%p)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
mod=pow(10,9)+7
numbers=[a for a in range(0,N+1)]
z=sorted(numbers,reverse=True)
NN=[]
b=0
for a in numbers:
    b+=a
    NN.append(b)
c=0
SS=[]
for a in z:
    c+=a
    SS.append(c)
if N+1==K:
    print(1)
else:
    answer=1
    for a in range(K,N+1):
        kotae=SS[a-1]-NN[a-1]+1
        answer+=kotae
        answer=answer%mod
    print(answer%mod)

Submission status: Accepted

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

mod = 10**9+7

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 = (res+ma-mi+1)%mod

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
mod = 10 ** 9 + 7
premin = 0
premax = 0
ans = 0
list = [i for i in range(n + 1)]
for x in range(n + 1):
    premin += list[x]
    premax += list[n - x]
    if x < k - 1: continue
    ans += (premax - premin + 1)
    if ans >= mod: ans %= mod
print(ans)

Submission status: Accepted

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

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*(i-1)//2
    ma=n*(n+1)//2-(n-i)*(n-i+1)//2
    ans+=ma-mi+1
print(ans%(10**9+7))

Submission status: Accepted

Solution code:
def nA(n, k):
    minimum = n*(n-1)//2
    maximum = n*k - minimum
    res = maximum-minimum+1

    return n*k - n*(n-1) + 1
    
    


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


res = 0
for i in range(K, N+2):
    res += nA(i, N)

res = res % 1000000007

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

num=0
for n in range(K,N+2):
    MIN=(n-1)*n//2
    MAX=(N+(N-n+1))*n//2
    num+=MAX-MIN+1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    mod = 10**9 + 7
    N, K = map(int, input().split())
    ans = 0
    for i in range(K, N+2):
        max = (N * i - 2 * (((i-1)*i)//2))%mod
#         min = (((i-1)*i)//2)%mod
        ans += max + 1
    print(ans%mod)
    
        
    
    
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
N,K = map(int,input().split(' '))
u = 10**9+7

M = (N+1)*N//2

LIMIT = 0
ANS = 0

#i個の要素を使って成立させられる数値が幾つあるのか考える
#LINITは, i個で作れる最大の数
for i in range(K,N+2):
    m_LIMIT = (0+(i-1))*i//2
    M_LIMIT = (N+(N-(i-1)))*i//2
    ANS += (M_LIMIT-m_LIMIT)+1
    ANS %= u
print(ANS)

Submission status: Accepted

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

def sm(mn :int, mx :int) -> int:
    n = mx - (mn - 1)
    return int((mx + mn) * (n / 2))

cnt = 0
for i in range(k, n+2):
    mn, mx = sm(0, i-1), sm(n-(i-1), n)
    cnt += mx - (mn-1)
    cnt = cnt % BASE
print(cnt)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
mod = 10 ** 9 + 7
ans = [None] * (n - k + 2)
ans[0] = ((n - k + 1 + n) * k // 2) - ((0 + k - 1) * k // 2) + 1

for i in range(1, n - k+2):
    ans[i] = (ans[i-1] + n - 2 * (k + i - 1)) % mod

print(sum(ans) % mod)

Submission status: Accepted

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

DP=[0]*(n+2)
DP[0]=1
point=n
n=n+1

for i in range(1,n+1):
    DP[i]=DP[i-1]+point
    point -=2
print(sum(DP[k:])%mod)

Submission status: Accepted

Solution code:

mod = pow(10, 9)+7
N, K = map(int, input().split())
sum = 0
for i in range(K, N+2):
    sum = sum+i*(2*N+1-i)/2.0 - 1/2.0*i*(i-1)+1
    sum = sum % mod

print(int(sum))

Submission status: Accepted

Solution code:
import itertools

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

MOD = 1000000007

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

print(int(ans)%MOD)

Submission status: Accepted

Solution code:
N, K = map(int,input().split())
MOD = 10**9 + 7
ans = 0
for k in range(K,N+2):
    t = N*(N+1)//2 - (N-k)*(N-k+1)//2
    s = k*(k-1)//2
    ans += t-s+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+=(n+1-i)*i+1

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

Submission status: Accepted

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

M = 10**9 + 7

cnt = 1

a = 0
b = N
c = 0

for i in range(N - K + 1):
    cnt += b - a + 1
    c += 1
    a += c
    b += N - c


print(cnt % M)

Submission status: Accepted

Solution code:
MOD = 10**9 + 7
N, K = [int(s) for s in input().split()]
cnt = 0
for i in range(K, N + 2):
    if i != N + 1:
        cnt += i * ((N + 1) - i) + 1
    else:
        cnt += 1
    cnt %= MOD

print(cnt)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

from pprint import pprint
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.buffer.readline

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

# x (= 10 ** 100) の係数がaのとき、和としてあり得るものの個数は何個あるかを考える
# K <= a <= N + 1

b_list = range(N + 1)

ans = 0
for a in range(K, N + 2):
    # print(f"a = {a}")
    b_min = a * (a - 1) / 2
    b_max = a / 2 * (2 * (N - (a - 1)) + a - 1)
    # print(f"b_min = {b_min}, b_max = {b_max}")
    ans += b_max - b_min + 1

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

Submission status: Accepted

Solution code:
import numpy as np

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

for i in range(k,n+2):
  #print(i,i*n-i*(i-1)+1)
  sum=sum+i*n-i*(i-1)+1
  
#print(int(sum))
print(int(int(sum)%(1e9+7)))

Submission status: Accepted

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

ans = 0
if(K <= N+1):
 for i in range(K, N+1 +1):
  tmp = i*(N-i+1)+1 % (10**9 + 7)
  ans += tmp
  ans = ans % (10**9 + 7)  
    
print(ans)

Submission status: Accepted

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

lst = [i for i in range(N+1)]
lst1 = lst[::-1]

ans = 0
mod = 10**9+7
tmp = sum(lst[:K-1])
tmp1 = sum(lst1[:K-1])

for i in range(K-1, N+1):
    tmp += lst[i]
    tmp1 += lst1[i]
    ans += (tmp1-tmp+1)
print(ans%mod)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from scipy.special import comb

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

'''
n += 1
an = comb(n, k, exact=True)
ans = an
for i in range(k,n):
    an=an*(n-i)//(i+1)
    ans += an
    ans %= (10**9+7)

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

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

Submission status: Accepted

Solution code:
from math import factorial

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

N += 1

ans = 0
head = sum(range(K-1))
tail = sum(range(N-K+1,N))

for i in range(K,N+1):
    head+=i-1
    tail+=N-i
    ans += tail - head +1

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
s_sum = [0]
g_sum = [N]
mod = 10**9 + 7
for i in range(N):
    s_sum.append(s_sum[i] + (i+1))
    g_sum.append(g_sum[i] + (N-(i+1)))

# print(s_sum)
# print(g_sum)
ans = 0
for i in range(K, N+2):
    ans += g_sum[i-1] - s_sum[i-1] + 1
    ans %= mod

print(ans%mod)

Submission status: Accepted

Solution code:
import math
mod = 10**9 +7
def cmb(n, r):
    if n < r:
        return 0
    else:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

N,K = map(int,input().split())
f = cmb(N+1,K)
mi = K*(K-1)//2
ma = K*(2*N-K+1)//2
s = 0
while N -K +2> 0:
    mi = K*(K-1)//2
    ma = K*(2*N-K+1)//2
    s += ma - mi +1
    K +=1
print(s%mod)

Submission status: Accepted

Solution code:
N,K = map(int, input().split())
count = 0
for k in range(K,N+2):
    low = (k-1)*k/2
    high = (N+N-k+1)*k/2
    count += (high-low+1)
print(int(count)%(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+1):
    cnt = (2*n-i+1)*i // 2 - (i-1)*i//2 + 1
    ans = ans + cnt % MOD
print(ans % MOD + 1)

Submission status: Accepted

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

mod = 10 ** 9 + 7
ans = 0

for i in range(k, n + 2):
    n_min = (i * (i - 1) // 2) % mod

    first = (n * (n + 1) // 2) % mod
    second = ((n - i) * (n - i + 1) // 2) % mod
    n_max = (first - second) % mod

    add = (n_max - n_min + 1) % mod
    ans = (ans + add) % mod

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
s=0
while K<=N+1:
    s+=(K*(N-K+1)+1)%(10**9+7)
    s%=(10**9+7)
    K+=1
print(s)

Submission status: Accepted

Solution code:
n,k = (int(x) for x in input().split())
M = 10**9+7
s = 0

for i in range(n+2-k):
    ad = 1 + (n+1-k-i)*(k+i)
    s += ad
#if s >= M:
#    s -= M
        
print(s%M)

Submission status: Accepted

Solution code:
import sys
mod = pow(10, 9) + 7
sys.setrecursionlimit(pow(10, 8))

def power(x, y):
    if   y == 0: return 1
    elif y == 1	 : return x % mod
    elif y % 2 == 0 : return power(x, y//2)**2 % mod
    else: return power(x, (y-1)//2)**2 * x % mod
    
def mul(a, b):
    return ((a % mod) * (b % mod)) % mod

def div(a, b):
    return mul(a, power(b, mod-2))
def div2(a, b):
    return mul(a, modinv(b))

def modinv(a):
    b, u, v = mod, 1, 0
    while b:
        t = a//b
        a, u = a-t*b, u-t*v
        a, b, u, v = b, a, v, u
    u %= mod
    return u

def cmb(n, r):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r]

NNN = (10**6)
g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]

for i in range( 2, NNN + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )
N, K = map(int, input().split())

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

Submission status: Accepted

Solution code:
def sum_n(n,M):
    return n*(n+1)//2 % M

def cmb(n,k,M):
    max_sum,min_sum = sum_n(n,M)-sum_n(n-k,M),sum_n(k-1,M)
    return (max_sum - min_sum + 1) % M

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

ans = 0
for k in range(K,N+2):
    ans = (ans + cmb(N,k,M)) % M

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys

MOD = 1000000007  # type: int


def solve(N: int, K: int):
    ans = 0
    for p in range(K,N+2):
        ans += p*(N-p+1) + 1
        ans %= MOD
    print(ans)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    solve(N, K)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 0
Ns = list(range(N+1))
cumsum = [0]
for i in range(N+1):
    cumsum.append(cumsum[i] + Ns[i])

MOD = 10**9 + 7

for i in range(K, N+2): #k-->N+1
    tmp = cumsum[-1] - cumsum[-i-1] - cumsum[i] + cumsum[0] + 1
    ans += tmp
    ans %= MOD

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
for i in range(k, n+2):
  ans += n*i -i**2+i +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):
  ans += (n-i+1)*i+1
  ans %= mod
  
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
ans = 0
s0 = 0
s = 0

for i in range(k):
    s0 += i
    s += n
    n -= 1
while s - s0 + 1 >= 0:
    ans += s - s0 + 1
    s += n
    n -= 1
    s0 += k
    k += 1
print(ans % (10**9 + 7))

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
MOD = 10**9+7
ans = 0
def wa(s,e):
  return int((0.5)*e*(e+1) - (0.5)*(e-s)*(e-s+1))
for i in range(k,n+1):
  ans += (wa(i,n) - wa(i,i-1) + 1)%MOD
ans += 1
print(ans%MOD)

Submission status: Accepted

Solution code:
MOD = 1000000007
NCK_MAX = 510000

fac = [0 for _ in range(NCK_MAX)]; finv = [0 for _ in range(NCK_MAX)]
inv = [0 for _ in range(NCK_MAX)]; facn = [0 for _ in range(NCK_MAX)]

def myPow(x,n,MOD=MOD):
    if n == 0:
        return 1
    elif n % 2 == 0:
        return myPow(x*x%MOD,n//2)
    else:
        return x*myPow(x,n-1)%MOD

def COMinit(n):
    fac[0] = 1; fac[1] = 1
    facn[1] = n
    finv[0] = 1; finv[1]=1
    inv[1]=1
    for i in range(2,NCK_MAX):
        fac[i] = fac[i-1]*i%MOD
        facn[i] = facn[i-1]*(n-i+1)%MOD
        inv[i] = MOD-inv[MOD%i]*(MOD//i)%MOD
        finv[i] = finv[i-1]*inv[i]%MOD

def modinv(a,MOD=MOD):
    return myPow(a,MOD-2)

def COM(n,k):
    if n<k:
         return 0
    elif n<0 and k<0:
         return 0
    return facn[k] * finv[k]%MOD

def shokou(i):
    return (i*(i-1))//2
def makkou(i,n):
    return ((2*n+1-i)*i)//2

n,k = map(int, input().split())
n = n
# COMinit(n)
ans = 0
li = list(range(n))
for i in range(k,n+2):
    ans = (ans + makkou(i,n)-shokou(i)+1+MOD)%MOD
    # print(ans)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
mods=10**9+7

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

cnt = 0
for k in range(K,N+1+1):
    minsum = k*(k+1)/2 -k
    maxsum = k*(k+1)/2 + (N-k)*(k)

    cnt+=(maxsum-minsum+1)
    cnt=cnt%mods

print(int(cnt))

Submission status: Accepted

Solution code:
import math
N, K = list(map(int, input().split(' ')))
ret = 0
c = 0
kmax = N*(N+1)//2
for k in range(N+1, K-1, -1):
    kmax = kmax - c
    kmin = (k-1)*k // 2
    ret += kmax - kmin + 1
    if k <= N:
        c += 1
modq = int(1e9) + 7
print(ret % modq)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    a,b= map(int,input().split())
    maxnum =0
    minnum =0
    diff = 10**9 +7
    ans = 0

    for i in range(b,a+2):
        max = (i * (2*a+1-i)) / 2
        min = (i* (i - 1) / 2)
        ans += max-min+1
        ans = ans % diff
    print(int(ans))

Submission status: Accepted

Solution code:
import numpy as np

n, k = map(int, input().split())
ans = 0
mod = 10 ** 9 + 7
arr = np.arange(n+1)
a = np.cumsum(arr)
b = np.cumsum(arr[::-1])

for i in range(k, n+1+1):
    ans += b[i-1] - a[i-1] + 1
print(ans%mod)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
mod=pow(10,9)+7
n,k=map(int,input().split())
mini,maxi=-1,-1
ranges=[[0,0]]
ans=0
for i in range(k,n+2):
    mini=(i*(i-1))//2
    maxi=((i)*(n+n-i+1))//2
    ans += (maxi-mini+1)%mod
    ans=ans%mod

print(ans)

Submission status: Accepted

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

for i in range(K):
    mi += i
for i in range(N, N-K, -1):
    ma += i
#print(mi, ma)
N2 = K
N3 = N-K
#print(count, mi, ma, N2, N3)
for i in range(1, N-K+2):
    count += ma - mi +1
    count %= mod
    mi +=N2
    N2+=1
    ma +=N3
    N3-=1
    #print(count, mi, ma, N2, N3)
count += 1
print(count%mod)

Submission status: Accepted

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

# n=int(input())
# A=list(map(int,input().split()))
mod=1000000007
n,k=map(int,input().split())
n+=1
ans=0
l=0
r=0
for i in range(k):
    l+=i
posl=k-1
for i in range(n-k,n):
    r+=i
posr=n-k


while l!=r:
    ans+=r-l+1
    ans%=mod
    posl+=1
    l+=posl
    posr-=1
    r+=posr
print(ans+1)

Submission status: Accepted

Solution code:
def slove(n, k):
    iter1 = iter(range(n+1))
    iter2 = iter(range(n, -1, -1))

    lower_num = 0
    upper_num = 0
    for _ in range(k):
        lower_num += iter1.__next__()
        upper_num += iter2.__next__()

    ans = len(range(lower_num, upper_num+1))

    for i in range(k, n+1):
        lower_num += iter1.__next__()
        upper_num += iter2.__next__()
        ans += len(range(lower_num, upper_num+1))

    return ans % (10**9 + 7)

if __name__ == "__main__":
    n, k  = [int(i) for i in input().split()]
    print(slove(n, k))

Submission status: Accepted

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

sum_min = 0
sum_max = 0

for i in range(1,k):
    if i != 0:   
        sum_min += i-1
    sum_max += n -i + 1
  #  print(i,sum_max - sum_min + 1,sum_min,sum_max)
for i in range(k,n+2):
    if i != 0:   
        sum_min += i-1
    sum_max += n -i+1
    ans += sum_max - sum_min + 1
 #   print(i,sum_max - sum_min + 1,sum_min,sum_max)
print(ans%(10**9+7))

Submission status: Accepted

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

r = 0
for k in range(K,N+2):
    r += k*(N-k+1)+1

print(r%MOD)

Submission status: Accepted

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

m=n+1-k
if m==0:
  print(1)
else:
  ans=1 + m*(6+(m+1)*(3*n-2*m+2))//6
  print(ans%(10**9+7))

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep="\n")
printH = lambda x: print(" ".join(map(str,x)))
def IS(): return sys.stdin.readline()[:-1]
def II(): 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 LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
mo = 10**9+7

def main():
	n,k = MI()
	a=(10**100) % mo
	dp = [0]*(n+1)
	ans = 0
	for kk in range(k,n+2):
		tmp = (n  - kk + 1)
		ans += (tmp+1) + tmp * (kk-1)
		ans %= mo
	print(ans % mo)

if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
num  = 0
for select_num in range(k,n+2):
    st = (select_num*(2*0+(select_num - 1)*( 1)))/2
    ed = (select_num*(2*n+(select_num - 1)*(-1)))/2
    sum = ed - st + 1
    num += sum
print(int(num)%1000000007)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import accumulate

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

count = 0
a = [x for x in range(n+1)]
MOD = 10 ** 9 + 7

if k > n:
    print(1)
else:
    prefix_sum = list(accumulate(a))
    suffix_sum = list(accumulate(a[::-1]))

    res = [a - b + 1 for a, b in zip(suffix_sum, prefix_sum)]

    #print(prefix_sum)
    #print(suffix_sum)
    print(sum(res[k-1:]) % MOD)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

ans=0
i=K

while i<=N+1:
      min=i*(i-1)/2
      max=(N+1)*N/2-(N-i+1)*(N-i)/2
      ans+=((max-min)+1)
      i+=1

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

Submission status: Accepted

Solution code:
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import permutations,combinations
from collections import defaultdict,Counter
# from math import gcd,ceil,floor,factorial
# from fractions import gcd
from functools import reduce
from pprint import pprint

def myinput():
    return map(int,input().split())

def mylistinput(n):
    return [ list(myinput()) for _ in range(n) ]

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col):
    data.sort(key=lambda x:x[col],reverse=False)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

n,k = myinput()

mod = 10**9 + 7

ls = list(range(n+1))
ls2 = ls[::-1]
# print(ls2)

e = sum(ls2[:k-1])
# print(e)

count = 0
for i in range(k,n+2):
    # print("i={}".format(i))
    s = (i-1)*i//2
    e += ls2[i-1]
    count += e - s + 1
    # print(s,e,count)
print(count%mod)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
a = []
a.append(n+1)
for i in range(n):
  t = a[i] + n - 2*i - 2
  a.append(t)
  
total = 0
for i in range(k-1,n+1):
  total += a[i]
  total = total % 1000000007

print(total)

Submission status: Accepted

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

mod = 10 ** 9 + 7
start = 0
end = N
k = 1
ans = 0
while k <= N + 1:
    if k >= K:
        ans += end - start + 1
        ans %= mod
    start = start + k
    end = end + N - k
    k += 1

print(ans)