A permutation p of size n is an array such that every integer from 1 to n occurs exactly once in this array.

Let's call a permutation an almost identity permutation iff there exist at least n - k indices i (1 ≤ i ≤ n) such that p_{i} = i.

Your task is to count the number of almost identity permutations for given numbers n and k.


-----Input-----

The first line contains two integers n and k (4 ≤ n ≤ 1000, 1 ≤ k ≤ 4).


-----Output-----

Print the number of almost identity permutations for given n and k.


-----Examples-----
Input
4 1

Output
1

Input
4 2

Output
7

Input
5 3

Output
31

Input
5 4

Output
76

Difficulty: MEDIUM_HARD

Solutions:
import math

def calculate_derangement(fac, n):
	ans = fac[n]
	for i in range(1, n + 1):
		if i % 2 == 1:
			ans -= fac[n] // fac[i]
		else:
			ans += fac[n] // fac[i]
	return ans
(n, k) = map(int, input().split())
fac = [1]
for i in range(1, n + 1):
	fac.append(fac[i - 1] * i)
ans = 0
for i in range(0, k + 1):
	choose = fac[n]
	choose //= fac[i]
	choose //= fac[n - i]
	ans += choose * calculate_derangement(fac, i)
print(ans)


import math
(n, k) = map(int, input().split())
dp = [1, n * (n - 1) // 2, n * (n - 1) * (n - 2) * 2 // 6, n * (n - 1) * (n - 2) * (n - 3) * 9 // 24]
ans = 0
for i in range(0, k):
	ans += dp[i]
print(ans)


from functools import reduce
_ = "\n (1) Let's define function f(n, k) - exactly k permutation elements\n     on non identical indices.\n     Our result function is then: F(n, k) = sum(f(n, ki) ki = 0 .. k)\n \n (2) f(n, 0) = 1\n     f(n, 1) = 0\n \n (3) We choose k indices - C(n, k).\n     All other indices are fixed and identical.\n     We need to permutate k! elements\n     so we do not set them to their own identical positions.\n     Let's define the latter function as ni(k)\n \n (4) ni(0) = 1\n     ni(1) = 0\n     ni(2) = 1\n     ni(3) = 2\n     0 1 2 - doesn't count\n     0 2 1 - doesn't count\n     1 0 2 - doesn't count\n     1 2 0\n     2 0 1\n     2 1 0 - doesn't count\n \n (5) We choose (k-1) elements for first position.\n     Let's define chosen element c1\n     Then we go to c1 position. There we can choose from (k-1) elements (c2).\n     Then we go to c2 position. There we can choose from (k-2) elements (c3).\n     Then we go to c3 position. There we can choose from (k-3) elements (c4).\n     ni'(k) := (k-1)*(k-1)!\n \n     ni'(2) := (2-1)*(2-1)! = 1\n     ni'(3) := (3-1)*(3-1)! = 4 - WRONG\n \n Let's just brutforce h(n, k) or manually check the last k=4 answer.\n"
ni = [1, 0, 1, 2, 9]
(n, k) = map(int, input().split())

def f_exactly_k(n, k):
	return reduce(lambda x, y: x * y, range(n - k + 1, n + 1), 1) // reduce(lambda x, y: x * y, range(1, k + 1), 1) * ni[k]
s = 0
for ki in range(k + 1):
	s += f_exactly_k(n, ki)
print(s)


import sys
import math
import operator as op
from functools import reduce
from sys import maxsize

def read_line():
	return sys.stdin.readline()[:-1]

def read_int():
	return int(sys.stdin.readline())

def read_int_line():
	return [int(v) for v in sys.stdin.readline().split()]

def read_float_line():
	return [float(v) for v in sys.stdin.readline().split()]

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer / denom

def nc2(n):
	return n * (n - 1) // 2
t = 1
for i in range(t):
	(n, k) = read_int_line()
	dp = [0] * (n + 1)
	dp[0] = 1
	dp[1] = 0
	dp[2] = 1
	for i in range(3, n + 1):
		dp[i] = (i - 1) * (dp[i - 1] + dp[i - 2])
	ans = 0
	for i in range(k + 1):
		ans += ncr(n, i) * dp[i]
	print(int(ans))


(n, k) = map(int, input().split())
o = 1
if k > 1:
	o += n * (n - 1) // 2
if k > 2:
	o += n * (n - 1) * (n - 2) // 3
if k > 3:
	o += n * (n - 1) * (n - 2) * (n - 3) // 8 * 3
print(o)


(n, k) = map(int, input().split())
ans = 1
k = min(k, n)
if k == 1:
	print(1)
elif k == 2:
	ans += n * (n - 1) // 2
	print(ans)
elif k == 3:
	ans += n * (n - 1) * (n - 2) // 3 + n * (n - 1) // 2
	print(ans)
else:
	ans += n * (n - 1) * (n - 2) * (n - 3) // 4 + n * (n - 1) * (n - 2) * (n - 3) // 8 + n * (n - 1) * (n - 2) // 3 + n * (n - 1) // 2
	print(ans)


(n, k) = [int(i) for i in input().split()]
ans = 1
if k >= 2:
	ans += n * (n - 1) // 2
if k >= 3:
	ans += n * (n - 1) * (n - 2) // 6 * 2
if k >= 4:
	ans += n * (n - 1) * (n - 2) * (n - 3) // 24 * 9
print(ans)


from math import factorial as f
(n, k) = map(int, input().split())
dp = [1, 0]
ans = 1
for i in range(2, k + 1):
	dp.append((i - 1) * (dp[-1] + dp[-2]))
	ans += f(n) // f(i) // f(n - i) * dp[i]
print(ans)


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
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')
ALPHA = 'abcdefghijklmnopqrstuvwxyz/'
M = 1000000007
EPS = 1e-06

def Ceil(a, b):
	return a // b + int(a % b > 0)

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]

