Problem:


Score : 400 points

Problem Statement

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

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

Constraints

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

1 \leq K \leq N+1

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

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

Sample Input 1

3 2

Sample Output 1

10

The sum can take 10 values, as follows:

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

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

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

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

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

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

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

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

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

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

Sample Input 2

200000 200001

Sample Output 2

1

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

Sample Input 3

141421 35623

Sample Output 3

220280457



Sample input:
3 2


Sample output:
10

Submission status: Accepted

Solution code:
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from math import factorial, sqrt, ceil, gcd
from functools import lru_cache, reduce
INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)

# UnionFind
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# ダイクストラ
def dijkstra_heap(s, edge, n):
    #始点sから各頂点への最短距離
    d = [10**20] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

# 素因数分解
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

# 2数の最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)

# リストの要素の最小公倍数
def lcm_list(numbers):
    return reduce(lcm, numbers, 1)

# リストの要素の最大公約数
def gcd_list(numbers):
    return reduce(gcd, numbers)

# 素数判定
def is_prime(n):
    if n <= 1:
        return False
    p = 2
    while True:
        if p ** 2 > n:
            break
        if n % p == 0:
            return False
        p += 1
    return True


# limit以下の素数を列挙
def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P

# 同じものを含む順列
def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        # set（集合）型で重複を削除、ソート
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret


# ここから書き始める
n, k = map(int, input().split())
ans = 0
for x in range(k, n + 2):
    ans += x * n - (x - 1) * x + 1
print(ans % MOD)

Submission status: Accepted

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

    print(ans % 1000000007)
    
resolve()

Submission status: Accepted

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

x = 10 ** 9 + 7

#A = [0] * (N-K+1)
answer = 0
#new = map(lambda x : x + 1, list(range(N+1)))
#print(new)

for k in range(N+1, K-1, -1):
    #print("Itereation :" + str(k))
    min_a = k * (k-1) // 2
    max_a = k * (2*N - k + 1) // 2
    #print("min: " + str(min_a))
    #print("max: " + str(max_a))
    num = max_a - min_a + 1
    #print("Num " + str(num))
    answer = (answer + (num % x))%x 
print(answer)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
X = list(map(int, input().split()))
N = int(X[0])
K = int(X[1])
#print(N,K)
S = 0

for i in range(K,N+2):
  Min = (i-1)*i/2
  Max = (N-i+N+1)*i/2
#  print(Min,Max)
  S+=(Max-Min+1)
  
print(int(S%(pow(10,9)+7)))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#D 
inp = input().split(' ')
N = int(inp[0])
K = int(inp[1])

print((int((N+1)*((N+1)*(N+2) - K*(K-1))/2.0 - ((N+1)*(N+2)*(2*N+3) - (K-1)*K*(2*K-1))/6.0) + (N+1+1-K)) % (int(10**9) + 7))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import numpy as np
A=list(input().split())
N=int(A[0])
K=int(A[1])

from math import factorial

t=[]
for i in range(K,N+2):
    t.append(i*(N-i+1)+1)


print((np.sum(t)%(10**9+7)))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
lis = []
for k in range(K,N+2):
    lis.append((N-k+1)*k+1)

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

Submission status: Accepted

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

MOD = 10**9 + 7

data = []
for i in range(n+1):
  data.append(i)

ans = 0

# あらかじめ累積和を求めておく
sum_data = [0] * (n + 1)
for i in range(1, n + 1): # 1..4
  sum_data[i] = sum_data[i - 1] + i

# sum_data: [0 1 3 6]

# 処理系
for i in range(k, (n+1) + 1): # 1..3
  # min
  min_ = sum_data[i-1]

  # max
  if n+1 - i - 1 == -1:
    max_ = sum_data[-1]
    # print("max :", max_)
  else:
    max_ = sum_data[-1] - sum_data[n+1 - i - 1]
    # print("max :", max_)

  diff = (max_ - min_ + 1)
  ans += diff

print(ans % MOD)

Submission status: Accepted

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

mod = 10 ** 9 + 7
num = n * (n + 1) // 2 + 2
ans = 0
for i in range(k, n + 2):
    left = (0 + (i - 1)) * i // 2
    right = ((n - (i - 1)) + n) * i // 2
    ans += (right - left) + 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):
  a=(i*(i-1))/2
  b=((N*2-i+1)*i)/2
  ans=int((ans+b-a+1)%1000000007)

