2020 牛客多校 第四场 H-Harder Gcd Problem(贪心 + 思维)

题目链接: H-Harder Gcd Problem

Description

题意:给出1~n的序列,求gcd>1的最大组数并输出任意一种方案

After solving the Basic Gcd Problem, ZYB gives you a more difficult one:
Given an integer n, find two subset A and B of {1,2,…,n} such that:
∣A∣=∣B∣=m and A∩B=∅ Let A={a1,a2,…,am} and B={b1,b2,…,bm},there exists two permutations p1,p2,…,pm and q1,q2,…qm such that for each 1 ≤ i ≤ m, gcd(api, bqi) > 1.
Please find two subsets with maximum value of m.

Input

  • There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases.
  • For each test case, there is only one line containing an integer n (4 ≤ n ≤ 2×105 )
  • It’s guaranteed that the sum of n of all test cases will not exceed 2×105 .

Output

For each test case, output an integer m in the first line. In the next m lines, each contains two integers ai and bi (gcd(ai, bi) > 1), denoting the i-th element in subset A and B.
If there are multiple solutions, you can output any of them.

Sample Input

2
4
10

Sample Output

1
2 4
4
3 9
5 10
8 2
4 6

Method

  • 显然,最大组数需要处理 2i 和 i2的交叉使用问题,先用vector将 i (1 < i <= n)的倍数存起来,这个时候得到n-1个gcd均大于1的序列;
  • 从大到小开始贪心,对于只有偶数个数的序列,直接两两选用即可,对于有奇数个数的序列,将第一个保留下来,其他的两两选用,此时需要判断 i2 是否小于等于 n,若小于则选用 i 和 i2;
  • 最后剩下 i=2 的序列没有选用,对该序列进行两两选用即可(记得在第二步时打上标记);

Code

详见注释

#include <bits/stdc++.h>

using namespace std;
#pragma GCC optimize(2)
#define ios std::ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);
#define rtxt freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#define ll long long
const int Max = 1e6+3;
const int mod = 1e9+7;

int T, n, book[Max], vis[Max];
vector<int> v[Max];
pair<int, int> a[Max];

void solve()
{
	int tot=0;
	for(int i=2; i<=n; i++)			//生成gcd大于1的序列 
	{
		for(int j=i; j<=n; j+=i)
		{
			if(!book[j]) {
				v[i].push_back(j);
				book[j] = 1;
			}
		}
	}
	for(int i=n; i>=3; i--)			//从大到小选用 
	{
		int size = v[i].size();
		if(size > 0) {
			if(size&1) {
				for(int j=1; j<size; j+=2)
				{
					a[++tot].first = v[i][j];
					a[tot].second = v[i][j+1];
				}
				if(2*i <= n) {
					a[++tot].first = i;
					a[tot].second = 2*i;
					vis[2*i] = 1;
				}
			}
			else {
				for(int j=0; j<size; j+=2)
				{
					a[++tot].first = v[i][j];
					a[tot].second = v[i][j+1];
				}
			} 
		}
	}
	int f=0, len = v[2].size();
	for(int i=0; i<len; i++)		//选用 i = 2 的序列 
	{
		if(!vis[v[2][i]]) {
			if(!f) {
				f = v[2][i];
			}
			else {
				a[++tot].first = f;
				a[tot].second = v[2][i];
				f = 0;
			}
		}
	}
	cout << tot << endl;
	for(int i=1; i<=tot; i++)
		cout << a[i].first << " " << a[i].second << endl;
}

int main()
{
	ios
	
	cin >> T;
	while(T--)
	{
		cin >> n;
		for(int i=1; i<=n; i++)
		{
			book[i] = vis[i] = 0;
			v[i].clear();
		}
		solve();
	}
	return 0;
}

蒟蒻一只,欢迎指正

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值