Here are some of the exciting features that Python 3.8 provides for programmers in competitive programming. These new features are related to some math functions and algorithms that are frequently used by competitive programmers. The implementation of these features has the time complexity which is the same in case of a program when implemented from scratch.

We will discuss

- Modular Exponentiation
- Multiplicative Modulo Inverse
- Calculating n
_{C}_{r}and n_{P}_{r}

#### Modular Exponentiation

Given 3 numbers A, B and Mod.Calculate (A^{B})%Mod.

**Examples:**

Input :A = 4, B = 3 Mod = 11Output :9Explanation:(4^{3})%11 = (64)%11 = 9Input :A = 3, B = 3 Mod = 5Output :3Explanation:(3^{3})%5 = (27)%11 = 5

The traditional Implementation of Modular Exponentiation is discussed here

Below is the discussed Python3.8 solution

`A ` `=` `4` `B ` `=` `3` `Mod ` `=` `11` ` ` `# Power function can take 3 ` `# parameters and can compute ` `# (A ^ B)% Mod ` `print` `(f` `'The power is {pow(A, B, Mod)}'` `) ` |

*chevron_right*

*filter_none*

**Output:**

The power is 9

#### Modular Multiplicative Inverse

Given two integers A and Mod, Calculate Modular multiplicative inverse of A under modulo Mod.

The modular multiplicative inverse is an integer B such that

(A.B)%Mod = 1 where gcd(A, Mod) should be equal to 1

**Examples:**

Input :A = 4, Mod = 11Output :3Explanation:(4*3)%11 = (12)%11 = 1Input :A = 3, Mod = 5Output :2Explanation:(3*2)%5 = (6)%5 = 1

The traditional Implementation of Modular Multiplicative Inverse is discussed here

Below is the discussed Python3.8 solution

`A ` `=` `4` `Mod ` `=` `11` ` ` `# Power function can take 3 parameters ` `# and can compute (A^-1)% Mod ` `print` `(f'The Modular Multiplicative Inverse \ ` `of A under Mod ` `is` `{` `pow` `(A, ` `-` `1` `, Mod)}') ` |

*chevron_right*

*filter_none*

**Output:**

The Modular Multiplicative Inverse of A under Mod is 3

#### Calculating Ncr and Npr

Given the value of N and r. Calculate the Ncr (Combinations of N things taken r at a time) and Npr(Permutations of N things taken r at a time).

**Examples:**

Input :N = 10, r = 3Output :Ncr = 120Input :N = 10, r = 3Output :Npr = 720

The traditional implementations of Ncr and Npr are discussed here and here

Below is the discussed Python3.8 solution.

`import` `math ` `N ` `=` `10` `r ` `=` `3` ` ` `print` `(f` `"Ncr = {math.comb(N, r)}"` `) ` `print` `(f` `"Npr = {math.perm(N, r)}"` `) ` |

*chevron_right*

*filter_none*

**Output:**

Ncr = 120 Npr = 720

## Recommended Posts:

- Tips and Tricks for Competitive Programmers | Set 2 (Language to be used for Competitive Programming)
- Python Tricks for Competitive Coding
- Python Input Methods for Competitive Programming
- Python in Competitive Programming
- Which Python Modules are useful for competitive programming?
- How to Setup VSCode with C, C++ and Python for Competitive Programming
- 10 Essential Python Tips And Tricks For Programmers
- Selenium Python Tricks
- 10 Interesting Python Cool Tricks
- Jupyter notebook Tips and Tricks
- Why is python best suited for Competitive Coding?
- A basic Python Programming Challenge
- Object Oriented Programming in Python | Set 1 (Class, Object and Members)
- Object Oriented Programming in Python | Set 2 (Data Hiding and Object Printing)
- Command Line Interface Programming in Python
- Socket Programming in Python
- CGI Programming in Python
- Socket Programming with Multi-threading in Python
- Python - Fastest Growing Programming Language
- Why is Python the Best-Suited Programming Language for Machine Learning?

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.