牛客小白月赛62-C-数组划分

暴力思想就是n^2遍历,一看范围10^5铁TLE

gcd != 1代表着什么??

有相同因子!

想到这思路就很清晰了,找什么?就是找有没有除1外的相同因子

一看数的范围,可以数组模拟,也可以用哈希表unordered_map,这个范围大的时候用还行

还有个小细节就是对当前值是不是1要处理,只看因子存在不看具体是哪个因子,会出现2,1也认为是不独立的情况

// Problem: 数组划分
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/47266/C
// Memory Limit: 524288 MB
// Time Limit: 2000 ms
// Date: 2024-03-14 19:00:38
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
#define endl '\n'
#define int int64_t
using namespace std;
int afac[1000005];
void solve() {
	int n; cin >> n;
	for (int i = 1,t; i <= n; ++i) {
		 cin >> t;
		 for (int j = 1; j * j <= t; ++j) {
			 if (t % j == 0) {
				 afac[j]++;
				 afac[t / j]++;
			 }
		 }
	}
	for (int i = 1,t; i <= n; ++i) {
		cin >> t;
		for (int j = 1; j * j <= t; ++j) {
			if (t % j == 0) {
				if ((j != 1 && afac[j]) || (t / j != 1 && afac[t / j])) {
					cout << "No\n";
					return;
				}
			}
		}
	}
	cout << "Yes\n";
}
signed main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int t = 1;
	//cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值