Fast Factorial Calculator

View as PDF

Submit solution

Points: 10
Time limit: 0.6s
Memory limit: 64M

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

quantum is not in a good mood today, so he has decided to torment people. He wants you to write a large factorial calculator. More specifically, he wants you to calculate very very very large factorials. He wishes you luck.

Input Specification

The first line of input contains the integer N, 0 \le N \le 1\,000, the number of cases to follow.

The next N lines will contain an integer n, such that 0 \le n \le 2^{63}, whose factorial you are to calculate.

Output Specification

For every integer n, you are to output n! \bmod 2^{32}.

Sample Input


Sample Output



  • 0
    SourceCode  commented on Dec. 16, 2017, 2:28 a.m.

    Can someone tell me what's wrong with my solution???

    • 3
      injust  commented on Dec. 16, 2017, 4:28 a.m. edited
      1. Re-read the input specification, you have to calculate N factorials.
      2. 2 ^ 32 is not 2^{32}

  • 5
    Cueball1234  commented on Sept. 21, 2016, 4:03 p.m.

    I have a solution to this problem, and I can't find anything wrong with it. I checked it in some other C++ coder, and it gave the right values. Can someone help me with why the second test case always shows me WA? Thanks!

    • 3
      Kirito  commented on Sept. 21, 2016, 7:02 p.m.

      Changing signed long long int to unsigned long long seems to fix it.

      Probably overflowed.

      • 6
        Cueball1234  commented on Sept. 25, 2016, 2:33 p.m.

        Hey, it works! Thank you so much for all your help Shinigami!

  • 24
    d  commented on Feb. 13, 2016, 8:45 p.m.

    The value of n!\text{ mod }2^{32}-5 is more interesting to compute and optimize.

    • 25
      Riolku  commented on April 14, 2019, 3:04 p.m.

      No please all we wanted was happiness