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
# sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]


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

def solve():
    n, k = MI()
    B = [i for i in range(0, n+1)]
    # print(B)
    mod = 1_000_000_007

    cnt = 0
    for i in range(k, n+2):
        mn = ssum(B[0], B[i-1])
        mx = ssum(B[n-i+1], B[-1])
        # print(mn, mx)
        cnt = cnt + mx - mn + 1
    print(cnt % mod)

if __name__ == '__main__':
    solve()

Submission status: Accepted

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

B=[0]*(N+2)
for i in range(N+1):
    B[i+1]=B[i]+A[i]

while True:
    if K>N+1:
        break
    else:
        ans+=B[-1]-B[N-K+1]-B[K]+1
        K+=1

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

Submission status: Accepted

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

Submission status: Accepted

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

a = 0

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

Submission status: Accepted

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

for i in range(K, N + 2):
    m = 1 / 2 * (i - 1) * i 
    M = 1 / 2 * (N + N - i + 1) * i
    if M == m:
        ans += 1
    else:
        ans += (M - m + 1)

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

Submission status: Accepted

Solution code:
n, k = list(map(int, input().split()))
# a = list(map(int, input().split()))
a = [i for i in range(n+1)]  # 1~nまで
ans = 0
mod = pow(10, 9)+7
sum_min = sum(a[:k])  # 前からkコの和
sum_max = sum(a[-k:])  # 後ろからkコ
for i in range(k, n+1):
    ans += sum_max-sum_min+1
    ans %= mod
    sum_min += a[i]  # 足してく
    sum_max += a[-i-1]
ans += 1  # 全部選んだ時
print(ans % mod)

Submission status: Accepted

Solution code:
import sys
from itertools import combinations
sys.setrecursionlimit(10 ** 6)  # 再帰関数の再帰の深さを設定
to_index = lambda x: int(x) - 1  # 入力した数字に1を引いたものを返す
print_list_in_2D = lambda x: print(*x, sep="\n")  # リストの要素を改行を挟んで表示する関数

# 入力を整数に変換して受け取る
def input_int():
    return int(input())

def map_int_input():
    return map(int, input())

MII = map_int_input

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

def MII_to_index():
    return map(to_index, input())

def MII_split_to_index():
    return map(to_index, input().split())

# 入力全てを整数に変換したものの配列を受け取る
def list_int_inputs():
    return list(map(int, input()))

LII = list_int_inputs

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

# 2次元リスト化
def LII_2D(rows_number):
    return [LII() for _ in range(rows_number)]

def LII_split_2D(rows_number):
    return [LII_split() for _ in range(rows_number)]

N, K = MII_split()

count = 0
p_min_sum = 0
p_max_sum = 0
for num_nums in range(K, N + 2):
    # 実は，すべての和を実際に計算する必要はない．
    # M個の数字を選んだ場合，和の最小値は小さい方からM個，最大値は大きい方からM個だが，
    # 最小値と最大値の間の数は ---全て作ることができる---
    # 和は和の公式か尺取法か累積和を使って求める
    # --- 尺取法？ ---
    if num_nums == K:
        p_min_sum = sum(range(num_nums))
        p_max_sum = sum(range(N - num_nums + 1, N + 1))
    else:
        p_min_sum += num_nums - 1
        p_max_sum += N - num_nums + 1

    # （等差数列の）和の公式
    # p_min_sum = num_nums * (num_nums - 1) // 2
    # p_max_sum = num_nums * (2 * N - num_nums + 1) // 2
    #
    count += p_max_sum - p_min_sum + 1

    # すべての和の計算（TLE）
    # tmp_nums = []
    # for n in combinations(range(0, N + 1), num_nums):
    #     s = sum(n)
    #     tmp_nums.append(s)
    # count += len(set(tmp_nums))

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

Submission status: Accepted

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

ans = 0

for i in range(K,N+2):
  Min = i*(i-1)/2
  Max = i*(2*N-i+1)/2
  ans += Max-Min+1
print(int(ans%Mod))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
MOD = 10**9 + 7


def main():
    N, K = (int(i) for i in input().split())
    ans = 0
    for i in range(K, N+2):
        # lower = (i-1)*i//2
        # upper = N*i - (i-1)*i//2
        # ans += upper - lower + 1
        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())
