Problem Description
自从小艾上次在和小牛的游戏中取得了信心之后,这次他又想到了一个问题,他要去计算两个数A和B的最大公约数。
由于这两个数非常的大,于是小牛把数字A拆成了N个数,这N个数相乘的结果是A,把B拆成了M个数,同样这M个
由于这两个数非常的大,于是小牛把数字A拆成了N个数,这N个数相乘的结果是A,把B拆成了M个数,同样这M个
数的乘积等于B,小艾迫不及待地想去计算出这两个数的最大公约数,这次你能帮帮他吗?如果结果超过了9位数,
输出最后的9位数。
Input
输入有多组数据。
第一行包含一个正整数N ( 1 <= N <= 1000 )。
第二行包含N个以空格隔开的小于 1 000 000 000的正整数,它们的乘积是A。
第三行包含一个正整数M ( 1 <= M <= 1000 )。
第四行包含M个以空格隔开的小于 1 000 000 000的正整数,它们的乘积是B。
第一行包含一个正整数N ( 1 <= N <= 1000 )。
第二行包含N个以空格隔开的小于 1 000 000 000的正整数,它们的乘积是A。
第三行包含一个正整数M ( 1 <= M <= 1000 )。
第四行包含M个以空格隔开的小于 1 000 000 000的正整数,它们的乘积是B。
Output
输出A和B的最大公约数,如果结果超过了9位数,输出后9位。
Sample Input
3 2 3 5 2 4 5 4 6 2 3 4 1 1 3 358572 83391967 82 3 50229961 1091444 8863
Sample Output
10 1 000012028
Source
2012暑假集训
//n,m的范围只有1000,所以可以直接两两求出最大公约数保存下来即可,n、m范围大的话就要用质因子分解了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
#define LL __int64
#define inf 1000000000
#define maxn 1005
LL a[maxn];
LL gcd(LL x,LL y)
{
LL z;
while(y)
{
z=y;
y=x%y;
x=z;
}
return x;
}
int main()
{
LL n,m,i,j;
while(~scanf("%I64d",&n))
{
for(i=1;i<=n;i++)
scanf("%I64d",&a[i]);
scanf("%I64d",&m);
LL ans=1,tmp=1,x,tt;
bool flag=false;
for(i=1;i<=m;i++)
{
scanf("%I64d",&x);
for(j=1;j<=n;j++)
{
tt=gcd(x,a[j]);
x/=tt,a[j]/=tt;
ans=ans*tt%inf;
if(!flag)
{
tmp*=tt;
if(tmp>=inf)
flag=true;
}
}
}
if(!flag)
printf("%I64d\n",ans%inf);
else
printf("%09I64d\n",ans%inf);
}
return 0;
}