2024牛客暑期多校训练营8 A - Haitang and Game

在比赛中我想的是最大数据是1e5,所以我320的平方就会超过1e5,我们需要插入数组里的两个数字的__gcd(x,y),并且__gcd(x,y)不在数组里,所以我想只用跑倍数跑数组

#include <bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl '\n'
using namespace std;
const int A = 1e5+10;
signed main() {
	IOS
	int t;
	cin >> t;
	while(t--) {
		int n;
		cin >> n;
		int vis[350]={0};
		int ve[n];
		for(int i = 0 ; i < n ; i++) {
			cin>>ve[i];
			if(ve[i]<=320) vis[ve[i]]=1;
		}
		if(n == 1) {
			cout << "Haitang" << endl;
		} else {
			int ans = 0;
			for(int i = 1 ; i <= 320 ; i++) {
				if(vis[i]) continue;
				int cnt = 0;
				int res = 0;
				for(int j = 0 ; j < n ; j++) {
					if(ve[j] % i == 0) {
						if(res == 0) {
							res = ve[j];
						} else {
							res = __gcd(res,ve[j]);
						}
						if(res == i) {
							vis[i] = 1;
							ans++;
							break;
						}
					}
				}
			}
			//cout<<ans<<"--"<<endl;
			if(ans % 2 != 0) cout << "dXqwq" << endl;
			else cout << "Haitang" << endl;
		}
	}
	return 0;
}

但是这样会找少,因为不一定320就把所有的数字找到,并且数字范围一加大就会超时,所以我们通过跑数组来找倍数,也就是通过判断有没有出现两个数字%i为0并且两数的__gcd(x,y) 等于i

#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int A = 1e5+10;
int vis[A];
signed main() {
	IOS
	int t;
	cin >> t;
	while(t--) {
		int n;
		cin >> n;
		int maxx = 0;
		memset(vis,0,sizeof vis);
		for(int i = 0 ; i < n ; i++) {
			int x;
			cin >> x;
			vis[x]++;
			maxx = x;
		}
		int ans = 0;
		for(int i = maxx - 1 ; i ; i--) {
			if(vis[i]) continue;
			int cnt = 0;
			for(int j = i * 2 ; j <= maxx ; j+=i) {
				if(!vis[j]) continue;
				if(cnt) {
					if(__gcd(cnt,j) == i) {
						vis[i] = 1;
						ans++;
						break;
					} 
				} else {
					cnt = j;
				}
			}
		}
		if(ans % 2 != 0) cout << "dXqwq" << endl;
		else cout << "Haitang" << endl;
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值