 # CAKP01 - Editorial

Obsession with 7

Author: Rishabh Rathi

SIMPLE-EASY

# PREREQUISITES:

Math, Permutations

# PROBLEM:

You have a number N and you need to check the divisibility of, all the numbers formed from permutations of digits of N by 7.
Print the number of distinct permutations of N that are divisible by 7.

# EXPLANATION:

Given the constraints of N were up to 10^7, it means that you can brute force to find all permutations (Maximum number of permutations = 7! = 5040). Convert it to a set (to remove duplicates).

Then you just need to loop through all the distinct elements and if the element is divisible by 7, increment the answer.

# SOLUTIONS:

Setter's Solution
``````from itertools import permutations

test = int(input())
for _ in range(test):
n = int(input())
dig = [ch for ch in str(n)]
all_perm = list(set(permutations(dig)))
ans = 0

for item in all_perm:
num = int("".join(item))
if num%7 == 0:
ans += 1

print(ans)
``````
1 Like

do the editorial for CAKP04 also.

Sure.

1 Like