mod = 1000000007
ans = 0
for i in range(k,n+2):
    j = i-1 
    if i == k:
        min = (j*j+j)//2 % mod
        max = sum(list(range(n,n-k,-1)))%mod
    else:
        min += j %mod
        max += (n-i+1) % mod
    ans += max - min +1
    ans %= mod
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# # 整数の入力
# a = int(input())
# # スペース区切りの整数の入力
# b, c = map(int, input().split())

# 整数の入力
a, b = map(int, input().split())

if a+1 == b:
    print(1)
else:
    res_count_list = []

    min_before = 0
    max_before = 0
    for i in range(b, a+2):
        if min_before == 0 and max_before == 0:
            min_before = sum(range(i))
            max_before = sum(range(a, a-i, -1))
        else:
            min_before = min_before + i - 1
            max_before = max_before + a - i + 1
        # print(max)
        # print(min_before)
        # print(max_before)
        res_count_list.append(max_before - min_before + 1)

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

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

resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K=map(int, input().split())
ans=0
mod=10**9+7
for i in range(K,N+2):
    if i==K:
        MIN=sum(list(range(0,K)))%mod
        MAX=sum(list(range(N,N-K,-1)))%mod
    else:
        MIN+=(i-1)%mod
        MAX+=(N+1-i)%mod
    ans+=(MAX-MIN+1)%mod
    ans%=mod

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
MOD = 10**9 + 7


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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

DIVIDER = 1000000007

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

ans = 0
for cnt in range(K, N + 2):
    val_min = (tab_sum[cnt] - tab_sum[0]) % DIVIDER
    val_max = (tab_sum[-1] - tab_sum[-1 - cnt]) % DIVIDER
    #
    ans = (ans + (val_max - val_min + 1)) % DIVIDER

print(ans)

Submission status: Accepted

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

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

print (ans)

Submission status: Accepted

Solution code:
def sum_from_1(n):
    return n * (n + 1) // 2


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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
mod = 7 + 10 ** 9

def solve():
    input = sys.stdin.readline
    N, K = map(int, input().split())
    ans = 0
    for k in range(K, N + 2):
        minS = (k - 1) * k // 2
        maxS = (N - k + 1 + N) * k // 2
        ans += (maxS - minS + 1)
        ans %= mod

    print(ans) 

    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

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

count_sum = 1

for k in range(K,N+1):
    count_sum += k*(N-k+1)+1
    
print(int((count_sum)%(1e9 + 7)))

Submission status: Accepted

Solution code:
mod = 10 ** 9 + 7

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

ans = 0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

MOD = int(1e9 + 7)


def solve(tc):
    n, k = map(int, stdin.readline().split())

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

        ans += hi-lo+1
        ans %= MOD
    print(ans)


tcs = 1
LOCAL_TEST = not __debug__
if LOCAL_TEST:
    infile = __file__.split('.')[0] + "-test.in"
    stdin = open(infile, 'r')
    tcs = int(stdin.readline().strip())

tc = 1
while tc <= tcs:
    solve(tc)
    tc += 1

Submission status: Accepted

Solution code:
#!/usr/bin/python3

from functools import reduce
from operator import add
import sys


