Codeforces Round #257 (Div. 2)E(数论+构造)

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

题意:在1到n的数中,找出尽量多的数对,使得每对数的最大公约数>1

思路:先看 1 和大于 n / 2 的素数,这些数可以直接删掉,因为它们不可能和其它任何数配对
  
            然后对于所有素数 x 且 2 <  x <= n / 2,在剩下的未选的数中找出 x 的倍数,看这些数的数量是否为偶数,为偶数可以直接两两配对,如果为奇数,则忽略 x*2,将剩下的

            数两两配对

            最后只剩下2的倍数的数没有被配对,两两配对即可

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         using namespace std; typedef long long ll; int n; int d; int top; int isprime[100010]; int prime[10010]; int ansx[100010]; int ansy[100010]; int vis[100010]; int stk[100010]; void getprime() { d=0; for(ll i=2;i<=50000;i++)if(!isprime[i]) { prime[d++]=i; for(ll j=i*i;j<=100000;j+=i)isprime[j]=1; } } int main() { getprime(); while(scanf("%d",&n)!=EOF) { int i,j; int dd=0; int aa=0; memset(vis,0,sizeof(vis)); for(i=1;prime[i]<=n/2&&i 
         
       
      
      
     
     
    
    
   
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值