首页 > 技术文章 > Codeforces 1025B Weakened Common Divisor

LMSH7 2018-08-20 22:49 原文

Codeforces 1025B Weakened Common Divisor

题目大意:给出若干组数对\((a_1, b_1),(a_2, b_2), (a_3, b_3), (a_4, b_4)\),定义\(WCD\)为满足每一个数对中至少作为一个数字的因子的正整数,现在要求你求出\(WCD\)

试图用暴力解决问题,以为最多也就枚举到\(\sqrt{max(a_i,b_i)}\)的素数就好,然后发现并不可能,如果给出的数全是大素数,就凉了.

正解

\(x=gcd(lcm(a_1, b_1),lcm(a_2, b_2),lcm(a_3,b_3))\)

挑出来\(x\)因子中最小的就好

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstring>
#include <cmath> 
#include <algorithm>

typedef long long ll;

const int N = 150005;
const int q = 600005;

ll n, a[N], b[N];
ll qwq, ans = 19260817000;

inline long long read(){
	long long x = 0, f = 1;
	char c = getchar();
	while(c < '0' || c > '9'){
		c = getchar();
	}
	while(c >= '0' && c <= '9'){
		x = (x << 3) + (x << 1) + (c ^ 48);
		c = getchar();
	}
	return x * f;
}

ll gcd(ll a, ll b){
	if(b == 0) return a;
	else return gcd(b, a % b);
}

int main(){
	n = read();
	for(int i = 1; i <= n; ++i){
		a[i] = read(); b[i] = read();
		qwq = gcd(qwq, 1ll* b[i] * a[i] / gcd(b[i], a[i]));

	}
	if(qwq == 1){
		printf("-1\n");
		return 0;
	}
	

	for(int i = 1; i <= n; ++i){
		ll x = gcd(qwq, a[i]);
		if(x > 1) qwq = x;
		ll y = gcd(qwq, b[i]);
		if(y > 1) qwq = y;
	}

	printf("%I64d\n", qwq);

	return 0;
}

晚上打cf,脑子不好使

推荐阅读