9
17
2015
0

C. Jzzhu and Apples

C. 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.

Sample test(s)
input
6
output
2
6 3
2 4
input
9
output
3
9 3
2 4
6 8
input
2
output
0
 

    也是一道比较傻的贪心。。。我就奇怪了。。为什么我挑的都是这种题。。好不容易鼓起勇气打了一场Div1。。然后碰到两道贪心这样真的好吗、、、

    具体的思路是。。先把素数筛出来。。然后处理出每一个数的素因数有多少个和每一个素数的倍数。。

然后从大到小枚举素数。。从素数的倍数里找两个数,计入答案。。找数的原则是先找素因数少的数。。因为这样显然比较优嘛。。好吧其实是我不会证。。。

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
struct data{int x,y;} Ans[50010];
vector <int> a[50010];
int n,ans,vis[100100],pri[50010],sum[100010],Sum,fis,tot,v[100010];
bool cmp(int a,int b){
	if (sum[a]==sum[b]) return a<b;
	return sum[a]<sum[b];
}
void Getpri(){
	for (int i=2;i<=n;i++){
		if (!v[i]) pri[++Sum] = i;
		for (int j=1;i*pri[j]<=n && j<=Sum;j++){
			v[pri[j]*i] = 1;
			if (!(i%pri[j])) break;
		}
	}
}
int main(){
	scanf("%d",&n);
	Getpri();
	for (int i=1;i<=Sum;i++)
		for (int j=1;pri[i]*j<=n;j++) a[i].push_back(pri[i]*j),sum[pri[i]*j]++;
	for (int i=Sum;i;i--){
		sort(a[i].begin(),a[i].end(),cmp);tot = 0;
		for (int j=0;j<a[i].size();j++)
			if (!vis[a[i][j]]){
				if (tot==1){
					Ans[++ans] = (data){fis,a[i][j]};
					vis[a[i][j]] = vis[fis] = 1;
					tot = 0;
				}
				else {tot = 1;fis = a[i][j];}
			}
	}
	printf("%d\n",ans);
	for (int i=1;i<=ans;i++) printf("%d %d\n",Ans[i].x,Ans[i].y);
}
Category: codeforces | Tags: | Read Count: 579

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com