- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find number of ways we can select sequence from Ajob Sequence in Python

Suppose there is a strange language called Ajob language. It has infinite number of letters. We know n words in this language. The first word is one character long, second one is two character long and so on. And all letters in a word are unique. If we select any one of the n words and form a subsequence from it. The length of the subsequence should be k less than the length of original word. For an example, if the length of the chosen word is say L, then the length of the subsequence should be (L - k). If any word with length smaller than k, then, you must not choose that word. And two subsequences are different to each other when the lengths of them are different or they contain different characters in the same position. We have to find result modulo p, and p i a prime.

So, if the input is like n = 6, k = 5, p = 11, then the output will be 7.

To solve this, we will follow these steps −

- create one empty dictionary memo
- n := n + 1, k := k + 1
- fact := a list with one element 1
- for i in range 1 to p - 1, do
- insert (last element of fact * i mod p) at the end of fact

- if p is present in memo, then
- inv_fact := memo[p]

- otherwise,
- inv := a list with two elements 0 and 1
- for i in range 2 to p - 1, do
- insert (p - floor of p/i * inv[p mod i] mod p) at the end of inv

- inv_fact := a list with one element 1
- for i in range 1 to p - 1, do
- insert (last element of inv_fact * inv[i] mod p) at the end of inv_fact

- memo[p] := inv_fact

- ret := 1
- while n > 0, do
- n1 := n mod p
- k1 := k mod p
- if k1 > n1, then
- return 0

- ret := ret * fact[n1] * inv_fact[k1] * inv_fact[n1 - k1] mod p
- n := floor of (n/p)
- k := floor of k/p

- return ret

## Example

Let us see the following implementation to get better understanding −

memo = {} def solve(n, k, p): n += 1 k += 1 fact = [1] for i in range(1, p): fact.append(fact[-1] * i % p) if p in memo: inv_fact = memo[p] else: inv = [0, 1] for i in range(2, p): inv.append(p - p // i * inv[p % i] % p) inv_fact = [1] for i in range(1, p): inv_fact.append(inv_fact[-1] * inv[i] % p) memo[p] = inv_fact ret = 1 while n > 0: n1 = n % p k1 = k % p if k1 > n1: return 0 ret = ret * fact[n1] * inv_fact[k1] * inv_fact[n1 - k1] % p n //= p k //= p return ret n = 6 k = 5 p = 11 print(solve(n, k, p))

## Input

6, 5, 11

## Output

7

- Related Questions & Answers
- Program to check we can make arithmetic progression from sequence in Python
- Program to find removed term from arithmetic sequence in Python
- Program to find number of ways we can decode a message in Python
- Program to find number of ways we can split a palindrome in python
- Program to find number of swaps required to sort the sequence in python
- Program to find maximum k-repeating substring from sequence in Python
- Program to find number of ways we can arrange symbols to get target in Python?
- Program to find number of ways we can concatenate words to make palindromes in Python
- Program to find length of longest consecutive sequence in Python
- Program to find nth sequence after following the given string sequence rules in Python
- Program to find number of ways we can reach from top left point to bottom right point in Python
- Program to find kth lexicographic sequence from 1 to n of size k Python
- Program to find number of ways we can get n R.s using Indian denominations in Python
- Program to count number of ways we can throw n dices in Python
- Program to find sum of given sequence in C++