You are not logged in. Please login at to post your questions!


NOWAYS Editorial

Problem Link : Contest Practice

Author and Editorialist : Arun Prasad geek_geek




Modular Arithmetic,Combinatorics,Repeated Squaring


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


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)


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

hence answer is 0

Solutions : Author's Solution

This question is marked "community wiki".

asked 29 Jun '15, 18:40

geek_geek's gravatar image

accept rate: 16%

edited 29 Jun '15, 19:03

toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • 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:


question asked: 29 Jun '15, 18:40

question was seen: 1,242 times

last updated: 29 Jun '15, 19:03