## Fast Factorial Calculator

View as PDF

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

Author:
Problem type

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 , , the number of cases to follow.

The next lines will contain an integer , such that , whose factorial you are to calculate.

#### Output Specification

For every integer , you are to output .

#### Sample Input

2
5
13

#### Sample Output

120
1932053504

• commented on March 23, 2021, 6:52 p.m.

why I'm getting RTE in my code pls HELP! PLS :)

• commented on March 23, 2021, 7:09 p.m. edit 6
1. The input can go up to 2e63 which overflows long long.
2. You seem to be looping from 1 to N, which is O(N) time, with N going up to 2e63, there is no way you will pass in time.
There is a much more efficient way that basically takes O(1) time.
Hint: Pay close attention to Mod 2^32
• commented on Dec. 16, 2017, 2:28 a.m.

Can someone tell me what's wrong with my solution???
https://dmoj.ca/submission/709777

• commented on Dec. 16, 2017, 4:28 a.m. edited
1. Re-read the input specification, you have to calculate factorials.
2. 2 ^ 32 is not
• 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!

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

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

Probably overflowed.

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

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

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

The value of is more interesting to compute and optimize.

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

No please all we wanted was happiness