NWERC11A - Binomial coefficients

no tags 

Binomial coefficients

Gunnar is quite an old and forgetful researcher. Right now he is writing a paper on security in social networks and it actually involves some combinatorics. He wrote a program for calculating binomial coefficients to help him check some of his calculations.

A binomial coefficient is a number

(n )        n!      =  ----------   k     k!(n - k)!

where n and k are non-negative integers.

Gunnar used his program to calculate (n)  k and got a number m as a result. Unfortunately, since he is forgetful, he forgot the numbers n and k he used as input. These two numbers were a result of a long calculation and they are written on one of many papers lying on his desk. Instead of trying to search for the papers, he tried to reconstruct the numbers n,k from the output he got. Can you help him and find all possible candidates?


On the first line a positive integer: the number of test cases, at most 100. After that per test case:

  • one line with an integer m (2 ≤ m ≤ 1015): the output of Gunnar’s program.


Per test case:

  • one line with an integer: the number of ways of expressing m as a binomial coefficient.
  • one line with all pairs (n,k) that satisfy ( )  n  k = m. Order them in increasing order of n and, in case of a tie, order them in increasing order of k. Format them as in the sample output.

Sample in- and output



(6,2) (6,4) (15,1) (15,14)

Copyright notice

This problem text is copyright by the NWERC 2011 jury. It is licensed under the Creative Commons Attribution-Share Alike license version 3.0; The complete license text can be found at: http://creativecommons.org/licenses/by-sa/3.0/legalcode

hide comments
nadstratosfer: 2020-02-16 14:15:12

Awesome problem. Totally didn't expect my solution to end up like it did, just kept building up on ideas and observations made while poking at it until I got me a working program.

Bharath Reddy: 2014-04-06 11:11:35

While printing (6,2) there should be no spaces before or after the 'comma'
Costed me WA! and lot of time.

Sidharth Gupta: 2012-01-31 21:29:52

test cases are weak. my wrong code got AC!
RE : Why did you submit a wrong code?

Last edit: 2014-03-01 03:46:16

Added by:Jeroen Bransen
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:NWERC 2011 Jury