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 Combination:
    def __init__(self, size, mod=10**9 + 7):
        self.size = size + 2
        self.mod = mod
        self.fact = [1, 1] + [0] * size
        self.factInv = [1, 1] + [0] * size
        self.inv = [0, 1] + [0] * size

        for i in range(2, self.size):
            self.fact[i] = self.fact[i - 1] * i % self.mod
            self.inv[i] = -self.inv[self.mod % i] * (self.mod // i) % self.mod
            self.factInv[i] = self.factInv[i - 1] * self.inv[i] % self.mod

    def npr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * self.factInv[n - r] % self.mod

    def ncr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * (self.factInv[r] * self.factInv[n - r] % self.mod) % self.mod

    def nhr(self, n, r):  # 重複組合せ
        return self.ncr(n + r - 1, n - 1)

    def factN(self, n):
        if n < 0:
            return 0
        return self.fact[n]

N, K = map(int, input().split())
MOD = 10**9 + 7
S = N * (N + 1) // 2

ans = 1
for d in range(N + 1):
    if N - d < K:
        break
    mx = S - (d * (d + 1) // 2)
    mi = max(0, (N - d - 1) * ((N - d - 1) + 1) // 2)
    ans += (mx - mi + 1) % MOD
    ans %= MOD
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
a = [i for i in range(n+1)]
a.reverse()
s = 0
l = k-1
c = sum(a[:k])
for i in range(k,n+2):
	#print (a[-1*i:])
	s += c - (i-1)*i//2 + 1
	s %= 10**9+7
	l += 1
	if l >= n+1:
		break
	c += a[l]
print (s)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def num(k):
    n = (N)*(N+1)//2%inf - (N-k)*(N-k+1)//2%inf - k*(k-1)//2%inf +1
    return n

N,K = map(int,input().split())
inf = 10**9+7
ans = 0
for k in range(K,N+2):
    ans += num(k)%inf

print(ans%inf)

Submission status: Accepted

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

MOD = 1000000007  # type: int


def solve(N: int, K: int):
    res = 0
    r = 1000000007
    for k in range(K, N+2):
        # print(N-k+2, N+1, 1, k)
        res += (sigma(N-k+2, N+1) - sigma(1, k) + 1)
        res %= r

    print(res)


def sigma(left, right):
    return (right - left + 1) * (left + right) // 2


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:
mod = 10**9+7
n,k = [int(x) for x in input().strip().split()]
res=0
for j in range(k,n+2):
    mn = (j-1)*j//2
    mx = j*(2*n-j+1)//2
    res = (res%mod+(mx-mn+1)%mod)%mod

print(res%mod)

Submission status: Accepted

Solution code:
def main():
    MOD = 10 ** 9 + 7

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

    ans = 0
    low = sum(range(K - 1))
    high = sum(range(N - K + 2, N + 1))
    for take in range(K, N + 2):
        low += take - 1
        high += N - (take - 1)
        ans = (ans + high - low + 1) % MOD
    print(ans)


if __name__ == '__main__':
    main()

# import sys
# input = sys.stdin.readline
# 
# sys.setrecursionlimit(10 ** 7)
# 
# (int(x)-1 for x in input().split())
# rstrip()

Submission status: Accepted

Solution code:
import heapq
from collections import defaultdict, deque
from math import ceil, factorial, gcd
from itertools import accumulate
import sys
import bisect
import numpy as np

sys.setrecursionlimit(10 ** 7)
INF = float("inf")
MOD = 10 ** 9 + 7

si = lambda: input().strip()
ii = lambda: int(input())
mii = lambda: map(int, input().split())
lmii = lambda: list(map(int, input().split()))
smii = lambda: sorted(map(int, input().split()))

# ----------

N, K = mii()

num = [x for x in range(N+1)]
# num.reverse()

acc = list(accumulate(num))

ans = 0
for i in range(K, N+1):
    if i == 1:
        ans += N + 1
    else:
        tail = acc[N] - acc[N-i]
        head = acc[i-1]
        ans += tail - head + 1
    ans %= MOD

print((ans+1)%MOD)

Submission status: Accepted

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

        ans %= p

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

mod = pow(10,9) +7

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

Submission status: Accepted

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

MOD = 10**9+7

st = int((K-1)*(K)/2)
go = int((2*N-K+1)*(K)/2)
ans = 0

for i in range(N+1-K+1):
    ans += go - st + 1
    if ans >= MOD:
        ans = ans % MOD
    st = st + i + K
    go = go + N - K - i
    
print(ans)

Submission status: Accepted

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

MOD = 10 ** 9 + 7
count = 0
total = (N * (N + 1)) // 2
for k in range(K, N + 2):
    lo = k * (k - 1) // 2
    temp = N - k + 1
    hi = total - temp * (temp - 1) // 2
    count += hi - lo + 1
print(count % MOD)

Submission status: Accepted

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

left = 0
right = 0
ans = 0
MOD =10**9+7
for i in range(n+1):
	left += i
	right += n-i
	if i >= k-1:
		ans += right - left + 1
		ans %= MOD

print(ans%MOD)

Submission status: Accepted

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

# x個の選んだ際の合計値の最小 から 最大 の間の数すべてがx個選んだ際の合計値になりうると仮定

def one_to_a_sum(a):
    return (a+1)*a//2

def a_to_b_sum(a, b):
    return one_to_a_sum(b) - one_to_a_sum(a-1)

sum_num = 0
for i in range(K, N+2):
    min_num = a_to_b_sum(0, i-1)
    max_num = a_to_b_sum(N-(i-1), N)
    sum_num = (sum_num + max_num-min_num+1) % (10**9+7)

print(sum_num)

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 = (count + int(((1/2*i*(i-1)+i*(n-i+1)) -(1/2*i*(i+1)-i)+1)%MOD))%MOD

print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import setrecursionlimit, exit
setrecursionlimit(1000000000)

def main():
    mod = 1000000007
    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 %= mod
    print(ans)

main()

Submission status: Accepted

Solution code:
N,K = map(int,input().split(' '))
num_list = list(reversed(range(N+1))) 
sum_list_reversed = [num_list[0]]
for i in range(1,N+1):
    sum_list_reversed.append(sum_list_reversed[i-1] + num_list[i])
num_list = list(range(N+1))
sum_list = [num_list[0]]
for i in range(1,N+1):
    sum_list.append(sum_list[i-1] + num_list[i])
ans = 0

for i in range(K-1,N+1):
    current_sum = sum_list_reversed[i] - (sum_list[i] -1)
    ans += current_sum
    ans = ans%(10**9+7)
print(ans)

Submission status: Accepted

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

for i in range(n + 2 - k):
    max_n = n * (n + 1) / 2 - (n - (k + i)) * (n + 1 - (k + i)) / 2
    min_n = (k + i) * (k + i - 1) / 2
    res = (res + max_n - min_n + 1) % 1000000007

print(int(res))

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:

# -*- coding: utf-8 -*-
N,K=map(int, input().split())

ans=0
for k in range(K,N+2):
    minc=round(k*(k-1)/2)
    maxc=round(k*(2*N-k+1)/2)
    count=maxc-minc+1
    ans+=count
    #print(count)

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n,k=list(map(int,input().split()))
od=10**9+7
s=0
e=0
ans=0
for i in range(n):
    s+=i
    e+=n-i
    if i+1<k:
        continue
    ans+=(e-s+1)%od
print((ans+1)%od)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
cnt = 0
for k in range(K, N+2):
    mn = k * (k - 1) // 2
    mx = k * (N - k + 1) + mn
    # print("k:{}, min:{}, max:{}".format(k, mn, mx))
    cnt += (mx-mn+1)

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
import sys
import os
from io import BytesIO, IOBase

#########################
# imgur.com/Pkt7iIf.png #
#########################

# returns the list of prime numbers less than or equal to n:
'''def sieve(n):
    if n < 2: return list()
    prime = [True for _ in range(n + 1)]
    p = 3
    while p * p <= n:
        if prime[p]:
            for i in range(p * p, n + 1, p):
                prime[i] = False
        p += 2
    r = [2]
    for p in range(3, n + 1, 2):
        if prime[p]:
            r.append(p)
    return r'''

# returns all the divisors of a number n(takes an additional parameter start):
'''def divs(n, start=1):
    divisors = []
    for i in range(start, int(n**.5) + 1):
        if n % i == 0:
            if n / i == i:
                divisors.append(i)
            else:
                divisors.extend([i, n // i])
    return len(divisors)'''

# returns the number of factors of a given number if a primes list is given:
'''def divn(n, primes):
    divs_number = 1
    for i in primes:
        if n == 1:
            return divs_number
        t = 1
        while n % i == 0:
            t += 1
            n //= i
        divs_number *= t
    return divs_number'''

# returns the leftmost and rightmost positions of x in a given list d(if x isnot present then returns (-1,-1)):
'''def flin(d, x, default=-1):
    left = right = -1
    for i in range(len(d)):
        if d[i] == x:
            if left == -1: left = i
            right = i
    if left == -1:
        return (default, default)
    else:
        return (left, right)'''

#count xor of numbers from 1 to n:
'''def xor1_n(n):
    d={0:n,1:1,2:n+1,3:0}
    return d[n&3]'''

def cel(n, k): return n // k + (n % k != 0)
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(map(int, input().split()))
def lcm(a, b): return abs(a * b) // math.gcd(a, b)
def prr(a, sep=' '): print(sep.join(map(str, a)))
def dd(): return defaultdict(int)
def ddl(): return defaultdict(list)
def ddd(): return defaultdict(defaultdict(int))

# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
            while True:
                    b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
                    if not b:
                            break
                    ptr = self.buffer.tell()
                    self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
            self.newlines = 0
            return self.buffer.read()

    def readline(self):
            while self.newlines == 0:
                    b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
                    self.newlines = b.count(b"\n") + (not b)
                    ptr = self.buffer.tell()
                    self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
            self.newlines -= 1
            return self.buffer.readline()

    def flush(self):
            if self.writable:
                    os.write(self._fd, self.buffer.getvalue())
                    self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
            self.buffer = FastIO(file)
            self.flush = self.buffer.flush
            self.writable = self.buffer.writable
            self.write = lambda s: self.buffer.write(s.encode("ascii"))
            self.read = lambda: self.buffer.read().decode("ascii")
            self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

from collections import defaultdict 
#from collections import deque
#from collections import OrderedDict
from math import gcd,pi
#import time
#import itertools
#import timeit
#import random
#from bisect import bisect_left as bl
#from bisect import bisect_right as br
#from bisect import insort_left as il
#from bisect import insort_right as ir
#from heapq import *
#from queue import PriorityQueue
#mod=998244353
mod=10**9+7
# for counting path pass prev as argument:
# for counting level of each node w.r.t to s pass lvl instead of prev:

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

Submission status: Accepted

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

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

    num = maxsum-minsum +1
    sum += num
    sum = sum % (10**9+7)

print(int(sum))

Submission status: Accepted

Solution code:
a=input().split(" ")
N=int(a[0])
K=int(a[1])

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

import sys
readline = sys.stdin.readline
read = sys.stdin.read

n,k = [int(i) for i in read().split()]

a = list(range(n+1))
r = sum(a[n+1-k:])
l = sum(a[:k])

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

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

Submission status: Accepted

Solution code:
from heapq import *
import sys
from collections import *
from itertools import *
from decimal import *
import copy
from bisect import *
import time
import math
def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))
N,K=map(int,input().split())
mod=10**9+7
"""
fac=[1,1]
for i in range(2,2*(N+1)):
    fac.append(fac[-1]*i%mod)
ifac=[pow(i,mod-2,mod) for i in fac]
def comb(a,b):
    if a<b:return(0)
    return(fac[a]*ifac[a-b]*ifac[b]%mod)
"""
c=0
for k in range(K,N+2):
    X=(N)*k - (k-1)*k//2 - (k-1)*k//2  +1
    c+=X
    #print(X,k)
    c%=mod
print(c)

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# coding: utf-8

import sys
import math
import collections
import itertools
INF = 10 ** 13
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def lcm(x, y) : return (x * y) // math.gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def LS() : return input().split()
def RS(N) : return [input() for _ in range(N)]
def LRS(N) : return [input().split() for _ in range(N)]
def PL(L) : print(*L, sep="\n")
def YesNo(B) : print("Yes" if B else "No")
def YESNO(B) : print("YES" if B else "NO")

N, K = LI()

k = list(range(N+1))
ans = 1
l = sum(k[0:K-1])
r = sum(k[N-K+2:])
# print(l,r)
for i in range(K-1, N):
    l += k[i]
    r += k[N-i]
    
    if  r-l < 0:
        break
    # print(l,r)
    ans = (ans + (r-l) + 1) % MOD

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys,os
from collections import defaultdict, deque
from math import ceil, floor
if sys.version_info[1] >= 5:
    from math import gcd
else:
    from fractions import gcd
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    MOD = 10**9+7

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

    

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
cnt = [0] * (n + 1)
MOD = 10**9+7
l, r = 0, 0
for i in range(n + 1):
    l += i
    r += n-i
    #print(l, r)
    cnt[i] = (r - l + 1) % MOD
ans = 0
for i in range(k-1, n + 1):
    ans += cnt[i] % MOD

print(ans % MOD)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip() 

def resolve():
    p = 10**9+7

    def combMod(n, r, p):
        numer = 1
        denom = 1
        for i in range(1, r+1):
            numer = numer * (n-r+i) % p
            denom = denom * i % p
        return numer * pow(denom, p-2, p) % p

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

    ans = 0
    for i in range(K, N+2):
        #0からN+iまでの和から、N+1-iからN+1までの和
        ans += ((N-i+1)+(N))*i//2 - (i-1)*i//2 + 1
        ans %= p

    print(ans)

if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
s=list(range(0,n+1))
cs=[]
mod=10**9+7
sum=0
for i in range(n+1):
	sum+=s[i]
	cs.append(sum)
cnt=0
for i in range(k,n+1):
	mini=cs[i-1]
	maxi=cs[n]-cs[n-i]
	cnt+=(maxi-mini+1)%mod
	cnt%=mod
print((cnt+1)%mod)

Submission status: Accepted

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

Submission status: Accepted

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

div = 10**9 + 7

ans = 0

top = (2*N-K+2)*(K-1) / 2
bottom = (K-1)*(K-2) / 2

for i in range(N-K+2):
    # print(top, bottom)
    top += (N-K+1-i)
    bottom += (K-1+i)

    ans = (ans + top-bottom+1) % div

print(int(ans))

Submission status: Accepted

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

cnt = 0

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

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

Submission status: Accepted

Solution code:
DIV = 10 ** 9 + 7


max_num, min_select = map(int, input().split())

ans = 0
for select in range(min_select, max_num + 2):
    sum_min = (select * (select - 1)) // 2
    sum_max = max_num * select - sum_min
    ans += (sum_max - sum_min + 1) % DIV

print(ans % DIV)

Submission status: Accepted

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

cnt = 0
L, R = 0, 0
for i in range(k-1):
    L += i
    R += n - i

for i in range(k-1, n+1):
    L += i
    R += n - i
    cnt += R - L + 1
print(cnt%mod)

Submission status: Accepted

Solution code:
def by_k(k):
    return k*(N+2) - k*(k+1) + 1

M = 10**9+7
N, K = [int(x) for x in input().split()]
ans = 0
for k in range(K, N+2):
    ans += by_k(k)
    while ans > M:
        ans -= M
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

for k in range(K,N+2):
    m=k*(k-1)//2
    M=k*(2*N-k+1)//2
    ans+=(M-m+1)
    ans=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*i-i**2+i+1
    ans = int(ans%(10**9+7))
print(ans)

Submission status: Accepted

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

def sum_(a, b):
    if (a-b)%2 == 1:
        return int((b+a) * (b-a+1) / 2)
    else:
        return int((b+a) * (b-a+1) / 2)

ret = 0
for i in range(K, N+2):
    ret += sum_(N-i+1, N) - sum_(0, i-1) + 1

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

Submission status: Accepted

Solution code:
MOD = 10**9+7
def cmb(n, r):
  if r < 0 or n < r:
    return 0
  r = min(r, n - r)
  return fact[n] * factinv[r] * factinv[n-r] % MOD

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

fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]
for i in range(2, N+1 + 1):
  fact.append((fact[-1] * i) % MOD)
  inv.append((-inv[MOD % i] * (MOD // i)) % MOD)
  factinv.append((factinv[-1] * inv[-1]) % MOD)


res = 0

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

for i in range(N+1+1)[K:]:
  res += (max_sum - min_sum)+1
  max_sum += N-i
  min_sum += i

print(res%MOD)

Submission status: Accepted

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

import sys
sys.setrecursionlimit(300000)

MOD = 1000000007  # type: int

def solve(N: int, K: int):
    sums = [0] * (N + 2)
    for i in range(1, N + 2):
        sums[i] = sums[i - 1] + i
        sums[i] %= MOD

    cum = [0] * (N + 3)
    for i in range(N + 2):
        if i == 0:
            tmp = N + 1
        else:
            #tmp = (i + 1) * N - sums[i]
            #tmp = (i + 1) * N - sums[i]
            tmp = sums[N] - sums[max(0, N - i)]
            tmp -= sums[i - 1] - 1
        tmp %= MOD
        cum[i + 1] = cum[i] + tmp
        cum[i + 1] %= MOD
    #print(cum)
    ret = cum[N + 2] - cum[K]
    ret %= MOD
    print(ret)
    return

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:
#D

#デバッグ
debug=0
def pront(*n):
    if debug==1:
        print(n)
        
#10**9+7を底とした処理

base=10**9+7

def add(a,b):#a+b
    return (a+b)%base

def sub(a,b):#a-b
    return (a-b)%base

def multi(a,b):#a*b
    return (a*b)%base

def exp(a,b):#a**b
    x=1
    c=a
    d=b
    while d!=0:
        if d%2==1:
            x=multi(x,c)
        c=(c**2)%base
        d=d//2
    return x

def div(a,b):#a/b
    return(multi(a,exp(b,base-2)))

#入力
N,K=map(int,input().split())

#処理
    #その他演算
def fact(m,n):#m~nの積(m<n)
    x=1
    for i in range(m,n+1):
        x=multi(x,i)
    return x
def comb(n,r):
    return div(fact(n-r+1,n),fact(1,r))

def ssum(m,n):#m~nの和(m<=n)
    x=(n)*(n+1)//2
    y=(m-1)*(m)//2
    return x-y

x=0

for i in range(K,N+2):
    #pront(i,ssum(N-i+1,N),ssum(0,i-1))
    x+=ssum(N-i+1,N)-ssum(0,i-1)+1
print(x%base)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

MOD = 10 ** 9 + 7

N, K = lr()
floor = (K-1) * (K-2) // 2
limit = (N+(N-K+2)) * (K-1) // 2
answer = 0
for x in range(K, N+2):
    floor += (x-1)
    limit += N-x+1
    answer += limit - floor + 1

print(answer % MOD)

Submission status: Accepted

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

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

MY_DIV = 1000000007
    
ans = 0
for i in range(K, N + 2):
    # sum_min = sum_tab[i] - sum_tab[0]
    # sum_max = sum_tab[-1] - sum_tab[-1 - i]
    # print("#", i, (sum_min, sum_max))
    # ans_tmp = sum_max - sum_min + 1
    ans_tmp = ((N - i + 1) * i + 1) % MY_DIV
    #
    ans = (ans + ans_tmp) % MY_DIV

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
def main():

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

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

    return ans

if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
ans=0
MOD=10**9+7
n,k=map(int,input().split())
min=0
max=n
num0=0
num1=n
for i in range(n+1):
  #print(min,max)
  i+=1
  if i>=k:
    ans+=max-min+1
    ans=ans%MOD
  min+=num0+1
  num0+=1
  max+=num1-1
  num1-=1
print(ans)

Submission status: Accepted

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

N += 1
n_patterns = 0
remain_min = 0
remain_max = 0

for i in range(M-1):
    remain_min += i
    remain_max += N - 1 - i

for i in range(M-1, N):
    remain_min += i
    remain_max += N - 1 - i
    n_remain = remain_max - remain_min + 1
    n_patterns += n_remain


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7
INF = 10 ** 12
import sys
sys.setrecursionlimit(100000000)
dy = (-1,0,1,0)
dx = (0,1,0,-1)
from  itertools import permutations
from heapq import heapify,heappop,heappush

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

if __name__ =='__main__':
    main()

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
A = []
c = 0

for i in range(N+2):
    A.append(i*(i-1)//2)

for i in range(K,N+2):
    c += A[N+1]-A[N-i+1]-A[i]+1
    
print(c%(10**9+7))

Submission status: Accepted

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

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

print(resp % (pow(10, 9) + 7))

Submission status: Accepted

Solution code:
import numpy as np
N, K = map(int, input().split())
MOD = 10**9 + 7
arr = np.arange(0, N + 1)
# print(arr[-K:], arr[:K])
_max = arr[-K:].sum()
_min = arr[:K].sum()
# print('_max, _min', _max, _min)
ans = _max - _min + 1
for k in range(K, N + 1):
    # print('k, arr[-k], arr[k]', k, arr[-(k + 1)], arr[k])
    _max += arr[-(k + 1)]
    _min += arr[k]
    # print('_max, _min', _max, _min)
    ans += _max - _min + 1
    ans = ans % MOD
print(ans)

Submission status: Accepted

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

MOD = 10**9+7

minus = sum(range(k))
plus  = sum(range(n+1-k,n+1))
count = plus - minus + 1

for i in range(k,n+1):
  plus += n-i
  minus += i
  
  count += plus - minus + 1
  count %= MOD
  
print(count)

Submission status: Accepted

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

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

S+=1

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0

s = n*(n+1)//2

for i in range(k, n+2):
    min_ = (i-1)*i//2
    max_ = s - (n-i)*(n-i+1)//2
    ans += (max_ - min_ +1)%mod

print(ans%mod)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
MOD=10**9+7
t=[]
r=[]
for i in range(0,n+1):
    t.append(i)
for i in range(n,-1,-1):
    r.append(i)

fr=0
af=0
for i in range(0,k-1):
    fr+=t[i]
    af+=r[i]

ans=0
for i in range(k-1,n+1):
    fr+=t[i]
    #print(fr)
    af+=r[i]
    ans+=(af-fr+1)%MOD

print(ans%MOD)