Codeforces 449C 贪心

Jzzhu and Apples
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Jzzhu has picked n apples from his big apple tree. All the apples are numbered from 1 to n. Now he wants to sell them to an apple store.

Jzzhu will pack his apples into groups and then sell them. Each group must contain two apples, and the greatest common divisor of numbers of the apples in each group must be greater than 1. Of course, each apple can be part of at most one group.

Jzzhu wonders how to get the maximum possible number of groups. Can you help him?

Input

A single integer n (1 ≤ n ≤ 105), the number of the apples.

Output

The first line must contain a single integer m, representing the maximum number of groups he can get. Each of the next m lines must contain two integers — the numbers of apples in the current group.

If there are several optimal answers you can print any of them.

Examples
input
6
output
2
6 3
2 4
input
9
output
3
9 3
2 4
6 8
input
2
output
0


题意:给出n个数,从1到n,求出不互质的数对最多有多少对(每个数只能用一次,且最大公约数大于1)


题解:首先大于n/2的素数肯定不行。

先找出n/2以内的素数,然后从后往前for。

找出p的倍数,如果p的倍数有偶数个(包含p),那么就可以加进去完。

如果有奇数个,那么我们把2*p取出来不管,最后扔给2,贪心下去就行了。


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
vector<int>sp,ans;
int prime[100005],cnt=0,vis[100005],num[100005];
void init(int t){
	int i,j;
	for(i=2;i<=t;i++){  
        if(!prime[i])num[++cnt]=i;  
        for(j=2;j*i<=t;j++){  
            prime[j*i]=1;  
            if(!(i%j))break;  
        }  
    }
    return ;
}
int main(){
	int n,i,j;
	scanf("%d",&n);
	init(n/2);
	for(i=cnt;i>=1;i--){
		sp.clear();
		for(j=num[i]*2;j<=n;j+=num[i]){
			if(!vis[j]){
				sp.push_back(j);
			}
		}
		sp.push_back(num[i]);
		int dt=sp.size();
		if(dt%2)j=2;
		else j=1;
		for(;j<sp.size();j+=2){
			ans.push_back(sp[j-1]);
			ans.push_back(sp[j]);
			vis[sp[j-1]]=1;
			vis[sp[j]]=1;
		}
	}
	int dt=ans.size();
	printf("%d\n",dt/2);
	for(i=1;i<dt;i+=2)printf("%d %d\n",ans[i-1],ans[i]);
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值