我是靠谱客的博主 爱笑秋天,最近开发中收集的这篇文章主要介绍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("%dn",dt/2);
for(i=1;i<dt;i+=2)printf("%d %dn",ans[i-1],ans[i]);
return 0;
}


最后

以上就是爱笑秋天为你收集整理的Codeforces 449C 贪心的全部内容,希望文章能够帮你解决Codeforces 449C 贪心所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部