def main():
    N, K = map(int, input().split(' '))
    sys.setrecursionlimit(N + 50)

    C = 1
    for i in range(K, N + 1):
        C += (N * (N + 1) // 2 - (N - i) * (N - i + 1) // 2) \
            - ((i * (i - 1)) // 2) \
            + 1
    print(C % 1000000007)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,k=map(int,input().strip().split())
ans=0
while(k<=n):
    max=(n+n-k+1)*k//2
    min=(0+k-1)*k//2
    ans=ans+max-min+1
    k=k+1
ans=(ans+1)%1000000007
print(ans)

Submission status: Accepted

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

print(s% 1000000007)

Submission status: Accepted

Solution code:
import math

n, k = map(int, input().split())
ary = [i for i in range(n + 1)]
# print(ary)

mod = 10 ** 9 + 7

ans = 0
min_v = sum(ary[0:(k - 1)])
max_v = sum(ary[(n + 1 - (k - 1)):])
# print(min_v, max_v)
for i in range(k, n + 2):
    min_v += ary[i - 1]
    max_v += ary[(n + 1 - i)]
    # print(min_v, max_v)
    ans = (ans + max_v - min_v + 1) % mod

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

n, k = [int(x) for x in input().split()]
a = [int(x) for x in range(n + 1)]
a_rev = a[::-1]
mod = 10**9 + 7

left = sum(a[:k])
right = sum(a_rev[:k])

ans = right - left + 1

for i in range(k, n + 2):
    if i > n:
        break
    left += a[i]
    right += a_rev[i]
    ans += right - left + 1
    ans %= mod

print(ans)

Submission status: Accepted

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

def calc(N, K):
    x = 0
    for i in range(K, N + 2):
        x += rangesum(N + 1 - i, i) - rangesum(0, i) + 1
    return x % (10 ** 9 + 7)


(N, K) = tuple([int(s) for s in input().split()])
print(calc(N, K))

Submission status: Accepted

Solution code:
import sys
from collections import deque
import math

input = sys.stdin.readline


def solve():
    N, K = map(int, input().split())
    answer = 0
    for k in range(K, N + 2):
        answer += k * (N - k + 1) + 1
    return answer % (10 ** 9 + 7)


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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
n,k=mii()

arr = [i for i in range(0,n+1)]
lenarr = len(arr)

S = [0]
for i in range(0, lenarr):
    S.append(S[i] + arr[i])
#print(S)
ans=0
for i in range(k,n+2):
    a = S[-1]-S[-1-i] - S[i]
    ans+=a+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):
    M = i*(2*N-i+1)//2
    m = i*(i-1)//2
    ans += (M - m + 1)
print(ans % mod)

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

Submission status: Accepted

Solution code:
def input_1line_number():
    return map(int, input().split())

def sum_of_equal_number_sequence(first_term, difference, number_of_terms):
    return (2*first_term + (number_of_terms-1)*difference) * number_of_terms // 2

def mod_number():
    return 10**9+7

def main():
    n, k = input_1line_number()

    answer = 0

    for i in range(k, n+2):
        number_of_fractions = sum_of_equal_number_sequence(n-i+1, 1, i) - sum_of_equal_number_sequence(0, 1, i) + 1
        answer += number_of_fractions

    answer %= mod_number()

    print(answer)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Ans = [0]*(N+2)
Sum_Ans = 0

def solution(N,K,Ans,Sum_Ans):
    for i in range(K,N+2):
        #max[i] = (2*N - i + 1)*i / 2
        #min[i] = i*(i - 1) / 2
        Ans[i] = i*(N - i + 1) + 1
        
        Sum_Ans += Ans[i]
    
    print(Sum_Ans%(10**9 + 7))

solution(N,K,Ans,Sum_Ans)

Submission status: Accepted

Solution code:
def main():
    import sys
    import itertools

    input = sys.stdin.readline

    n, k = map(int, input().split())
    l = list(range(0, n + 1))
    l_1 = list(itertools.accumulate(l))
    ans = 0
    for i in range(k, n + 2):
        if i == n + 1:
            ans += 1
        else:
            mini = l_1[i - 1]
            maxi = l_1[-1] - l_1[-i - 1]
            ans += (maxi - mini + 1)
    print(ans % (10 ** 9 + 7))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:


# coding: utf-8
DATA = """
141421 35623
""".split("\n")

def get_debug_line():
	for i in DATA:
		yield i

if 1:
	get_line = input
else:
	get_line = get_debug_line().__next__
	get_line()

MOD = 10**9 + 7

N, M = [int(i) for i in get_line().strip().split(" ")]

minV = (0 + M-1) * M // 2
maxV = (N-M+1 + N) * M // 2
#print (minV, maxV)
#0, 1, 2, 3
count = 0
for i in range(M, N+2):
    count += (maxV - minV + 1)
    #print (i, N+1, minV, maxV)
    minV += i
    maxV += (N-i)
print (count%MOD)

Submission status: Accepted

Solution code:
# n, k = 0
n, k = map(int, input().split())
# 0,1,2,3,,,,,,n
# k個選んだ時の最小値と最大値の差をk,k+1,k+2,,,,nと積算する

iResult = 0  # 答え
for i in range(k, n + 2):
#	print(i)
	#	min = 0
	#	max = 0
	#	for j in range(0, i):
	#		print('    ' + str(j))
	#		min = min + j
	#		max = max + n - j
#	print(iResult)
	iResult = iResult + i * (n - i + 1) + 1

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

Submission status: Accepted

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

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

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import numpy as np
n,k = map(int,input().split())
mod = 10**9 + 7
ans = 0
if k==n+1:
    print(1)
    exit()
for i in range(k, n+2):
    low = (i-1)*i//2
    high = (2*n-i+1)*i//2
    ans += high - low + 1
    ans %= mod
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):
    min = i*(i - 1)/2
    max = (N + (N-i+1)) * (N - (N- i + 1)+1)/2
    ans += (max - min + 1)
