Binomial CoefficientProblem code: CB01 |
All submissions for this problem are available.
This is a very simple problem. Given the value of N and K, you need to tell us the value of the binomial coefficient C(N,K). You may rest assured that K <= N and the maximum value of N is 1,000,000,000,000,000. Since the value may be very large, you need to compute the result modulo 1009.
Input
The first line of the input contains the number of test cases T, at most 1000. Each of the next T lines consists of two space separated integers N and K, where 0 <= K <= N and 1 <= N <= 1,000,000,000,000,000.
Output
For each test case, print on a new line, the value of the binomial coefficient C(N,K) modulo 1009.
Example
Input: 3 3 1 5 2 10 3 Output: 3 10 120
| Author: | moneymachine |
| Date Added: | 16-01-2010 |
| Time Limit: | 3 sec |
| Source Limit: | 50000 Bytes |
| Languages: | ADA, ASM, BASH, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, FORT, FS, GO, HASK, ICON, JAR, JAVA, LISP clisp, LISP sbcl, LUA, NEM, NICE, NODEJS, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TEXT |
Comments
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
|
If you are still having problems, see a sample solution here. |

Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach.
Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section.
I believe this question has a
I believe this question has a wrong test case!
My algorithm is:
I find n!/k! modulo 1009
then i find (n-k)! modulo 1009
and because 1009 is a prime , then both of these numbers are coprime to 1009
and we can use multiplicative inverse of (n-k)! modulo 1009
I tested my program for all inputs less than 1000 with a brute force algorithm. I'm sure my algorithm is right and my program works well. but it says wrong answer!!
Please at least give me the test i'm wrong with.
Thanks
(n-k)! is not coprime to 1009
(n-k)! is not coprime to 1009 when n-k>=1009, so your algorithm doesn't work.
Thanks
Thanks
Then what's your idea?
Then what's your idea?
I designed the problem. Use
I designed the problem. Use Lucas theorem to solve it.
Sorry for writing in Persian
Sorry for writing in Persian but I want to make Amir Understand:
ببین یه قضیه ای وجود داره به نام قضیه ی لوکاس که می گه اگر
p
یک عدد اول باشه اونوقت می شه با یه سری محاسبات راحت باقی مانده ی یک ترکیب بر
p
رو به دست آورد.
ویکیپدیا بخون!
As I saw Mr. Merriman's
As I saw Mr. Merriman's answer precomputes C(n,k) for n,k<=1009
I just merged the two algorithms mentioned above. I use Lucas' theorem for big ones and for small ones I find multiplicative inverse of [n-k]! mod 1009.
My solutions are
My solutions are memory-friendly :D
Hi guys, This is a problem