Given a number N , calculate total number of permutations of it and also the sum of all permutations including that number itself.
 
Example 1:
Input: N = 5
Output: 1 5
Explanation: There is only one permutation
of 5 and sum of this permutaion is 5.
Example 2:
Input: N = 12
Output: 2 33
Explanation: Permutations of 12 are 12 and 21.
Sum of these permutations are = 12 + 21 = 33.
 
Your Task:
You don't need to read or print anyhting. Your task is to complete the function permutation() which takes N as input parameter ans returns a list that contains total number of permutations and sum of all these permutations including the number iteself.
 
Expected Time Complexity: O(K!) where K = log_{10}N
Expected Space Complexity: O(1)
Constraints:
1 <= N <= 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Combinatorics

Mathematics

Starter code: #User function Template for python3

class Solution:
	def permutaion(self, N):
		# Code here

Solutions:
class Solution:

	def permutaion(self, N):
		cnt = 1
		dig_sum = 0
		n = N
		n1 = N
		map1 = {}
		while N > 9:
			N = N // 10
			cnt += 1
		no_dig = cnt
		no_dig1 = cnt
		perm = 1
		perm = self.factorial(cnt)
		sum1 = 0
		x = perm // no_dig
		while no_dig > 0:
			dig = n // 10 ** (no_dig - 1)
			if dig not in map1:
				map1[dig] = 1
			else:
				map1[dig] += 1
			sum1 += dig * x
			n = n % 10 ** (no_dig - 1)
			no_dig -= 1
		no = 1
		while no_dig1 > 1:
			no = no + 10 ** (no_dig1 - 1)
			no_dig1 -= 1
		final_sum = sum1 * no
		for items in map1.values():
			M = self.factorial(items)
			perm = perm // M
			final_sum = final_sum // M
		return [perm, final_sum]

	def factorial(self, no):
		ans = 1
		while no > 0:
			ans = ans * no
			no -= 1
		return ans


class Solution:

	def permutaion(self, N):
		b = []
		from itertools import permutations
		perms = list(permutations(str(N)))
		dic = {}
		for i in perms:
			if i not in dic:
				b.append(int(''.join(i)))
				dic[i] = 1
		ans = []
		ans.append(len(b))
		ans.append(sum(b))
		return ans


class Solution:

	def permutaion(self, nums):
		s = ''
		c = 0
		su = []
		k = str(nums)
		n = len(k)
		f = [0] * n

		def per(n, f, k, s, su, c):
			if len(s) == n:
				if int(s) not in su:
					su.append(int(s))
				return
			for i in range(n):
				if f[i] == 0:
					f[i] = 1
					s += k[i]
					per(n, f, k, s, su, c)
					f[i] = 0
					s = s[:-1]
		per(n, f, k, s, su, c)
		return (len(su), sum(su))


class Solution:

	def permutaion(self, n):

		def convert_list_of_string_nums_to_nums(list_of_nums):
			ans_num = []
			for num in list_of_nums:
				ans_num.append(int(num))
			return ans_num

		def permute(nums):
			if len(nums) == 0:
				return []
			if len(nums) == 1:
				return [nums]
			all_permutations = []
			for (i, each_num) in enumerate(nums):
				rem_nums = nums[:i] + nums[i + 1:]
				for each_permutation in permute(rem_nums):
					all_permutations.append(each_num + each_permutation)
			return all_permutations
		all_permutes = sorted(set(permute(str(n))))
		all_permutes = convert_list_of_string_nums_to_nums(all_permutes)
		return [len(all_permutes), sum(all_permutes)]


class Solution:

	def permutaion(self, n):
		N = str(n)

		def rec(s):
			if len(s) == 1:
				return [s]
			ret = []
			per = rec(s[1:])
			for p in per:
				for i in range(len(p) + 1):
					ret.append(p[:i] + s[0] + p[i:])
			return ret
		ans = set(rec(N))
		return (len(ans), sum([int(s) for s in ans]))


from itertools import permutations

class Solution:

	def permutaion(self, N):
		sum = 0
		pmt = [''.join(p) for p in permutations(str(N))]
		pmt = set(pmt)
		for i in pmt:
			sum += int(i)
		return [len(pmt), sum]


from itertools import permutations

class Solution:

	def permutaion(self, N):
		N = str(N)
		perm = permutations(N)
		perm = sorted(perm)
		(result, output) = ([], [])
		for i in perm:
			i_ = ''.join(i)
			result.append(i_)
		result = sorted(list(set(result)))
		output.append(len(result))
		c = 0
		for i in result:
			c += int(i)
		output.append(c)
		return output