print(int(ans%mod))

Submission status: Accepted

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

ans = 0
min_x, max_x = 0, 0
for i in range(N+1):
    min_x += i
    max_x += N-i
    if i+1 < K: continue
    ans += max_x - min_x + 1
    ans %= mod

print(ans)

Submission status: Accepted

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

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

ans %= 1000000007
print(ans)

Submission status: Accepted

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

n_min = sum([i for i in range(0, k-1)])
n_max = sum([n - i for i in range(0, k-1)])

for i in range(k-1, n+1):
  n_min += i
  n_max += n - i
  ans += n_max - n_min + 1
    
print(ans % mod)

Submission status: Accepted

Solution code:
import sys
import itertools
flatten = itertools.chain.from_iterable
input = sys.stdin.readline


def inp():
    return int(input())


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


N, K = inpl()

ss = list(range(0, N + 3))
for i in range(1, N + 3):
    ss[i] += ss[i - 1]

mod = 10**9 + 7

ans = 0
for k in range(K, N + 2):
    L = ss[k]
    R = ss[N + 1] - ss[N + 1 - k]
    ans += (R - L + 1)
    ans %= mod

print(ans)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
	N, K = map(int, input().split())
	ans = 0
	P = 10**9 + 7
	for k in range(K,N+2):
		minimum = k * (k-1) // 2
		maximum = (2*N - k + 1) * k / 2
		v = maximum - minimum + 1
		ans += v%P

	print(int(ans%P))

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

Submission status: Accepted

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

res = 0

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

res = res % mod

print(res)

Submission status: Accepted

Solution code:
NUM = 10**9 + 7


def sum1(n):
    return n*(n+1)//2


def sum_arithmetic_seq(a, d, n):
    ans = n*(2*a+(n-1)*d)//2
    return ans


n, k = map(int, input().split())
ans = 0
for i in range(k, n+2):
    tmp = sum_arithmetic_seq(n-i+1,1,i) - sum_arithmetic_seq(0,1,i) + 1
    ans += tmp % NUM
print(ans % NUM)

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    # 1 <= N <= 2 * 10^5
    # 1 <= K <= N + 1
    N, K = map(int, input().split(" "))

    total_num = 0
    for k in range(K, N+2):
        # NからN-(k-1)までの和
        sum_max = int(k*(2*N - k + 1) / 2)
        # 0から(k-1)までの和
        sum_min = int(k*(k-1) / 2)
        # 個数を計上
        total_num += sum_max - sum_min + 1
        total_num %= 1000000007
    
    print(total_num)

Submission status: Accepted

Solution code:
# coding: utf-8

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

ans = 0
for i in range(k, n+2):
    mini = (0+(i-1))*i/2
    maxi = ((n)+(n-i+1))*i/2
    ans += maxi-mini+1
    
print(int(ans%z))

Submission status: Accepted

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

res = 0

for k in range(K, N+2) :

  mini = int(k*(k-1)/2)
  maxi = int(k*(2*N-k+1)/2)

    
  res = (res + (maxi-mini+1)) % (10**9+7)

  
print(res)

Submission status: Accepted

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

def calc(a):
    smallest=a*(a-1)//2
    largest=smallest+a*(n-a+1)
    return largest-smallest+1

ans=0

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

print(ans)

Submission status: Accepted

Solution code:

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

l = range(n+1)

c = 0

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

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

Submission status: Accepted

Solution code:
import math
n, k = map(int, input().split())
mod = 1000000007
m = math.ceil((n+1)/2)

def makeTable(n):
    fac = [n+1]
    
    for i in range(1, m):
        fac.append(fac[i-1]+(n-i)-i)
    return fac

def answer(fac):
    ans = 0
    for i in range(k-1, n):
        if (i < m):
            ans = ans + fac[i]
        else:
            ans = ans + fac[n-1-i]
    ans = ans + 1
    return ans

fac = makeTable(n)
print(answer(fac) % mod)

Submission status: Accepted

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

Submission status: Accepted

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

