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:
class Mint:
    
    MOD = 10**9+7
    
    def __init__(self, x=0):
        self.x = (x%self.MOD + self.MOD) % self.MOD
    
    def __iadd__(self, other):
        self.x += other.x
        if self.x >= self.MOD:
            self.x -= self.MOD
        return self
    
    def __isub__(self, other):
        self.x += self.MOD - other.x
        if (self.x >= self.MOD):
            self.x -= self.MOD
        return self
    
    def __imul__(self, other):
        self.x *= other.x
        self.x %= self.MOD;
        return self;
    
    def __add__(self, other):
        ans = Mint(self.x)
        ans += other
        return ans
        
    def __sub__(self, other):
        ans = Mint(self.x)
        ans -= other
        return ans
    
    def __mul__(self, other):
        ans = Mint(self.x)
        ans *= other
        return ans
    
    def __pow__(self, n):
        if n == 0:
            return Mint(1)
        a = self.__pow__(n >> 1)
        a *= a
        if n&1:
            a *= self
        return a
    
    def inv(self):
        return self**(self.MOD-2)
    
    def __ifloordiv__(self, other):
        self *= other.inv()
        return self
    
    def __floordiv__(self, other):
        ans = Mint(self.x)
        ans //= other
        return ans
        

    
def Main(N, K):
    ans = Mint(0)
    for i in range(K, N+2):
#         low = ((Mint(0)+Mint(i-1))*Mint(i))//Mint(2)
#         high =((Mint(N)+Mint(N-i+1))*Mint(i))//Mint(2)
        ans += Mint(i)*Mint(N-i+1)+Mint(1)
    return ans.x

def main():
    N, K = map(int, input().split())
    print(Main(N, K))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    s = input().rstrip().split(' ')
    N,K = int(s[0]), int(s[1])

    ret = 0

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

        ret += t-f+1

    print(int(ret) % 1000000007)

Submission status: Accepted

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

list=[]
a=1
for i in range(N+2):
    if i==0 or i==N+1:
        list.append(1)
    else:
        list.append(N*i+a)
        a-=2*i

ans=0
for i in range(M,N+2):
    ans+=list[i]

print(ans%1000000007)

Submission status: Accepted

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

A = [0] * N
ruiseki = [0] * N
for i in range(1, N):
    ruiseki[i] = ruiseki[i-1] + i

total = 0
for i in range(K, N):
    start = ruiseki[i-1]
    end = ruiseki[N-1] - ruiseki[N-i-1]
    total += end - start + 1

# all select
total += 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a,b=(int(x) for x in input().split())
z=0
for i in range(b,a+2):
  z=(z+(a-i+1)*i+1)%int(1e9+7)
print(z)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

kaijo=[0]*(N+2)
kaijo[0]=1
kaijo[1]=1
s=0
rui=[0]*(N+2)
rui[0]=0
rev=[0]*(N+2)
rev[0]=N
for i in range(N+1):
    kaijo[i+1] *= (i+1)*kaijo[i]
    rui[i+1] += rui[i] + i+1
    rev[i+1] += rev[i] + (N-i-1)
    

for k in range(K,N+2):
    s += rev[k-1]-rui[k-1]+1

print(s%1000000007)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import scipy as sp
n, k = list(map(int, (input().split())))

k_list = sp.arange(k, n+2, dtype="int64")

k_list = n*k_list - k_list**2 + k_list + 1

ans = 0
for i in k_list:
    ans += i % (10**9 + 7)
    ans = ans % (10**9 + 7)
#print(sum(k_list))
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

