Factorial Sum(URI Online Judge | 1161)

URI Online Judge | 1161

Factorial Sum

Adapted by Neilor Tonin, URI  Brazil
Timelimit: 1
Read two numbers M and N indefinitely. Calculate and write the sum of their factorial. Be carefull, because the result can have more than 15 digits.

Input

The input file contains many test cases. Each test case contains two integer numbers M (0 ≤ M ≤ 20) and N (0 ≤ N ≤ 20). The end of file is determined by eof.

Output

For each test case in the input your program must print a single line, containing a number that is the sum of the both factorial (M and N).
Input SampleOutput Sample
4 4
0 0
0 2
48
2
3
Solution:

#include<bits/stdc++.h>
using namespace std;
int main()
{
long int m,n;
while(scanf("%ld%ld",&m,&n)!=EOF)
{
long long int factM=1,factN=1,i,j,sum=0;
for(i=1;i<=m;i++)
{
factM=factM*i;
}
for(j=1;j<=n;j++)
{
factN=factN*j;
}
sum=factM+factN;
printf("%lld\n",sum);
}
}

Comments