cf Round #505 (Div. 1 + Div. 2) B:Weakened Common Divisor (思维+gcd+最小因子)

题目链接:

B. Weakened Common Divisor

昊神讲的很好,必须码,这里是传送门:昊神的传送门

AC代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;

typedef long long ll ;

const ll mod=1e9+7;
const int MAX=1e5+10;

ll solve(ll m)//最小因子
{
	for(int i=2;i*i<=m;i++)
	{
		if(m%i==0)
		{
			return i;
		}
	}
	return m;
}

ll gcd(ll m,ll n)//求最大公约数
{
	if(n==0)
	return m;
	
	return gcd(n,m%n);
}

int main()
{
	ll T;
	scanf("%lld",&T);
	if(T == 1)
	{
		ll a,b;
		scanf("%lld%lld",&a,&b);
		printf("%lld\n",a);
	}
	else if(T != 1)
	{
		ll a,b,a1,b1;
		scanf("%lld%lld",&a,&b);
		ll ans1,ans2;
		ans1=a;
		ans2=b;
		//printf("%lld %lld",ans1,ans2);
		for(ll i=1;i<T;i++)
		{
			scanf("%lld%lld",&a1,&b1);
			ans1=gcd(ans1,a1*b1);
			ans2=gcd(ans2,a1*b1);
			//printf("%lld %lld",ans1,ans2);
		}
		
		if(ans1!=1)
		{
			printf("%lld\n",solve(ans1));
		}
		else if(ans2!=1)
		{
			printf("%lld\n",solve(ans2));
		}
		else
		{
			printf("-1\n");
		}
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值