题目链接:
http://codeforces.com/problemset/problem/822/A
Holidays have finished. Thanks to the help of the hacker Leha, Noora managed to enter the university of her dreams which is located in a town Pavlopolis. It's well known that universities provide students with dormitory for the period of university studies. Consequently Noora had to leave Vičkopolis and move to Pavlopolis. Thus Leha was left completely alone in a quiet town Vičkopolis. He almost even fell into a depression from boredom!
Leha came up with a task for himself to relax a little. He chooses two integers A and B and then calculates the greatest common divisor of integers "A factorial" and "B factorial". Formally the hacker wants to find out GCD(A!, B!). It's well known that the factorial of an integer xis a product of all positive integers less than or equal to x. Thus x! = 1·2·3·...·(x - 1)·x. For example 4! = 1·2·3·4 = 24. Recall that GCD(x, y) is the largest positive integer q that divides (without a remainder) both x and y.
Leha has learned how to solve this task very effective. You are able to cope with it not worse, aren't you?
The first and single line contains two integers A and B (1 ≤ A, B ≤ 109, min(A, B) ≤ 12).
Print a single integer denoting the greatest common divisor of integers A! and B!.
4 3
6
题目大意:
输入两个数a,b,求a! 与 b! 的最大公因数。
解题思路:
这道题不是大数问题,是考察逻辑思维的,其实很简单。我们来分析一下:
就按照第一组测试数据分析:4!=4 * 3 * 2 * 1
3!= 3 * 2 * 1
最大公因数为:3*2*1=6(也就是等于4和3中 小的那个数3 的 阶乘)。所以这道题求的就是 a和b中 小的那个数 的阶乘;又因为题目说min(a,b)<=12,所以直接暴力(因为最大也不过是 12的阶乘 )。
代码:
#include<iostream>
using namespace std;
int main()
{
int a,b,c[13];
c[1]=1;
for(int i=2;i<=12;i++) //数组里保存的是1、 2、 3......12 这12个数各自的阶乘值
c[i]=c[i-1]*i;
while(cin>>a>>b)
{
if(a<b) //输出较小的那个数阶乘即可
cout<<c[a]<<endl;
else
cout<<c[b]<<endl;
}
return 0;
}