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
#include
#include
#include
#include
using namespace std;
vectorsp,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


你可能感兴趣的:(每日一题,贪心)