def nCr(n, r):
	(p, k) = (1, 1)
	r = min(n - r, r)
	if r != 0:
		while r:
			p *= n
			k *= r
			m = gcd(p, k)
			p //= m
			k //= m
			n -= 1
			r -= 1
	else:
		p = 1
	return p
(n, k) = value()
C = {0: 1, 1: 0, 2: 1, 3: 2, 4: 9}
ans = 0
for i in range(k + 1):
	ans += nCr(n, i) * C[i]
print(ans)


(n, k) = map(int, input().split())
sum = 0
if k >= 0:
	sum += 1
if k >= 2:
	pres = n * (n - 1) / 2
	sum += pres
if k >= 3:
	pres = n * (n - 1) * (n - 2) / 6
	sum += pres * 2
if k >= 4:
	pres = n * (n - 1) * (n - 2) * (n - 3) / 24
	sum += pres * (3 * 3)
print(int(sum))


(N, K) = map(int, input().split())
print(sum([1, 0, N * (N - 1) // 2, N * (N - 1) * (N - 2) // 3, N * (N - 1) * (N - 2) * (N - 3) * 3 // 8][:K + 1]))


(n, k) = map(int, input().split())
res = 1
if k >= 2:
	res += n * (n - 1) / 2
if k >= 3:
	res += n * (n - 1) * (n - 2) / 3
if k >= 4:
	res += n * (n - 1) * (n - 2) * (n - 3) / 24 * 9
print(int(res))


import sys, math, queue, bisect
MOD = 10 ** 9 + 7
sys.setrecursionlimit(1000000)
(n, k) = map(int, input().split())
if k == 1:
	print(1)
elif k == 2:
	ans = 1 + n * (n - 1) // 2
	print(ans)
elif k == 3:
	ans = 1 + n * (n - 1) // 2 + n * (n - 1) * (n - 2) // 3
	print(ans)
else:
	ans = 1 + n * (n - 1) // 2 + n * (n - 1) * (n - 2) // 3 + 9 * (n * (n - 1) * (n - 2) * (n - 3) // 24)
	print(ans)


d = input()
n = int(d.split(' ')[0])
k = int(d.split(' ')[1])

def c(a, b):
	s = 1
	for i in range(b):
		s *= a - i
		s /= i + 1
	return int(s)
dizi = [-1, 0, 1, 2, 9]
son = 0
for i in range(k, 1, -1):
	son += c(n, i) * dizi[i]
print(son + 1)


(n, k) = map(int, input().split())
p = [0, 1, 1, 2, 9]
f = [0, 1, 2, 6, 24]
ans = 0
for i in range(n - k, n - 1):
	m = 1
	for j in range(i + 1, n + 1):
		m *= j
	add = m // f[n - i] * p[n - i]
	ans += add
print(ans + 1)


from math import factorial as f

def cnk(n, k):
	x = int(f(n) // (f(k) * f(n - k)))
	return x
(n, k) = map(int, input().split())
acc = 0
if k >= 1:
	acc += 1
if k >= 2:
	acc += cnk(n, 2)
if k >= 3:
	acc += cnk(n, 3) * 2
if k == 4:
	acc += cnk(n, 4) * 9
print(acc)


def notinx(x):
	if x == 0:
		return 1
	if x == 1:
		return 0
	fac = [1]
	for i in range(x):
		fac.append(fac[-1] * (i + 1))
	ans = 0
	for i in range(x):
		ans += fac[x] / fac[i + 1] * pow(-1, i)
	ans = fac[x] - ans
	return ans

def nCr(n, r):
	(up, down) = (1, 1)
	for i in range(r):
		down *= i + 1
		up *= n - i
	return up / down

def solve(n, k):
	ans = 0
	for i in range(k + 1):
		ans += nCr(n, i) * notinx(i)
	return ans
(n, k) = map(int, input().split())
print(int(solve(n, k)))


def C(n, k):
	result = 1
	for i in range(n - k + 1, n + 1):
		result *= i
	for i in range(2, k + 1):
		result //= i
	return result
(n, k) = map(int, input().split())
derangements = {2: 1, 3: 2, 4: 9}
result = 1
for (a, n_derang) in derangements.items():
	if a <= k:
		result += C(n, a) * n_derang
print(result)


import math

def ncr(n, r):
	f = math.factorial
	return f(n) // f(r) // f(n - r)
(n, k) = map(int, input().split())
if n == k:
	print(24)
elif k == 1:
	print(1)
elif k == 2:
	print(1 + ncr(n, 2))
elif k == 3:
	print(1 + ncr(n, 2) + ncr(n, 3) * 2)
elif k == 4:
	print(1 + ncr(n, 2) + ncr(n, 3) * 2 + ncr(n, 4) * 9)


(n, k) = map(int, input().split())
a = [0, 0, 1, 2, 9]
s = 0
for i in range(1, k + 1):
	x = 1
	y = 1
	p = n
	for j in range(1, i + 1):
		x = x * j
		y = y * p
		p -= 1
	s += y // x * a[i]
print(s + 1)


(n, k) = map(int, input().split())
answer = 0
if k > 1:
	answer += n * (n - 1) // 2
if k > 2:
	answer += n * (n - 1) * (n - 2) // 3
if k > 3:
	answer += n * (n - 1) * (n - 2) * (n - 3) // 8 * 3
print(int(answer + 1))


import sys
(n, k) = (int(i) for i in input().split())
ff = [1] * (n + 1)
for i in range(1, n + 1):
	ff[i] = ff[i - 1] * i
dd = [0] * (n + 1)
dd[1] = 0
dd[2] = 1
for i in range(3, n + 1):
	dd[i] = (i - 1) * (dd[i - 1] + dd[i - 2])
ans = ff[n]
for i in range(n - k):
	c = ff[n] // ff[n - i]
	c = c // ff[i]
	c = c * dd[n - i]
	ans -= c
print(ans)


(n, k) = map(int, input().split())
c = lambda i: c(i - 1) * (n - i + 1) // i if i else 1
print(sum([1, c(2), 2 * c(3), 9 * c(4)][:k]))


(n, k) = map(int, input().split())
ar = []
ar.append(1)
for i in range(1, n + 1):
	ar.append(ar[i - 1] * i)
k = n - k
ans = 0
br = []
br.append(1)
br.append(0)
for i in range(2, n + 1):
	br.append((i - 1) * (br[i - 1] + br[i - 2]))
for i in range(k, n + 1):
	ans = ans + ar[n] // (ar[i] * ar[n - i]) * br[n - i]
print(ans)


c = 1
(n, k) = map(int, input().split())
if k >= 2:
	tmp = 1
	for i in range(3, n + 1):
		tmp *= i
	for i in range(2, n - 1):
		tmp = tmp // i
	c += tmp
if k >= 3:
	tmp = 1
	for i in range(4, n + 1):
		tmp *= i
	for i in range(2, n - 2):
		tmp = tmp // i
	c += tmp * 2
if k == 4:
	tmp = 1
	for i in range(5, n + 1):
		tmp *= i
	for i in range(2, n - 3):
		tmp = tmp // i
	c += tmp * 9
print(c)


(n, k) = [int(x) for x in input().split(' ')]
ans = 1
if k >= 2:
	ans += n * (n - 1) // 2
if k >= 3:
	ans += n * (n - 1) * (n - 2) // 3
if k >= 4:
	ans += 9 * (n * (n - 1) * (n - 2) * (n - 3)) // 24
print(ans)


(n, k) = map(int, input().split())
ans = 1
ct = n
spoils = [0, 1, 1, 2, 9]
for i in range(2, k + 1):
	ct = ct * (n - i + 1) // i
	ans += ct * spoils[i]
print(ans)


import math
(n, k) = map(int, input().split(' '))

def combination(n, r):
	npr = math.factorial(n) // math.factorial(r)
	ncr = npr / math.factorial(n - r)
	return int(ncr)
dp = [1, 0, 1, 2, 9]
tem = 0
for _ in range(k + 1):
	tem += dp[_] * combination(n, _)
print(tem)


(n, k) = map(int, input().split())
answer = 1
if k > 1:
	answer += n * (n - 1) / 2
if k > 2:
	answer += n * (n - 1) * (n - 2) / 3
if k > 3:
	answer += n * (n - 1) * (n - 2) * (n - 3) / 24 * 9
print(int(answer))


from math import factorial as fact

def C(n, k):
	return fact(n) // (fact(k) * fact(n - k))
(n, k) = map(int, input().split())
sum = 0
for i in range(k + 1):
	if i == 0:
		sum += C(n, i)
	if i == 2:
		sum += C(n, i) * 1
	if i == 3:
		sum += C(n, i) * 2
	if i == 4:
		sum += C(n, i) * 9
print(sum)


import sys

def main():
	args = sys.stdin.readline()[:-1].split()
	(n, k) = (int(args[0]), int(args[1]))
	a = 1
	b = n * (n - 1) // 2
	c = n * (n - 1) * (n - 2) // 6 * 2
	d = n * (n - 1) * (n - 2) * (n - 3) // 24 * 9
	if k == 1:
		print(a)
	if k == 2:
		print(a + b)
	if k == 3:
		print(a + b + c)
	if k == 4:
		print(a + b + c + d)
main()


import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
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')
(n, k) = map(int, input().split())
comb = n
d = 1
s = 0
fact = 1
ans = 0
for i in range(k):
	ans += comb * (fact * s)
	comb *= n - i - 1
	comb = comb // (d + 1)
	d += 1
	s += (-1) ** i * Fraction(1, fact * (i + 2))
	fact *= i + 2
print(ans + 1)


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

def f(n):
	if n == 1:
		return 0
	if n == 2:
		return 1
	return (n - 1) * (f(n - 1) + f(n - 2))

def g(n, k):
	res = 1
	for i in range(1, k + 1):
		res *= n - i + 1
		res //= i
	return res
res = 1
for i in range(1, k + 1):
	res += g(n, i) * f(i)
print(res)


(a, b) = map(int, input().split())
s = 1
if b >= 2:
	s += a * (a - 1) >> 1
if b >= 3:
	s += a * (a - 1) * (a - 2) / 3
if b == 4:
	s += 3 * a * (a - 1) * (a - 2) * (a - 3) >> 3
print(int(s))


from math import factorial as fac
pc = {3: 2, 2: 1, 4: 9}
(n, k) = [int(x) for x in input().split()]
good = 0
if k >= 2:
	cp = int(fac(n) / (fac(2) * fac(n - 2)))
	good += cp * pc[2]
if k >= 3:
	cp = int(fac(n) / (fac(3) * fac(n - 3)))
	good += cp * pc[3]
if k == 4:
	cp = int(fac(n) / (fac(4) * fac(n - 4)))
	good += cp * pc[4]
print(good + 1)


import operator as op
from functools import reduce
from itertools import permutations

def C(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom
(n, k) = map(int, input().split())
d = [1]
for i in range(1, k + 1):
	p = [j for j in range(i)]
	x = 0
	for q in permutations(p):
		valid = True
		for j in range(len(q)):
			if j == q[j]:
				valid = False
		if valid:
			x += 1
	d.append(x)
answer = 0
for i in range(k + 1):
	answer += C(n, i) * d[i]
print(answer)


import sys

def gint():
	return int(input())

def read():
	return map(int, input().split())
(n, k) = read()
res = 1
if k >= 2:
	res += n * (n - 1) // 2
if k >= 3:
	res += n * (n - 1) * (n - 2) // 3
if k >= 4:
	res += n * (n - 1) * (n - 2) * (n - 3) * 3 // 8
print(res)


from math import factorial as f
(n, k) = map(int, input().split())
an = 0
dr = [0, 0, 1, 2, 9]
for i in range(n - k, n + 1):
	r = n - i
	if r == 0:
		an += 1
	elif r != 1:
		an += dr[r] * f(n) // (f(i) * f(n - i))
print(an)


import math
(n, k) = map(int, input().split())
der = [0 for i in range(1000 + 1)]
der[1] = 0
der[0] = 1
for i in range(2, n + 1):
	der[i] = (i - 1) * (der[i - 1] + der[i - 2])
ans = 0
for t in range(n - k, n + 1):
	bin = math.factorial(n) // (math.factorial(t) * math.factorial(n - t))
	ans += bin * der[n - t]
print(ans)


import math

def der(n):
	sm = 0
	p = 1
	for i in range(0, n + 1):
		sm += p * (math.factorial(n) // math.factorial(i))
		p = p * -1
	return sm

def nCr(n, r):
	f = math.factorial
	return f(n) // (f(r) * f(n - r))
(n, k) = (int(i) for i in input().split())
ans = 0
for i in range(0, k + 1):
	ans += nCr(n, i) * der(i)
print(ans)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
from collections import defaultdict
from itertools import permutations
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')
(n, k) = map(int, input().split())
r = 1
for i in range(2, k + 1):
	f = math.factorial(i)
	d = 0
	temp = 2
	ch = 1
	for j in range(2, n + 1):
		d += ch * (f // temp)
		temp *= j + 1
		ch = -1 * ch
	d2 = 1
	for j in range(2, i + 1):
		d2 *= j
	d1 = 1
	for j in range(n - i + 1, n + 1):
		d1 *= j
	r += d1 * d // d2
print(r)


def bexpans(n, p):
	if p == 0:
		return 1
	else:
		return n * bexpans(n - 1, p - 1) // p
s = [1, 0, 1, 2, 9]
(n, k) = map(int, input().split())
res = sum((bexpans(n, i) * s[i] for i in range(k + 1)))
print(res)


def factorial(k):
	ans = 1
	for i in range(1, k + 1):
		ans *= i
	return ans
(n, k) = list(map(int, input().split()))
ans = 0

def A(n, m):
	ans = 1
	for x in range(m + 1, n + 1):
		ans *= x
	return ans
for i in range(2, k + 1):
	si = 0
	for j in range(2, i + 1):
		coeff = -1 if j % 2 else 1
		si += coeff / factorial(j)
	ans += A(n, n - i) * si
print(int(ans + 1.5))


(a, b) = map(int, input().split())
ans = 1
p = 1
aq = [1, 0, 1, 2, 9]
for i in range(1, b + 1):
	for j in range(min(a - i, i)):
		p *= (a - j) / (j + 1)
	ans += p * aq[i]
	p = 1
print(int(ans))


from math import factorial as f
from itertools import permutations
(n, k) = map(int, input().split())

def nCk(k):
	return f(n) // (f(k) * f(n - k))

def isBad(a):
	for (i, val) in enumerate(a):
		if i == val:
			return False
	return True
ret = 0
for cnt in range(k + 1):
	ways = nCk(cnt)
	badCnt = 0
	for p in permutations(range(cnt)):
		badCnt += isBad(p)
	ret += ways * badCnt
print(ret)


import sys
input = sys.stdin.readline

def fast_factorial(mod=None):
	res = [1, 1]
	size = 2

	def ff(k):
		nonlocal size
		while k >= size:
			res.append(res[-1] * size)
			size += 1
		return res[k]

	def ff_mod(k):
		nonlocal size
		while k >= size:
			res.append(res[-1] * (size % mod) % mod)
			size += 1
		return res[k]
	if mod == None:
		return ff
	else:
		return ff_mod
ff = fast_factorial()

def nCk(n, k):
	return ff(n) // (ff(k) * ff(n - k))
(n, k) = map(int, input().split())

def remain(n):
	nums = [-1] * n
	res = 0

	def solve(i):
		nonlocal nums, res
		if i == n:
			res += 1
			return
		for j in range(n):
			if i != j and (not j in nums):
				nums[i] = j
				solve(i + 1)
				nums[i] = -1
	solve(0)
	return res
res = 1
for c in range(n - k, n - 1):
	num = nCk(n, c) * remain(n - c)
	res += num
print(res)


import sys
ls = sys.stdin.readline().split(' ')
n = int(ls[0])
k = int(ls[1])
if k == 1:
	print(str(1))
elif k == 2:
	print(str(int(1 + n * (n - 1) / 2)))
elif k == 3:
	print(str(int(1 + n * (n - 1) / 2 + n * (n - 1) * (n - 2) / 3)))
elif k == 4:
	print(str(int(1 + n * (n - 1) / 2 + n * (n - 1) * (n - 2) / 3 + 3 * n * (n - 1) * (n - 2) * (n - 3) / 8)))


from sys import stdin, stdout
import math
nmbr = lambda : int(input())
lst = lambda : list(map(int, input().split()))

def ncr(n, r):
	lim = min(r, n - r)
	(num, den) = (n, 1)
	nn = dd = 1
	for i in range(lim):
		nn *= num
		dd *= den
		num -= 1
		den += 1
	return nn // dd
for _ in range(1):
	(n, k) = lst()
	dp = [0] * (1 + n)
	dp[1] = 0
	dp[2] = 1
	for i in range(3, n + 1):
		dp[i] = (i - 1) * (dp[i - 1] + dp[i - 2])
	sm = p = 0
	while p <= k:
		sm += ncr(n, p) * dp[p]
		p += 1
	print(sm + 1)


(n, k) = map(int, input().split())
if k == 1:
	print(1)
elif k == 2:
	print(1 + n * (n - 1) // 2)
elif k == 3:
	print(1 + n * (n - 1) * (n - 2) // 3 + n * (n - 1) // 2)
else:
	print(1 + n * (n - 1) // 2 + n * (n - 1) * (n - 2) // 3 + 3 * n * (n - 1) * (n - 2) * (n - 3) // 8)


def C(n, k):
	res = 1
	for i in range(1, k + 1):
		res *= n - i + 1
	for i in range(2, k + 1):
		res //= i
	return res
(n, k) = map(int, input().split())
ans = 1
val = {1: 1, 2: 2, 3: 9}
for i in range(k, 1, -1):
	ans += val[i - 1] * C(n, i)
print(ans)


import math

def comb(x, y):
	return math.factorial(x) // (math.factorial(x - y) * math.factorial(y))
s = input().split()
n = int(s[0])
k = int(s[1])

def Count_ALP_C(n, k):
	count = 0
	for m in range(k + 1):
		count += comb(n, m) * d(m)
	return count

def d(m):
	if m == 0:
		return 1
	if m == 1:
		return 0
	if m == 2:
		return 1
	if m == 3:
		return 2
	if m == 4:
		return 9
print(Count_ALP_C(n, k))


(n, k) = [int(x) for x in input().split()]
if k == 1:
	print(1)
elif k == 2:
	print(int(n * (n - 1) / 2 + 1))
elif k == 3:
	print(int(n * (n - 1) * (n - 2) / 3 + n * (n - 1) / 2 + 1))
elif k == 4:
	print(int(3 * n * (n - 1) * (n - 2) * (n - 3) / 8 + n * (n - 1) * (n - 2) / 3 + n * (n - 1) / 2 + 1))


from sys import stdin, stdout
import math

def c(n, r):
	return math.factorial(n) // math.factorial(n - r) // math.factorial(r)
I = stdin.readline
P = stdout.write
(n, k) = map(int, I().split())
ans = 1
if k >= 2:
	ans += c(n, 2)
if k >= 3:
	ans += 2 * c(n, 3)
if k == 4:
	ans += 9 * c(n, 4)
P(str(ans))


inf = lambda : map(int, input().split())
(n, k) = inf()
ans = 1
c = n * (n - 1) // 2
if k >= 2:
	ans += c
	c = c * (n - 2) // 3
	if k >= 3:
		ans += c * 2
		c = c * (n - 3) // 4
		if k >= 4:
			ans += c * 9
print(ans)


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

def derangement(n):
	if n == 0 or n == 2:
		return 1
	if n == 1:
		return 0
	return (n - 1) * (derangement(n - 1) + derangement(n - 2))
f = [derangement(i) for i in range(k + 1)]

def C(n, m):
	z = 1
	for i in range(m):
		z = z * (n - i) // (i + 1)
	return z
ans = 0
for i in range(k + 1):
	ans += C(n, i) * f[i]
print(ans)


(n, k) = [int(x) for x in input().split()]

def bikkuri(n):
	ans = 1
	for i in range(1, n + 1):
		ans *= i
	return ans

def combination(n, r):
	return int(bikkuri(n) / (bikkuri(r) * bikkuri(n - r)))
q = [0, 0, 1]
for i in range(3, k + 1):
	q__ = q[i - 2]
	q_ = q[i - 1]
	qq = (i - 1) * (q__ + q_)
	q.append(qq)
ans = 1
for i in range(1, k + 1):
	ans += combination(n, i) * q[i]
print(ans)


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def ii():
	return int(input())

def si():
	return input()

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

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(1000000)
	threading.stack_size(1024000)
	thread = threading.Thread(target=main)
	thread.start()
import sys
input = sys.stdin.readline
scanner = lambda : int(input())
string = lambda : input().rstrip()
get_list = lambda : list(read())
read = lambda : map(int, input().split())
get_float = lambda : map(float, input().split())

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def factorial(x, m):
	val = 1
	while x > 0:
		val = val * x % m
		x -= 1
	return val

def fact(x):
	val = 1
	while x > 0:
		val *= x
		x -= 1
	return val

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // math.gcd(a, b)

def is_integer(n):
	return math.ceil(n) == math.floor(n)

def nCr(n, k):
	if k > n:
		return 0
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	if y == 0:
		return 1
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	(prime[0], prime[1]) = (False, False)
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def phi(n):
	result = n
	p = 2
	while p * p <= n:
		if n % p == 0:
			while n % p == 0:
				n = n // p
			result = result * (1.0 - 1.0 / float(p))
		p = p + 1
	if n > 1:
		result = result * (1.0 - 1.0 / float(n))
	return int(result)

def is_prime(n):
	if n == 0:
		return False
	if n == 1:
		return True
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True

def next_prime(n, primes):
	while primes[n] != True:
		n += 1
	return n
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]

def factoriazation(x):
	res = []
	for i in range(2, int(x ** 0.5) + 1):
		while x % i == 0:
			res.append(i)
			x //= i
	if x != 1:
		res.append(x)
	return res

def factors(n):
	res = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			res.append(i)
			res.append(n // i)
	return list(set(res))

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

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

def str_array():
	return input().strip().split()

def binary_search(low, high, w, h, n):
	while low < high:
		mid = low + (high - low) // 2
		if check(mid, w, h, n):
			low = mid + 1
		else:
			high = mid
	return low

def check(moves, n):
	val = (moves + 1) // 2
	rem = moves - val
	sol = (val + 1) * (rem + 1)
	return sol < n

def sortSecond(val):
	return val[1]
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
alphs = 'abcdefghijklmnopqrstuvwxyz'
from itertools import permutations
import math
import bisect as bis
import random
import sys
import collections as collect
import functools as fnt
from decimal import Decimal

def solve():
	(n, k) = read()
	derangements = [1, 0, 1, 2, 9]
	ans = 1
	for i in range(1, k + 1):
		ans += nCr(n, i) * derangements[i]
	print(ans)
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')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = 1
for i in range(t):
	solve()


import math
R = lambda : map(int, input().split())
(n, k) = R()
cnt = [1, 0, 1, 2, 9]
res = 0
for i in range(k + 1):
	res += math.factorial(n) // (math.factorial(i) * math.factorial(n - i)) * cnt[i]
print(res)


(n, k) = map(int, input().split())
a = n * (n - 1) // 2
b = a * (n - 2) // 3
c = b * (n - 3) // 4
print(sum([1, 0, a, 2 * b, 9 * c][:k + 1]))


from math import factorial

def comb(n, r):
	if r == 1:
		return n
	elif r == 2:
		return n * (n - 1) // 2
	elif r == 3:
		return n * (n - 1) * (n - 2) // 6
	else:
		return n * (n - 1) * (n - 2) * (n - 3) // 24

def derange(n):
	if n == 1:
		return 0
	elif n == 2:
		return 1
	elif n == 3:
		return 2
	elif n == 4:
		return 9
(n, k) = map(int, input().split())
ans = 0
for i in range(1, k + 1):
	ans += comb(n, i) * int(derange(i))
print(ans + 1)


def fall_fac(n, k):
	ans = 1
	for i in range(k):
		ans *= n - i
	return ans

def C(n, k):
	if not 0 <= k <= n:
		return 0
	else:
		return fall_fac(n, k) // fall_fac(k, k)

def derangement(n):
	if n == 0:
		return 1
	elif n == 1:
		return 0
	else:
		return (n - 1) * (derangement(n - 1) + derangement(n - 2))
(n, k) = [int(x) for x in input().split()]
print(sum([C(n, i) * derangement(i) for i in range(k + 1)]))


from math import factorial as fact
(n, k) = map(int, input().split())
ans = 1
while k != 1:
	if k == 4:
		ans += 9 * (fact(n) // (fact(k) * fact(n - k)))
	elif k == 3:
		ans += 2 * (fact(n) // (fact(k) * fact(n - k)))
	else:
		ans += fact(n) // (fact(k) * fact(n - k))
	k -= 1
print(ans)


(n, k) = map(int, input().split())
ans = [1, 0]
ans.append(n * (n - 1) // 2)
ans.append(n * (n - 1) * (n - 2) // 3)
ans.append(n * (n - 1) * (n - 2) * (n - 3) * 3 // 8)
print(sum(ans[:k + 1]))


import math

def derange(n):
	return int(math.factorial(n) * sum([(-1) ** i / math.factorial(i) for i in range(n + 1)]))

def choose(n, k):
	ans = 1
	if n - k < k:
		k = n - k
	for i in range(k):
		ans *= n - i
	ans /= math.factorial(k)
	return int(ans)

def almostIdent(n, k):
	ans = 0
	for i in range(k + 1):
		ans += choose(n, n - i) * derange(i)
	return ans
s = input().split(' ')
n = int(s[0])
k = int(s[1])
print(almostIdent(n, k))


import math
from sys import stdin
string = stdin.readline().strip().split()
n = int(string[0])
k = int(string[1])

def factorial(N):
	result = 1
	for i in range(1, N + 1):
		result *= i
	return result
output = 1
for i in range(2, k + 1):
	if i == 2:
		output = output + factorial(n) / factorial(n - 2) / 2
	if i == 3:
		output = output + 2 * (factorial(n) / factorial(n - 3) / 6)
	if i == 4:
		output = output + 9 * (factorial(n) / factorial(n - 4) / 24)
print(int(output))


from math import factorial

def c(n, k):
	return factorial(n) / (factorial(n - k) * factorial(k))
ar = [1, 1, 2, 9]
(n, k) = map(int, input().split())
if k == 1:
	print(1)
else:
	ans = 1
	for i in range(2, k + 1):
		ans += c(n, i) * ar[i - 1]
	print(int(ans))


(n, k) = list(map(int, input().split()))
if k == 1:
	print(1)
elif k == 2:
	print(n * (n - 1) // 2 + 1)
elif k == 3:
	print(n * (n - 1) * (n - 2) // 3 + n * (n - 1) // 2 + 1)
elif k == 4:
	print(n * (n - 1) * (n - 2) * (n - 3) * 9 // 24 + n * (n - 1) * (n - 2) // 3 + n * (n - 1) // 2 + 1)


import sys
import math

def derange(k):
	ans = math.factorial(k)
	z = -1
	mult = 1
	for i in range(1, k + 1):
		mult += z * (1 / math.factorial(i))
		z *= -1
	ans = ans * mult
	return int(ans)

def com(n, k):
	if k == 0:
		return 1
	if k == 1:
		return n
	if k == 2:
		return n * (n - 1) // 2
	if k == 3:
		return n * (n - 1) * (n - 2) // 6
	return n * (n - 1) * (n - 2) * (n - 3) // 24
(n, k) = map(int, sys.stdin.readline().split())
ans = 0
for i in range(k + 1):
	x = com(n, i) * derange(i)
	ans += x
print(ans)