print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline
MOD = 1000000007

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


res = 0

if N < K:
    print(1)
    exit()

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

res = 0

for i in range(K,N+2):
    if i != K:
       _min+=i-1
       _max+=(N+1 -i)

    #print(_min,_max)
    
    res +=(_max-_min+1)

print(res%MOD)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n, k = map(int, input().split())
    num = [0] * (n + 1)
    for i in range(1, n + 1):
        num[i] = num[i - 1] + i
    num.append(num[-1])
    ans = 0
    for i in range(k, n + 2):
        max_num = num[n] - num[max(0, n - i)]
        min_num = num[i - 1]
        ans += max_num - min_num + 1
        ans %= (10 ** 9 + 7)
    print(ans)

Submission status: Accepted

Solution code:
def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    N, K = read_ints()
    # 0, 1, 2, ..., N
    # k = 3
    # (0+1+2)+...(N-2+N-1+N)
    total = 0
    prefix = sum(range(K))
    suffix = sum(range(N, N-K, -1))
    modulo = 10**9+7
    for k in range(K, N+2):
        total = (total+(suffix-prefix+1))%modulo
        prefix = (prefix+k)%modulo
        suffix = (suffix+N-k)%modulo
    return total


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

Submission status: Accepted

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

MOD = 1000000007  # type: int


def solve(N: int, K: int):
    ct = 0
    for k in range(K, N+2):
        ct += int((N + N - k + 1) * k / 2 - (k - 1) * k / 2 + 1) % MOD

    print(ct % MOD)
    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(' '))
result = 0

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

print(int(result))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#l=int(input())
n,k=map(int,input().split())
#pl=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]

mod=10**9+7
total=0

for m in range(k,n+2):
    add=m*(n-m+1)+1
    total=(total+add)%mod
print(total)

Submission status: Accepted

Solution code:
import numpy as np

s = list(map(int, input().split()))
N,K = s[0],s[1]
ans = 0
n = 10**9 + 7

"""
if K==2:
    min = (0+K-1)*K/2
    max = (N-K+1+N)*K/2
elif K==3:
    min = (0+K-1)*K/2
    max = (N-K+1+N)*K/2
else:
    min = (0+K-1)*K/2
    max = (N-K+1+N)*K/2
"""

def num(x, N):
    y = -x**2 + (N+1)*x +1
    return y

#a = np.array(range(K,N+2))
#b = num(a,N)
ans = num(K,N)
for i in range(1,N-K+2):
    if ans >= n:
        ans = ans % n
    ans += num(K+i,N)

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read

# def input(): return sys.stdin.readline().strip()
# def INT(): return int(input())
# def MAP(): return map(int, input().split())
# def LIST(): return list(map(int, input().split()))
# def ZIP(n): return zip(*(MAP() for _ in range(n)))


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

# a = list(map(int, input().split()))  # n個の文字列がリストに格納される

def sum_num(l,r): 
    result = (l+r) * (r-l+1)/2
    return result

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

ans=0
for s in range(K,N+2):
    L = sum_num(0,s-1)
    R = sum_num(N-s+1,N)
    ans += R - L + 1

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

Submission status: Accepted

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

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

mod = 10 ** 9 + 7

sum = 0

for i in range(k, n + 2):
    
    min_value = int(i * (i - 1) / 2)
    max_value = int(i * (2 * n - i + 1) / 2)
    
    sum += max_value - min_value + 1

print(sum % mod)

Submission status: Accepted

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

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

min_lis = [0] * (N + 2)
max_lis = [0] * (N + 2)

for i in range(1, N + 2):
    min_lis[i] = min_lis[i - 1] + (i - 1)
    max_lis[i] = max_lis[i - 1] + (N - i + 1)

answer = 0
for k in range(K, N + 2):
    answer += max_lis[k] - min_lis[k] + 1
print(answer % MOD)

Submission status: Accepted

Solution code:
nk = input()
nk = nk.split()
[n,k] = nk
n, k = int(n), int(k)
ans = 0
for s in range(k, n + 2):
    temp = s*(n-s+1) + 1
    ans += temp
    if(ans > 1000000007):
        ans = ans % 1000000007