dp = []
dp.append(K*(2*N-K+1)//2-K*(K-1)//2+1)
for k in range(K+1,N+1+1):
    s_min = k*(k-1)//2
    s_max =  k*(2*N-k+1)//2
    dp.append((dp[k-1-k]+s_max-s_min+1)%(10**9+7))

ans = dp[-1]
print(ans)

Submission status: Accepted

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

first = 0
second = 0
result = 0
mod = 10 ** 9 + 7

for i in range(K):
    first += i
    second += N - i

result = (result + second - first + 1) % mod

for i in range(K, N + 1):
    first += i
    second += N - i
    result = (result + second - first + 1) % mod

print(result)

Submission status: Accepted

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

m = 10**9 + 7



#print(ans)

rr = 0
mx = 0
mn = 0
for i in range(k):
    mx += (n-i)%m
    mn += i%m

#print(mx)
#print(mn)
#print("-------")
rr = mx-mn+1
#print(rr)
#print("-------")
pp = rr
for j in range(k+1,n+2):
    mn_p = j-1
    mx_p = n-j+1
    #print(mn_p,mx_p)
    if(mx_p==0 or mn_p == n):
        pp += 1
        break
    else:
        pp += rr +mx_p - mn_p
        rr = rr +mx_p - mn_p
        #print(pp)
        
    
    
print(pp%m)

Submission status: Accepted

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

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

print(int(ans))

'''
1ko 0 -- n n+1 ko
2ko 1 -- 2n-1 2n-1 ko
3ko 3 -- 3n-3 3n-5 ko
4ko 6 -- 4n-6 4n-11 ko
k-ko sigma 1 - (k-1) -- k*n - sigma(1 - (k-1))
(sigma 1 - (k-1)) = k * (k-1) / 2
k-ko ippannsiki -> k*n - ( k * (k-1) )  + 1
'''

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K=input().split()
N=int(N)
K=int(K)
if (1<=N<=2*10**5)and(1<=K<=N+1):
    a=(N+1)*(N+1)*(N+2)/2-(1/6)*(N+1)*(N+2)*(2*N+3)+N-K+2-(N+1)*(K-1)*K/2+(K-1)*(2*K-1)*K/6
    a=int(a)
    a=a%(10**9+7)
    print(a)

Submission status: Accepted

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

def S(a,MOD):
    x=[0]*(n+1)
    s=0
    for i in range(1,n+1):
        s+=i
        s%=MOD
        x[i]=s
    return x

ans=0
X=S(n,MOD)
for i in range(k,n+2):
    if i<n+1:
        m=X[n]-X[n-i]
        if m<0:
            m+=MOD
        ans+=(m+1-(X[i-1]-1+1))
    else:
        ans+=1
    ans%=MOD
print(ans)

Submission status: Accepted

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

sum = 0

sum_A, sum_B = 0, 0
for i in range(1, K):
    sum_A += i-1
    sum_B += (N-i+1)

for i in range(K, N+2):
    sum_A += i-1
    sum_B += (N-i+1)
    sum += sum_B - sum_A + 1

print(sum % Z)

Submission status: Accepted

Solution code:
p = 226732710 #10***100 %(10**9+7)
num = 10**9+7
[N,K] = [int(s) for s in input().rstrip().split(' ')]
L = []
ans = 0
for i in range(K,N+2):
    ans += i*(N-i+1)+1
    ans %= num
print(ans)

Submission status: Accepted

Solution code:
import math
import itertools
N, M = map(int,input().split())
s = 0

min = 0
max = 0
for j in range(M):
  min += j
  max += N-j
s = max - min + 1

for i in range(M+1, N+2):
  min += i
  max += N+2-i
  s += max - min + 1
  
print(s%(1000000000+7))

Submission status: Accepted

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

CalcList = tuple(range(int(K), int(N)+2))
N = int(N)
#print(CalcList)

ans = 0

for i in CalcList:
    tmp = 0
    min = i * (i-1) /2
#    print(min)
    max = i * N -min
#    print(max)

    tmp = max - min +1
    ans += tmp

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

Submission status: Accepted

Solution code:
#ここまでテンプレ
from sys import stdin
from sys import setrecursionlimit


setrecursionlimit(10 ** 7)

n,k = map(int,stdin.readline().rstrip().split())

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

point = 0

mod = 10**9+7

for i in range(k,n+2):
    mi = ((li[0]+li[i-1])//2)*i%mod
    ma = ((li[n-i+1]+li[-1])//2)*i%mod
    point += (ma-mi+1)%mod
    point %= mod

print(point)

Submission status: Accepted

Solution code:
from __future__ import print_function

import sys

import re
import array
import copy
import functools
import operator

import math
import string
import fractions
from fractions import Fraction
from fractions import gcd

import collections
import itertools
import bisect

import random
import time

import heapq
from heapq import heappush
from heapq import heappop
from heapq import heappushpop
from heapq import heapify
from heapq import heapreplace
from queue import PriorityQueue as pq
from queue import Queue

from itertools import accumulate

from collections import deque
from collections import Counter

from operator import mul
from functools import reduce

input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return


def lcm(n, m):
    return int(n * m / math.gcd(n, m))


def coprimize(p, q):
    common = math.gcd(p, q)
    return (p // common, q // common)

mod = 10**9 + 7

def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(lambda x,y : x * y % mod, range(n, n - r, -1), 1)
    denom = reduce(lambda x,y : x * y % mod, range(1, r + 1), 1)
    return (numer // denom ) % mod


def find_gcd(list_l):
    x = reduce(gcd, list_l)
    return x


def main():
    n,k = map(int,input().strip().split())
    # sumx=0
    # for i in range(k,n+1):
    #     sumx+=combinations_count(n+1,i)
    #     eprint('combinations_count(n+1,i) ',end=':\n')
    #     eprint(combinations_count(n+1,i))
    # print(sumx)

    ans=0
    for i in range(k,n+2): # kコ選ぶ から n+1コ選ぶ まで
        ans += (i*(n-i+1)+1)
        ans = ans % mod
        # eprint('ans ',end=':\n')
        # eprint(ans)
    
    print(ans)



if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = tuple(int(s) for s in input().split())
# arr = list(int(s) for s in input().split())

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

Submission status: Accepted

Solution code:
def main(N=None, K=None):
    if not N:
        N, K = map(int, input().split())
    ans = (K-N-2)*(2*K*K-K*(N+2)-N*N-N-6)//6
    ans %= 10**9+7
    print(ans)
    return ans


if __name__ == "__main__":
    # assert main(3, 2) == 10
    # assert main(200000, 200001) == 1
    # assert main(141421, 35623) == 220280457
    main()

Submission status: Accepted

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

Submission status: Accepted

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

counter = 0

for i in range(K, N + 2):
    S = (i ** 2 - i) / 2
    B = N * i - S
    counter += B - S + 1
    
print(int(counter) % (1000000000 + 7))

Submission status: Accepted

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

result = 0
x_n = n*(n+1)//2

for i in range(k, n+1):
    x_min = (i-1)*i//2
    x_max = x_n - (n-i)*(n-i+1)//2
    result += (x_max - x_min + 1)
    
result += 1
print(result%(10**9+7))

Submission status: Accepted

Solution code:
import numpy as np
N, M = map(int, input().split())
A = np.asarray(range(N+1), dtype=np.int64)
MOD = 10**9+7
S, E = (N/2) * (N+1), (N/2) * (N+1)
result = 1
for i, j in zip(range(N - M + 1), range(N, -1, -1)):
    S = S - i
    E = E - j
    # print(S, E)
    result += (S - E + 1) % MOD
    result %= MOD
    # print(result)
print(int(result))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
l=[]
for i in range(n+1):
    l.append(i)
l1=[l[0]]
for x in l[1:]:
    l1.append(l1[-1]+x)
#print(l1)
S=0
for i in range(k,n+1):
    S+=l1[n]-l1[n-i]-(l1[i-1]-1)
S+=1

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

# sys.setrecursionlimit(100000)
input = sys.stdin.buffer.readline
INF = 2**62-1

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


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


class Combination:
    def __init__(self, n, mod):

        g1 = [1, 1]
        g2 = [1, 1]
        inverse = [0, 1]
        for i in range(2, n + 1):
            g1.append((g1[-1] * i) % mod)
            inverse.append((-inverse[mod % i] * (mod//i)) % mod)
            g2.append((g2[-1] * inverse[-1]) % mod)
        self.MOD = mod
        self.N = n
        self.g1 = g1
        self.g2 = g2
        self.inverse = inverse

    def __call__(self, n, r):
        if (r < 0 or r > n):
            return 0
        r = min(r, n-r)
        return self.g1[n] * self.g2[r] * self.g2[n-r] % self.MOD



@mt
def slv(N, K):
    ans = 0
    M = 10**9 + 7
    for i in range(K, N+2):
        a = i*(i-1)//2 - 1
        b = i*(2*(N-i+1) + i-1) // 2
        ans += b - a
        ans %= M
    return ans


def main():
    N, K = read_int_n()
    print(slv(N, K))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(K,N+1):
    s = (N*i- i*(i-1) + 1)
    cnt += s%p
    cnt %= p
print((cnt+1)%p)

Submission status: Accepted

Solution code:
data = input().split()
N = int(data[0])
K = int(data[1])
e = [n for n in range(N+1)]
count = 0
for k in range(K,N+2):
    count += k * N - k * (k-1) + 1
print(count%(10**9+7))

Submission status: Accepted

Solution code:
from scipy.special import comb
n,k=map(int, input().split())
ans=0
for j in range(k, n+2):
    ans+= j*(n+1-j)+1
    #print(ans)
    
print(ans%(10**9+7))

Submission status: Accepted

Solution code:
MOD = 10**9+7

N, K = map(int, input().split())
A = [i for i in range(N+1)]
S = [0]*(N+2)
for i in range(1,N+2):
    S[i] = S[i-1] + A[i-1]
C = [0 for i in range(N+1)]
for i in range(N+1): # i+1個とる場合
    mx = S[N+1] - S[N-i]
    mi = S[i+1] - S[0]
    #print(mx, mi)
    C[i] = mx - mi +1
#print(C)
ans = 0
for i in range(K-1, N+1):
    ans += C[i]
    ans %= MOD
print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N, K = [int(x) for x in input().split(' ')]

counter = 0

for i in range(N + 1, K - 1, -1):
  # m = sum(range(N, N - i, -1)) - sum(range(i)) + 1
  m = (i * (2 * N - (i - 1)) - (i - 1) * i) / 2 + 1
  counter += int(m)

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

Submission status: Accepted

Solution code:
N,K = map(int, input().split())
mod = 10**9+7
ans = (((2*K-1)*(K-1)%mod*K%mod - (2*N+3)*(N+2)%mod*(N+1)%mod \
     + 3*(N+1)*(N+1)%mod*(N+2)%mod - 3*(N+1)*K%mod*(K-1)%mod)*pow(6,mod-2,mod) \
     + (N-K+2))%mod
# ans = (((2*K-1)*(K-1)%mod*K%mod - (2*N+3)*(N+2)%mod*(N+1)%mod \
#      + 3*(N+1)*(N+1)%mod*(N+2)%mod - 3*(N+1)*K%mod*(K-1)%mod)/6 \
#      + (N-K+2))%mod

print(int(ans))

Submission status: Accepted

Solution code:
n,k  = map(int,input().split())
cnt = 0
m = 10**9+7
a = [o for o in range(n+1)]
b = sorted(a,reverse = True)
for i in range(k,n+2):
    t = int((n*(n+1))/2-((i-1)*i)/2+1)
    if i < n:
        t -= int(((n-i)*(n-i+1))/2)
    cnt += t
print(cnt%m)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
N,K=map(int, sys.stdin.readline().split())
mod=10**9+7
ans=0
for k in range(K,N+2):
    max_n=(N+(N-k+1))*k/2
    min_n=(k-1)*k/2
    ans+=max_n-min_n+1
print ans%mod

Submission status: Accepted

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


def main():
    n, k = map(int, input().split())
    sum_all = 0
    for i in range(k, n+2):
        # sum_min = sum(list(range(1, i+1)))
        # sum_max = sum(list(range(n+2-i, n+2)))
        # sum_all += (sum_max-sum_min+1)
        sum_all += (n+1-i)*i+1
        sum_min = 0
        sum_max = 0

    # sum_all = (sum_max-sum_min) % (10**9+7)
    print(sum_all % (10**9+7))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, K = list(map(int, input().strip().split()))
M = 10**9 + 7
l = list(range(N+1))
l2 = [0]
for i in range(1, N+1):
  l2.append(l2[i-1] + l[i])
#print(l2)
ans = 0
for i in range(K, N+1):
  #minimum = sum(l[0:i])
  minimum = l2[i-1]
  #maximum = sum(l[-i::])
  maximum = l2[-1] - l2[N-i]
  n = maximum - minimum + 1
  #print(n)
  #print(minimum)
  #print(maximum)
  ans += n
ans += 1
print(ans % M)

Submission status: Accepted

Solution code:
N, K = [int(INPUT) for INPUT in input().strip().split(' ')]

total_num = 0

for k in range(K, N + 2):
    # Min = sum(range(k))
    # Max = sum(range(N - (k - 1), N + 1))
    # # print("%s~%s" % (Min, Max))
    # num = Max - Min + 1
    num = (N - (k - 1)) * k + 1
    total_num += num

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

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

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

N,K = LI()

min_ = 0
for i in range(K):
    min_ += i

max_ = 0
for i in range(K):
    max_ += N-i

ans = max_-min_+1
last_max = max_
for i in range(K+1,N+2):
    min_ += i-1
    max_ += N-i+1
    ans += max_-min_+1
    ans %= mod

print(ans)

Submission status: Accepted

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

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

print(ans%LL)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

left = 0
right = 0
A = []
for k in range(0, N+1):
    left += k
    right += N - k
    A.append((right - left + 1) % (10 ** 9 + 7))

# print(A)
print(sum(A[K-1:]) % (10 ** 9 + 7))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#coding:utf-8
n, k = map(int,input().split())
count = 0

for i in range(k,n+2):
    mini = 0.5 * i * (i - 1)
    maxi = 0.5 * n * (n + 1) - 0.5 * (n - i) * (n - i + 1)
    dif = maxi - mini + 1
    count += dif

count = count % (10 ** 9 + 7)
print("{}".format(int(count)))

Submission status: Accepted

Solution code:
from collections import defaultdict

n, m = list(map(int, input().split()))

def func(x):
    res = 0
    res += -1/6 * x * (x + 1)* (2*x + 1)+ (n + 1) * 1/2 * x * (x + 1) + x
    return int(res)

print(int((func(n + 1) - func(m - 1)) % (10 ** 9 + 7)))

Submission status: Accepted

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

mod = 10 ** 9 + 7
ans = 0
begin = 0
end = 0
a = 0
b = n
for i in range(k):
    begin += a
    end += b
    a += 1
    b -= 1

ans += end - begin + 1

for i in range(k + 1, n + 2):
    begin += a
    end += b
    a += 1
    b -= 1
    ans += end - begin + 1

if k == n + 1:
    ans = 1
    
print(ans % mod)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

MOD = 10**9 + 7

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

n = N+1
ans = 0
a = np.arange(n)
l = np.cumsum(a)
r = np.cumsum(a[::-1])[::-1]
l %= MOD
r %= MOD
for i in range(k, n+1):
    ans += r[-i] - l[i-1] +1
    ans %= MOD
print(ans)

Submission status: Accepted

Solution code:
import math


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


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


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

ans = 0
M = N + 1 - K

MOD = 10**9+7

for m in range(M + 1):
    #print(summ(N-(m+K-1), N) - summ(0, m+K-1) + 1)
    ans += (summ(N-(m+K-1), N) - summ(0, m+K-1) + 1) % MOD

#print(summ(1, 3))
print(ans % MOD)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K =map(int,input().split())
def tasu(n,m):
    return((n-m+1)*m+m*(m+1)/2)

answer = 0
for i in range(K,N+2):
    maxsum = tasu(N,i)
    minsum = tasu(i-1,i)
    answer += maxsum-minsum+1
    answer = answer%(10**9+7)
print(int(answer))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
N,K=map(int, input().split())
mod=1000000007
A=[i for i in range(N+1)]
ans=0

S=[0]*(N+1)
for i in range(1,N+1):
    S[i]=S[i-1]+A[i]

for k in range(K,N+1):
    MIN=S[k-1]
    MAX=S[N]-S[N-k]
    ans+=(MAX-MIN+1)%mod
    ans%=mod
print(ans+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))
n,k = iim()
ans = 0
mod = 1000000007
for i in range(k,n+2):
    ans += n*(n+1)//2 - i*(i-1)//2 - (n-i)*(n-i+1)//2 + 1
    ans = ans%mod

print(ans)

Submission status: Accepted

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


sum = 0
for k in range(K,N+2):
    # sum(arr[0:0+k]), sum([l-,l])
    s1 = ((0 + k-1) * k) // 2
    s2 = (((N - k + 1) + N) * k) // 2
    sum = sum + (s2 - s1 + 1)

print(sum % (1000000000 + 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):
  # 0 ~ N から k個選んだ時の最小値min = 0+1+...+(k-1) = (k-1)*k//2
  # 0 ~ N から k個選んだ時の最大値max = N-K+1+...+N = (2N-k+1)*k//2
  # k個選んだ時の場合の数 : max-min+1 = k*(N-k+1)+1個(min:0 max:3なら0,1,2,3の4個)
  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())

ans = 0
buttom = 0
for i in range(k):
    buttom += n - i

top = 0
for i in range(k):
    top += i

_max = 0
for i in range(n+1):
    _max += n - i

_MOD = 10**9+7

for i in range(k, n+1+1):
    ans += buttom - top + 1
    buttom = min(_max, buttom+n-i)
    top += i
    ans = ans % _MOD

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def f(i,j):
    return max(0, j*(i-j)+1)

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

i = N + 1
print(sum(f(i,j) for j in range(N+2-K)) % jon)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

n,k = (int(x) for x in input().split())
ans = 0
a = n*(k-1)
b = (k-1)*(k-2)//2

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

Submission status: Accepted

Solution code:
a = list(map(int,input().split()))
ans=0
min=0
max=0
for i in range(a[1]):
    min +=i
    max +=a[0]-i
    minx=i-1
    maxx=a[0]-i-1
ans=max-min+1
for i in range(a[1]+1,a[0]+2):
    minx +=1
    min += minx
    maxx += -1
    max +=maxx
    ans=(ans+max-min+1)%(10**9+7)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#author : dokueki

import sys
INT_MAX = sys.maxsize
INT_MIN = -(sys.maxsize)
sys.setrecursionlimit(10**7)
mod = 1000000007


def IOE():
    sys.stdin = open("input.txt", "r")
    sys.stdout = open("output.txt", "w")


def main():
    n, k = map(int, sys.stdin.readline().split())
    if k > n:
        print(1)
    else:
        ans = 0
        for i in range(k, n+2):
            i = i - 1
            l = ((i * (i + 1)) // 2)%mod
            r = ((n*(n+1))//2 - ((n-i)*(n-i-1))//2) % mod
            ans += ((r-l)+1) % mod
        print(ans % mod)


if __name__ == "__main__":
    try:
        IOE()
    except:
        pass
    main()

Submission status: Accepted

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

MOD = 10 ** 9 + 7

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
def summ(n, nn=0):
  nn = max(nn, 0)
  return n*(n+1)/2 - nn*(nn+1)/2

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

a = 1
for i in range(k, n + 1):
  mmin = summ(i - 1)
  mmax = summ(n, n - i)
  m = mmax - mmin + 1
  a += m

k = 10**9 + 7
print(int(a%k))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 10**9 + 7

N, K = rl()

import itertools

def f(n, k):
    return k*(n-k) + 1

N += 1
ans = 0
for k in range(K, N+1):
    ans = (ans + f(N, k)) % mod
print(ans)