10323 - Factorial! You Must be Kidding!!!
Problem M
Factorial! You Must be Kidding!!!
Input: standard input
Output: standard output
Time Limit: 1 second
Memory Limit: 32 MB
Arif has bought a super computer from Bongobazar. Bongobazar is a place in Dhaka where second hand goods are found in plenty. So the super computer bought by him is also second hand and has some bugs. One of the bugs is that the range of unsigned long integer of this computer for C/C++ compiler has changed. Now its new lower limit is 10000 and upper limit is 6227020800. Arif writes a program in C/C++ which determines the factorial of an integer. Factorial of an integer is defined recursively as:
Factorial (0) = 1
Factorial (n) = n*factorial (n-1).
Of course one can manipulate these expressions. For example, it can be written as
Factorial (n) =n*(n-1)*factorial (n-2)
This definition can also be converted to an iterative one.
But Arif knows that his program will not behave rightly in the super computer. You are to write program which will simulate that changed behavior in a Normal Computer.
Input
The input file contains several lines of input. Each line contains a single integer n. No integer has more than six digits. Input is terminated by end of file.
Output
For each line of input you should output a single line. This line will contain a single integer n! if the value of n! fits within the unsigned long integer of Arif’s computer. Otherwise the line will contain one of the following two words
Overflow! //(When n! > 6227020800)
Underflow! //(When n! < 10000)
Sample Input
2
10
100
Sample Output
Underflow!
3628800
Overflow!
(The Decider Contest, Problem setter: Shahriar Manzoor)
#include<stdio.h>
long long int fac(long long a)
{
if(a==0)
{
return 1;
}
return a*fac(a-1);
}
int main()
{
long long int a;
while(scanf("%lld",&a)!=EOF)
{
if(a>=14)
{
printf("Overflow!\n");
}
else if(a==13)
{
printf("6227020800\n");
}
else if(a<0)
{
a=a*(-1);
if(a%2==0)
{
printf("Underflow!\n");
}
else
{
printf("Overflow!\n");
}
}
else
{
long long int n=fac(a);
if(n<10000)
{
printf("Underflow!\n");
}
else
{
printf("%d\n",n);
}
}
}
return 0;
}
No comments:
Post a Comment