×

# NOWAYS Editorial

Author and Editorialist : Arun Prasad geek_geek

Easy

# PREREQUISITES:

Modular Arithmetic,Combinatorics,Repeated Squaring

# PROBLEM

Given n items , find number of ways to choose k different items.

# QUICK EXPLANATION

You have to find the value of nCk it will give you number of ways of choosing k items from n items print nCk % mod (mod = 1000000007)

# EXPLANATION

nck = (n!)/((n-k)! * k!)

now you cannot store n! in an array as it can be very big instead you can store n!%mod in an array since mod is a prime number you have to use fermat's theorem to calculate the value of modular inverse of ((k!)%mod) and ((n-k)!%mod)

store the value of n!%mod in an array , and calculate inverse factorial for each n by using the formula

inv_factorial(n) = exp(n,mod-2,mod) (use repeated squaring for faster answers)

store all in an array

if n is greater tha k : nCk = (fact(n)inv_fact(n-k)inv_fact(k))%mod

if n is lesser than k

You cannot choose k items from n if k is greater than n

# Solutions : Author's Solution

This question is marked "community wiki".

43914
accept rate: 16%

 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×15,499
×3,710
×6
×3
×3

question asked: 29 Jun '15, 18:40

question was seen: 1,242 times

last updated: 29 Jun '15, 19:03