class Solution:

	def perm(self, lst):
		n = len(lst)
		if n == 0:
			return []
		elif n == 1:
			return [lst]
		res = []
		for i in range(n):
			ele = lst[i]
			remlst = lst[:i] + lst[i + 1:]
			for j in self.perm(remlst):
				res.append([ele] + j)
		return res

	def permutaion(self, N):
		st = str(N)
		arr = list(map(int, st))
		if len(set(arr)) == 1:
			return [1, N]
		l = []
		l = self.perm(arr)
		sum = 0
		v = set()
		for lst in l:
			s = ''
			for number in lst:
				st = str(number)
				s = s + st
			v.add(s)
		for item in v:
			sum = sum + int(item)
		return [len(v), sum]


class Solution:

	def nextp(self, l, c, b):
		if c == len(l) - 1:
			b.append(l[:])
			return
		for i in range(c, len(l)):
			(l[i], l[c]) = (l[c], l[i])
			self.nextp(l, c + 1, b)
			(l[i], l[c]) = (l[c], l[i])

	def permutaion(self, N):
		l = []
		s = {}
		y = 0
		while N != 0:
			g = str(N % 10)
			l.append(g)
			if g in s:
				s[g] += 1
			else:
				s[g] = 1
			y += 1
			N = N // 10
		l.sort()
		m = {}
		p = 1
		for i in range(1, y + 1):
			p = p * i
			m[i] = p
		for i in s:
			p = p // m[s[i]]
		b = []
		self.nextp(l, 0, b)
		h = 0
		t = {}
		for i in b:
			d = ''.join(i)
			if d not in t:
				t[d] = 1
				h += int(d)
		return [p, h]


class Solution:

	def permutaion(self, N):

		def helper(s, ans, l):
			if len(s) == 0:
				l.append(int(ans))
				return
			for i in range(len(s)):
				ch = s[i]
				rest = s[0:i] + s[i + 1:]
				helper(rest, ans + ch, l)
		s = str(N)
		l = []
		ans = ''
		helper(s, ans, l)
		l = set(l)
		return [len(l), sum(l)]
if __name__ == '__main__':
	T = int(input())
	for i in range(T):
		N = int(input())
		ob = Solution()
		ans = ob.permutaion(N)
		for _ in ans:
			print(_, end=' ')
		print()


class Solution:

	def permutaion(self, N):
		res = set()

		def rec(s, l, r):
			if l == r:
				res.add(int(''.join(s)))
			else:
				for i in range(l, r):
					(s[l], s[i]) = (s[i], s[l])
					rec(s, l + 1, r)
					(s[l], s[i]) = (s[i], s[l])
		rec(list(str(N)), 0, len(str(N)))
		return (len(res), sum(res))
if __name__ == '__main__':
	T = int(input())
	for i in range(T):
		N = int(input())
		ob = Solution()
		ans = ob.permutaion(N)
		for _ in ans:
			print(_, end=' ')
		print()


import copy

class Solution:

	def formnumber(self, N):
		p = 0
		number = 0
		for i in range(len(N) - 1, -1, -1):
			number += N[i] * 10 ** p
			p += 1
		return number

	def permutaion(self, N):
		p = set()
		digits = []
		while N > 0:
			lastdigit = N % 10
			digits.append(lastdigit)
			N //= 10
		freqmap = [False for _ in range(len(digits))]

		def permute(current):
			if len(current) == len(digits):
				x = copy.deepcopy(current)
				x = self.formnumber(x)
				p.add(x)
				return
			for i in range(len(digits)):
				if freqmap[i] is True:
					continue
				freqmap[i] = True
				current.append(digits[i])
				permute(current)
				current.pop()
				freqmap[i] = False
			return
		permute([])
		sm = 0
		sz = len(p)
		for item in p:
			sm += item
		return (sz, sm)
if __name__ == '__main__':
	T = int(input())
	for i in range(T):
		N = int(input())
		ob = Solution()
		ans = ob.permutaion(N)
		for _ in ans:
			print(_, end=' ')
		print()


class Solution:

	def permutaion(self, N):
		seen = set()
		taken = set()
		s = 0
		cnt = 0
		arr = []
		while N > 0:
			arr.append(N % 10)
			N //= 10

		def solve(cur, l):
			nonlocal seen, taken, s, cnt
			if l == len(arr):
				if cur not in seen:
					seen.add(cur)
					cnt += 1
					s += cur
				else:
					return
			for i in range(len(arr)):
				if i not in taken:
					taken.add(i)
					solve(cur * 10 + arr[i], l + 1)
					taken.remove(i)
		solve(0, 0)
		return [cnt, s]