print(ans)

Submission status: Accepted

Solution code:
a, b = map(int, input().split())
MOD = 10 ** 9 + 7
n = [0] * (a + 1)

for i in range(1, a + 1):
    n[i] = n[i - 1] + i

ans = 0
for k in range(b - 1, a + 1):

    ans = (ans + (n[a] - n[max(0, a - k - 1)]) - n[k] + 1) % MOD

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    mod=10**9+7
    N,K=map(int,readline().split())
    A=list(range(N+1))
    s1=A[:]
    s2=A[:]
    for i in range(1,N+1):
        s1[i]+=s1[i-1]
    for i in range(N-1,-1,-1):
        s2[i]+=s2[i+1]
    
    res=0
    for l in  range(K-1,N+1):
        a=s2[N-l]-s1[l]+1
        res+=a
        res%=mod
    print(res)

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
import numpy as np
from itertools import combinations
n,K = map(int,input().split())
num_ls = [i for i in range(n+1)]
csum_num_ls = list(np.cumsum(num_ls))
mod = 1000000007

ans = 0
for k in range(K,n+1):
    smallest = csum_num_ls[k-1]
    biggest = csum_num_ls[-1] - csum_num_ls[-k-1]
    ans_k = biggest - smallest + 1
    ans += ans_k
    ans %= mod
ans += 1
print(ans)

Submission status: Accepted

Solution code:
import numpy as np 

a = input()
N,K = np.array(a.split()).astype(int)

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

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

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

print(ans%mod)

Submission status: Accepted

Solution code:

mod = pow(10, 9) + 7

def main():
    N, K = map(int, input().split())
    ans = 0
    for k in range(K, N+2):
        #ans += sum(range(N+1-k, N+1)) - sum(range(0, k)) + 1
        ans += (N*(N+1))//2 - ((N-k)*(N-k+1))//2 - ((k-1)*k)//2 + 1
        ans %= mod
    print(ans%mod)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
'''ika tako
選ぶ個数を固定して、それ毎に結果を足し合わせればよい。
k  個の数を選ぶとき、各項に含まれる  10^100  は除いて考えても
和の個数を求める上では問題ない。
最小値は小さい方から  k  個を足し合わせた数0+1+2+…+(k-1)
初項  0 、末項  k−1 、長さ  k  の等差数列の総和なので  
k(k−1)/2 
最大値は大きい方から  k  個を足し合わせた数(N-k+1)+…+(N-1)+N
初項  N−k+1 、末項N 、長さk  の等差数列の総和なので  
k(2N−k+1)/2 
数字は1刻みで、特に選び方に制約も無いので、最小値～最大値の間の整数は全て作れる。
'''
# 累積和解法
from itertools import accumulate
 
n, k = map(int, input().split())
MOD = 10 ** 9 + 7
f_acc = [0] + list(accumulate(list(range(n + 1))))
b_acc = [0] + list(accumulate(reversed(list(range(n + 1)))))
ans = 0
for i in range(k, n + 2):
    min_ = f_acc[i]
    max_ = b_acc[i]
    ans = (ans + max_ - min_ + 1) % MOD
print(ans)

Submission status: Accepted

Solution code:
import sys
N, K = map(int, sys.stdin.readline().split())
MOD = 10 ** 9 + 7
m = sum(range(K))
M = sum(range(N - K + 1, N + 1))
ans = (M - m + 1) % MOD
for i in range(K + 1, N + 2):
    m += i - 1
    M += N - i + 1
    p = M - m + 1
    ans += p % MOD
print(ans % MOD)

Submission status: Accepted

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

def counts(n,k):
    if k==1:
        return N+1
    else:
        return k*N - k*( k - 1 ) + 1

total =0
for k in range(K,N+2):
    total += int(counts(N,k))

print(total%mod)

Submission status: Accepted

Solution code:
# @oj: atcoder
# @id: hitwanyang
# @email: 296866643@qq.com
# @date: 2020-04-29 14:45
# @url:https://atcoder.jp/contests/abc163/tasks/abc163_d
import sys,os
from io import BytesIO, IOBase
import collections,itertools,bisect,heapq,math,string
# region fastio

