Developing School's Contest 2012-2 by HUT :数字游戏续

Problem Description

自从小艾上次在和小牛的游戏中取得了信心之后,这次他又想到了一个问题,他要去计算两个数A和B的最大公约数。
由于这两个数非常的大,于是小牛把数字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。

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值