if __name__ == '__main__':
	T = int(input())
	for i in range(T):
		N = int(input())
		ob = Solution()
		ans = ob.permutaion(N)
		for _ in ans:
			print(_, end=' ')
		print()


from itertools import permutations as perm

class Solution:

	def permutaion(self, N):
		x = N
		s = []
		while x / 10 > 0:
			s.append(x % 10)
			x = x // 10
		x = perm(s)
		ans = set()
		for ele in x:
			s = [str(integer) for integer in ele]
			a_string = ''.join(s)
			res = int(a_string)
			ans.add(res)
		return [len(ans), sum(ans)]


from itertools import permutations

class Solution:

	def permutaion(self, N):
		arr = [i for i in str(N)]
		p = list(set(permutations(arr)))
		t = []
		for i in p:
			s = ''.join(i)
			t.append(int(s))
		return (len(p), sum(t))


from itertools import permutations

class Solution:

	def permutaion(self, N):
		p = str(N)
		l = list(permutations(p, len(p)))
		res = set((''.join(tups) for tups in l))
		q = 0
		sum1 = 0
		for i in res:
			q += 1
			sum1 += int(i)
		return [q, sum1]


from itertools import permutations

class Solution:

	def permutaion(self, N):
		(output, ans) = ([], [])
		(i, s) = (0, 0)
		N = str(N)
		perm = permutations(N)
		for i in perm:
			m = ''.join(i)
			ans.append(m)
		ans = set(ans)
		output.append(len(ans))
		for i in ans:
			s += int(i)
		output.append(s)
		return output


from itertools import permutations

class Solution:

	def permutaion(self, n):
		perm = list(set(permutations(list(str(n)))))
		c = sm = 0
		for i in perm:
			x = ''.join(i)
			sm += int(x)
			c += 1
		return [c, sm]


class Solution:

	def permutaion(self, n):
		global sm, c, setx
		setx = set()
		sm = c = 0
		que = list(str(n))
		solve(que, 0, len(str(n)))
		return [c, sm]

def solve(arr, idx, n):
	global sm, c, setx
	if idx >= n:
		x = ''.join(arr)
		if x not in setx:
			sm += int(x)
			c += 1
			setx.add(x)
		return
	for i in range(idx, n):
		(arr[i], arr[idx]) = (arr[idx], arr[i])
		solve(arr, idx + 1, n)
		(arr[i], arr[idx]) = (arr[idx], arr[i])


def cal_possibilities(x):
	count = 1
	while x > 0:
		count *= x
		x -= 1
	return count

class Solution:

	def permutaion(self, N):
		str_n = str(N)
		length = len(str_n)
		unique_chars = []
		for i in range(length):
			add_new = True
			for x in range(len(unique_chars)):
				if unique_chars[x][0] == str_n[i]:
					unique_chars[x][1] += 1
					add_new = False
			if add_new:
				unique_chars.append([str_n[i], 1])
		duplicate_combinations = 1
		for i in unique_chars:
			if i[1] > 1:
				duplicate_combinations *= cal_possibilities(i[1])
		count = int(cal_possibilities(length) / duplicate_combinations)
		total = 0
		unique_len = len(unique_chars)
		for i in range(unique_len):
			char_occurance = int(count * unique_chars[i][1] / length)
			total += int(unique_chars[i][0] * length) * char_occurance
		return (count, total)


class Solution:

	def solve(self, nums, arr, ds, freq):
		if len(ds) == len(nums):
			if ds not in arr:
				arr.append(ds[:])
			return
		for i in range(len(nums)):
			if freq[i] == 0:
				ds.append(nums[i])
				freq[i] = 1
				self.solve(nums, arr, ds, freq)
				freq[i] = 0
				ds.pop()

	def permutaion(self, N):
		nums = []
		while N > 0:
			nums.append(N % 10)
			N //= 10
		arr = []
		ds = []
		flag = [0] * len(nums)
		self.solve(nums, arr, ds, flag)
		length = len(arr)
		su = 0
		for i in arr:
			y = 0
			for j in i:
				y = y * 10 + j
			su += y
		return [length, su]


from itertools import permutations

class Solution:
	ans = set()

	def fun(self, s, i):
		if i == len(s):
			Solution.ans.add(int(''.join(s)))
			return
		for idx in range(i, len(s)):
			(s[idx], s[i]) = (s[i], s[idx])
			self.fun(s, i + 1)
			(s[idx], s[i]) = (s[i], s[idx])

	def permutaion(self, N):
		Solution.ans = set()
		s = str(N)
		self.fun(list(s), 0)
		return (len(Solution.ans), sum(Solution.ans))