BUFSIZE = 8192

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")
# ------------------------------
def main():
   nk=list(map(int,input().split()))
   n,k=nk[0],nk[1]
   ans=0
   mod=10**9+7
   for i in range(k,max(n+2,k)):
       ans+=((n+1)*i-i*i+1)
   print (ans%mod)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#abc163  Sum of Large Numbers
n, k = map(int, input().split())
def Funmy(a, N):
    return (8+3*a*N + 3*N -2*a*a)*a/6

total =Funmy(n+1,n) -Funmy(k-1,n)
m = 7 + 10**9
output = (total)%m
print(int(output))

Submission status: Accepted

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

min = K*(K-1)/2
max = K*(2*N-K+1)/2


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

    sum+=(int(max-min+1))
print(sum%(10**9+7))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

mod = 10**9+7

ans = 0
for k_ in range(k, n+2):
    low_lim = (0+(k_-1))*k_//2
    up_lim = ((n-k_+1)+n)*k_//2
    # print(up_lim-low_lim + 1)
    ans += up_lim - low_lim + 1

# low_lim = (((1+k) % mod) * (k % mod)) % mod // 2
# up_lim = (((1+n) % mod)*(n % mod) % mod)//2


# print(num % mod)
print(ans % mod)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def sum_start(k):
    return (k-1) * k / 2

def sum_end(n,k):
    return sum_start(n+1) - sum_start(n-k+1)


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

total = 0
while((N + 1) >= K):
    start = sum_start(K)
    end = sum_end(N,K)
    total += end - start + 1
    K += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

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


ans = 0
for i in range(K,N+2):
    a = sum_lr(N-(i-1),N)
    b = sum_lr(0,i-1)
    temp = a - b + 1
    # print('i',i)
    # print(a)
    # print(b)
    # print(temp)
    ans += temp
    ans %= MOD
print(ans)

Submission status: Accepted

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

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

print(sum%1000000007+1)

Submission status: Accepted

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

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

mod = 10 ** 9 + 7
ans = 0

for i in range(K, N+2):
    sum_min = i * (i - 1) / 2
    sum_max = i * (2*N - i + 1) / 2
    ans += sum_max - sum_min + 1

print(int(ans % mod))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K=(int(x) for x in input().split())
# N,K=3,2
ans=0
for i in range(K,N+2):# i個選ぶ
    
    m=i*(i-1)/2
    M=N*(N+1)/2-(N-i)*(N-i+1)/2
    ans+=M-m+1
    # print("i,m,M",i,int(m),int(M))
