蓝桥杯真题讲解:公因数匹配(数论:分解质因数)

本文解析了蓝桥杯竞赛中的一个题目,涉及数论中的公因数匹配和分解质因数算法,提供了C++代码示例,展示了如何使用质因数分解来找出特定条件下的因子对。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

蓝桥杯真题讲解:公因数匹配(数论:分解质因数)

一、视频讲解

蓝桥杯真题讲解:公因数匹配(数论:分解质因数)
在这里插入图片描述

二、正解代码

#include<bits/stdc++.h>
#define endl '\n'
#define deb(x) cout << #x << " = " << x << '\n';
#define INF 0x3f3f3f3f
using namespace std;
map<int,vector<int>>st;
int cnt = 0;

void test(){

	for(int i = 2; i <= 1e6; i ++){
		int tmp = i;
		bool flag = true;
		for(int j = 2; j <= tmp / j; j ++){
			if(tmp % j == 0)
			{
				flag = false;
				break;
			}
		}
		cnt += flag;
	}
	cout << cnt << endl;
}

void prim(int x, int pos) {
	for(int i = 2; i <= x / i; i ++){
		if(x % i)
			continue;
		st[i].push_back(pos);
		while(x % i == 0){
			x /= i;
		}
	}

	if(x > 1){
		st[x].push_back(pos);
	}
	return;

}

void solve()
{
	int n; cin >> n;
	for(int i = 1; i <= n; i ++)
	{
		int x; cin >> x;
		prim(x, i);
	}

	pair<int,int> ans = {INF, INF};

	for(auto [x, y]: st){
		if(y.size() < 2){
			continue;
		}
		
		if(y[0] < ans.first){
			ans = {y[0], y[1]};
		}else if(y[0] == ans.first){
			if(y[1] < ans.second){
				ans = {y[0], y[1]};
			}
		}
	}

	cout << ans.first << " " << ans.second << endl;
	// test();

}

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	t = 1;
	//cin >> t;
	while(t--)
	solve();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值