from itertools import permutations

class Solution:

	def permutaion(self, N):
		s = str(N)
		sm = 0
		cnt = 0
		st = set()
		for i in permutations(s):
			st.add(int(''.join(i)))
		return (len(st), sum(st))


class Solution:

	def permutaion(self, n):
		global sm, p, setx
		setx = set()
		s = list(str(n))
		l = len(s)
		sm = 0
		p = 0
		solve(s, l, 0)
		return [p, sm]

def solve(s, n, idx):
	global sm, p
	if idx == n - 1:
		st = ''.join(s)
		if st not in setx:
			setx.add(st)
			p += 1
			sm += int(st)
	for i in range(idx, n):
		swap(i, idx, s)
		solve(s, n, idx + 1)
		swap(i, idx, s)

def swap(i, idx, s):
	(s[i], s[idx]) = (s[idx], s[i])


def generate_permutations(string, answer, permutations):
	if not string:
		permutations.add(answer)
		return
	for i in range(len(string)):
		char = string[i]
		left_str = string[:i]
		right_str = string[i + 1:]
		remain_str = left_str + right_str
		generate_permutations(remain_str, answer + char, permutations)

class Solution:

	def permutaion(self, N):
		S = str(N)
		permutations = set()
		generate_permutations(S, '', permutations)
		return (len(permutations), sum(map(int, permutations)))


from itertools import permutations

class Solution:

	def permutaion(self, n):
		l = []

		def allPermutations(str):
			permList = permutations(str)
			for perm in list(permList):
				l.append(''.join(perm))
		s = 0
		allPermutations(str(n))
		for i in set(l):
			s += int(i)
		return [str(len(set(l))), str(s)]


class Solution:

	def rec(self, index, arr, n, summ, cnt, ans):
		if index == n:
			s = ''
			s = s.join([str(i) for i in arr])
			if s not in ans:
				ans.append(s)
		for i in range(index, n):
			(arr[index], arr[i]) = (arr[i], arr[index])
			self.rec(index + 1, arr, n, summ, cnt, ans)
			(arr[index], arr[i]) = (arr[i], arr[index])

	def permutaion(self, N):
		arr = list(map(int, str(N)))
		n = len(arr)
		cnt = 0
		summ = 0
		ans = []
		self.rec(0, arr, n, summ, cnt, ans)
		for i in ans:
			summ += int(i)
			cnt += 1
		return (cnt, summ)


class Solution:

	def solve(self, arr, n, ind, ans):
		if ind == n:
			ans.append(''.join(arr))
			return
		for i in range(ind, n):
			(arr[i], arr[ind]) = (arr[ind], arr[i])
			self.solve(arr, n, ind + 1, ans)
			(arr[i], arr[ind]) = (arr[ind], arr[i])
		return

	def permutaion(self, n):
		ans = []
		self.solve(list(str(n)), len(str(n)), 0, ans)
		ans.sort()
		l = [ans[0]]
		ln = len(ans)
		c = 1
		s = int(ans[0])
		for i in range(1, ln):
			if ans[i - 1] != ans[i]:
				s += int(ans[i])
				c += 1
		return [c, s]


import functools

class Solution:

	def permute(self, nums):
		if len(nums) == 0 or len(nums) == 1:
			return [nums]
		ret = []
		for i in range(len(nums)):
			ni = nums[i]
			remaining = nums[:i] + nums[i + 1:]
			for p in self.permute(remaining):
				ret.append([ni] + p)
			unique_set = set((tuple(x) for x in ret))
			unique = [list(x) for x in unique_set]
		return unique

	def permutaion(self, N):
		permute = [int(x) for x in str(N)]
		res = self.permute(permute)
		sum = 0
		for i in range(0, len(res)):
			sum += functools.reduce(lambda x, y: int(str(x) + str(y)), res[i], 0)
		return [str(len(res)), str(sum)]


class Solution:

	def permutaion(self, N):
		from itertools import permutations
		numbers = [''.join(p) for p in permutations(str(N))]
		numbers = list(map(int, numbers))
		freq = dict()
		repetitive = 1
		for digit in str(N):
			if digit not in freq:
				freq[digit] = 1
			else:
				freq[digit] += 1
				repetitive *= freq[digit]
		totalWays = len(numbers) // repetitive
		totalSum = sum(numbers) // repetitive
		return (totalWays, totalSum)