def calc_ans(AR,i):
	max = AR*(AR-1)/2 - (AR-i)*(AR-i-1)/2
	min = i*(i-1)/2
	return max - min +1

for i in range(k, AR+1):
	ans += calc_ans(AR,i)

print(int(ans % mod))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np
MOD = 10 ** 9 + 7

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

k = np.arange(K, N + 2, dtype=np.int64)
low = (k - 1) * k // 2
high = N * k - low
cnt = high - low + 1
print(cnt.sum() % MOD)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import numpy as np

n, k = map(int, input().split())
MOD = int(1e9+7)
rem = (10*100)%MOD 
ans = 0 

for i in range(k, n+2):
    if i == k:
        minimum = np.arange(i).sum()
        maximum = np.arange(n, n-i, -1).sum()
    else:
        minimum += i-1 
        maximum += n-i+1
    ans += (maximum-minimum+1)%MOD

print(ans%MOD)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = [int(e) for e in input().split()]
nums = []
u = 10**9 + 7
t = 0
s = 0
Min =0
Max =0
for i in range(0,N[1]):
    Min += i
    t = i+1
    Max += N[0] - i
    s = N[0] - i -1
ans = Max - Min + 1
for k in range(N[1]+1,N[0]+2):
    Min += t
    t += 1
    Max += s
    s -= 1
    ans += Max - Min + 1
print(ans%u)

Submission status: Accepted

Solution code:
import math

def fac(x):
    if x<=1:
        return 1
    s=1
    for i in range(1,x+1):
        s=s*i
    return s
def com(m,n):
    return fac(m)/(fac(n)*fac(m-n))


temps=input().split()
n=int(temps[0])
m=int(temps[1])
sum=0
for i in range(m,n+2):
    min=int((i-1)*i/2)
    max=int((2*n-i+1)*i/2)
    sum=sum+max-min+1
print(int(sum%(math.pow(10,9)+7)))

Submission status: Accepted

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

total = 0
for i in range(K, N + 2):
  maximum = (N - i + 1 + N) * i / 2
  minimum = (i - 1) * i / 2
  count = maximum - minimum + 1
  total += (count % mod)
  
print(int(total % mod))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

mod = 10**9+7 

res = 0
for i in range(K, N+2):
    ma = (2*N-i+1)*i/2
    mi = (i-1)*i/2
    res += (ma-mi+1)%mod

print(int(res%mod))

Submission status: Accepted

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

mod = 10**9+7 #出力の制限

res = 0
for i in range(K, N+2):
    ma = (2*N-i+1)*i/2
    mi = (i-1)*i/2
    res += (ma-mi+1)%mod

print(int(res%mod))

Submission status: Accepted

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

Submission status: Accepted

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

    wa=max-min+wa+1


wa=wa+1
wa=wa%(10**9+7)
print(int(wa))

Submission status: Accepted

Solution code:
a, b = map(int, input().split())
mod = pow(10,9) + 7

def cal(a,i):
  min = i*(i-1)/2
  max = a*(a-1)/2 - (a-i)*(a-i-1)/2
  return max - min + 1

count = 0
for i in range(a-b+2):
  count += cal(a+1,i+b)
print(int(count % mod))

Submission status: Accepted

Solution code:
#create date: 2020-04-19 21:10

import sys
stdin = sys.stdin

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n, k = na()
    ans = 0
    for s in range(k, n+2):
        mmin = (0+s-1)*s//2
        mmax = (n-s+1+n)*s//2
        ans += mmax - mmin + 1
        #print(s, mmax, mmin)
    print(ans%(10**9+7))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:

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



#数字が1刻みなので最大値と最小値の間の数字は全部求める事が可能である
#ただし足す項の数で結果が異なるので注意する。

N = N+1

#選ぶ項の数がKの時の組み合わせの数を確認する
def minmax(N,K):
    #最大値-最小値={N+(n-1)+   }-{k+(k-1)+(k-2)+   }
    #=(N-k)*k
    return (N-K)*K+1


Ans = 0
for i in range(K,N+1):
    Ans = Ans + minmax(N,i)


mod = 1000000007
Ans = Ans % mod

print(Ans)

Submission status: Accepted

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

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

print(cnt)

Submission status: Accepted

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

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


print(result%1000000007)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

max_sum = np.sum(ref[-k + 1:])
min_sum = np.sum(ref[:k - 1])
if k == 1:
    max_sum = min_sum = 0
ans = 0

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

print(ans)