print(int(ans%(1e9+7)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
mod = 10**9 + 7
def sum(a, b):
    r = (a + b)*(b - a + 1) / 2
    return int(r)

def main():
    sum_num = 0
    N, K = list(map(int, input().split(" ")))
    for i in range(K, N+2):
        mi = sum(0, i - 1)
        ma = sum(N-i+1, N)
        sum_num += ma - mi + 1

    print(sum_num % mod)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
l = [_ for _ in range(n + 1)]

tmp_min = sum(l[0:k])
tmp_max = sum(l[-1:-k-1:-1])
x = tmp_max - tmp_min + 1

for i in range(n-k+1):
    tmp_min += l[k+i]
    tmp_max += l[-k-i-1]
    x += tmp_max - tmp_min + 1

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

Submission status: Accepted

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

mod = 1000000007


def sum(a, b):
    r = (a+b)*(b-a+1) / 2
    return int(r) if r < mod else int(r % mod)


res = 0
for i in range(K, N+2):
    num_l = sum(0, i-1)
    num_r = sum(N-i+1, N)
    res += (num_r - num_l + 1)

print(res % mod)

Submission status: Accepted

Solution code:
mod = 10**9 + 7
def main():
    N,K = map(int,input().split())
    ans = 0
    S = [0]
    for i in range(1,N+4):
        S.append( S[-1] + i )
    for k in range(K,N + 1):
        ans = (ans + (S[N] - S[N - k]) - S[k - 1] + 1)%mod
    print((ans+1)%mod)
    return

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def q4():
    N, K = (int(i) for i in input().split())

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

    d, ans = divmod(ans, 10**9 + 7)
    print(ans)


if __name__ == '__main__':
    q4()

Submission status: Accepted

Solution code:
import math

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

mod = 10**9 +7

sn = (n*(n+1)*(n+2)/6 +n+1)% mod
sk = ((k-1)*k*(3*n-2*k+4)/6 +k-1)% mod

snk = math.floor((sn - sk)% mod)

print(snk)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = list(map(int, input().split(' ')))
# Memo:
#     sum of k numbers from 0 ~ N
#     -> min: k(k-1)/2
#        max: min + k(N-k+1)
#     -> num of sum patterns: k(N-k+1)+1
#     sum up values above from k=K to k=N+1
val = (N * (N+1) * (N+2) - K * (K - 1) * (3 * N - 2 * K + 4)) // 6 + N + 2 - K
print(val % (10**9 + 7))

Submission status: Accepted

Solution code:
def S(k):
    R = 0
    for i in range(1, k+1):
        R += i
    return R

N, K = [int(n) for n in input().split()]
# 0,1,2,...,Nからk個選ぶときの和の種類を求めて
# K <= k <= N+1 の総和をとる

# N + 1
# 2N - 2*1 + 1
# 3N - 2*(1+2) + 1

# 0     ~ N               N                   +1
# 0+1   ~ N+(N-1)         2N - (1)    -(1)    +1
# 0+1+2 ~ N+(N-1)+(N-2)   3N - (2+1)  -(1+2)  +1

# k個選ぶとき kN - 2*(1+2+...+(k-1)) + 1 種類
# これを KからN+1まで
M = 10**9 + 7
B = K*N - 2*S(K-1) + 1
A = B
for k in range(K+1, N+2):
    B += N - 2*(k-1)
    A += B
print(A % M)

Submission status: Accepted

Solution code:
def f_sum(l, r):
    return (l+r)*(r-l+1)//2

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

MOD = 10**9 + 7
print((culc(N, N+1)-culc(N, K-1))%MOD)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
# 小さい方から順にk個とったものと、大きい方から順にk個とったものの差
# １刻みから作る最小と最大間は、全てを満たす


def calc(x, N):
    a = x * (x - 1) // 2
    b = (2 * N - x + 1) * x // 2
    return b - a + 1


tot = 0
for i in range(K, N + 2):
    ans = calc(i, N)
 #   print(ans)
    tot += ans

aa = tot % (10 ** 9 + 7)

print(aa)

Submission status: Accepted

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

ans = 0
max=0
min=0

a=[0]*(n+1)
b=[0]*(n+2)

for i in range(n+1):
    a[i]=i
    b[i+1]=a[i]+b[i]

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

ans = ans % p

print(ans)

Submission status: Accepted

Solution code:
a,b=map(int,input().split(" "))
#print(a,b)
min=0
max=0
count=0

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
ans = 0
mod = 10 **9 + 7
for i in range(k, n+2):
    # i個(k<= i <= n+1)を選ぶとき
    # i * 10^100 + (1+2+・・・+(i-1)) 〜 i * 10^100 + (n + (n-1) + ・・・ + (n-(i-1) ) )
    # の間の値をとりうるので
    # i*(n + n-(i-1))//2 - (i-1)*i//2 + 1 = i * (n-i+1) + 1
    ans += i * (n - i + 1) + 1
print(ans % mod)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
Z = list(map(int,input().split()))
N = Z[0]
K = Z[1]
x = int(N*(N+1)*(N+2)/6+(K-1)*K*(2*K-3*N-4)/6+N+2-K)
y = x%1000000007
print(y)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

def main():
  n, k = map(int, input().split())
  n += 1
  cnt = 0
  for ik in range(k, n+1):
    cnt += ik * (n - ik) + 1
    cnt %= (10**9 + 7)
  print(cnt)

if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
MOD = 10**9 + 7
ans = 0
for i in range(K, N+2): #頭からK個の和と
    ans += i * (N - i + 1) + 1
print(ans % MOD)

Submission status: Accepted

Solution code:
N, K = (int(x) for x in input().split())
def sumcount(l, r):
    return (l+r)*(r-l+1)//2
num = 0
for i in range(K, N+2):
  l = sumcount(0,i-1)
  r = sumcount(N-i+1,N)
  num += r-l+1
print(num%(10**9+7))