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。所以偶数的组合肯定能够。可是奇数的就有点难搞了。

假设用加倍的方法来组成一对的话那不是最多的情况。

可是多加两位就是最多的情况了,这是前20名的代码中的做法。

我没想明确。后面才感觉这得想到才行。由于奇数加两位之后为偶数的机率比較小,就不和偶数的组合情况反复了,然后又能够把奇数组合成一对。这太机智了。比赛的时候确实非常难想出来。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<bitset>
#define mem(a,b) memset(a,b,sizeof(a))
#define INF 1000000070000
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int vis[100005],is[100005];
vector<pair<int,int> >v;
int main()
{
int n,i,j;
cin>>n;
for(i=4;i<=n;i+=2)
vis[i]=1;
for(i=3;i<=n;i+=2)
{
if(!vis[i])
{
if(i*2>n) break;
vector<int>a;
for(j=i;j<=n;j+=2*i)
{
vis[j]=1;
if(!is[j]) a.push_back(j),is[j]=1;
}
for(j=a.size()-1;j>0;j-=2)
{
v.push_back(make_pair(a[j],a[j-1]));
is[a[j]]=is[a[j-1]]=1;
}
if(a.size()&1)
{
v.push_back(make_pair(a[0],a[0]*2));
is[a[0]]=is[a[0]*2]=1;
}
}
}
if(n&1) n--;
int x=0,y;
for(i=n;i>0;i-=2)
{
if(is[i]) continue;
if(!x) y=i,x=1;
else x=0,v.push_back(make_pair(i,y));
}
printf("%d\n",v.size());
for(i=0;i<v.size();i++)
printf("%d %d\n",v[i].first,v[i].second);
return 0;
}

Codeforces Round #257 (Div. 2) E题:Jzzhu and Apples 模拟的更多相关文章

  1. Codeforces Round #257 (Div. 2) D题:Jzzhu and Cities 删特殊边的最短路

    D. Jzzhu and Cities time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. Codeforces Round #257 (Div. 2) A题

    A. Jzzhu and Children time limit per test 1 second memory limit per test 256 megabytes input standar ...

  3. Codeforces Round #257 (Div. 2 ) B. Jzzhu and Sequences

    B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...

  4. Codeforces Round #257 (Div. 1)A~C(DIV.2-C~E)题解

    今天老师(orz sansirowaltz)让我们做了很久之前的一场Codeforces Round #257 (Div. 1),这里给出A~C的题解,对应DIV2的C~E. A.Jzzhu and ...

  5. Codeforces Round #378 (Div. 2) D题(data structure)解题报告

    题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...

  6. Codeforces Round #612 (Div. 2) 前四题题解

    这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...

  7. Codeforces Round #713 (Div. 3)AB题

    Codeforces Round #713 (Div. 3) Editorial 记录一下自己写的前二题本人比较菜 A. Spy Detected! You are given an array a ...

  8. Codeforces Round #552 (Div. 3) A题

    题目网址:http://codeforces.com/contest/1154/problem/ 题目意思:就是给你四个数,这四个数是a+b,a+c,b+c,a+b+c,次序未知要反求出a,b,c,d ...

  9. Codeforces Round #257(Div. 2) B. Jzzhu and Sequences(矩阵高速幂)

    题目链接:http://codeforces.com/problemset/problem/450/B B. Jzzhu and Sequences time limit per test 1 sec ...

随机推荐

  1. FFmpeg基础库编程开发学习笔记——视频常见格式

    声明一下:这些关于ffmpeg的文章仅仅是用于记录我的学习历程和以便于以后查阅,文章中的一些文字可能是直接摘自于其它文章.书籍或者文献,学习ffmpeg相关知识是为了使用在Android上,我也才是刚 ...

  2. Warning: RPMDB altered outside of yum.的解决办法

    错误提示: Warning: RPMDB altered outside of yum 解决办法: 删除yum的历史记录rm -rf /var/lib/yum/history/*.sqlite 上面的 ...

  3. eclipse 垃圾回收器,内存释放

    http://zhangrong-0825-163-com.iteye.com/blog/7334071.如何在eclipse里修改web工程的访问路径,步骤如下: 点击web工程——>选择pr ...

  4. 【jQuery】JQuery-ui autocomplete与strtus2结合使用

    汉字搜索效果图: 拼音首字母搜索效果图:  1)数据库表及函数(SQL Server 2008) 先来建立数据库表City,它包含两个字段CityID,CityName. CREATE TABLE C ...

  5. Python练习笔记——计算个人体重指数

    输入您的身高 体重 性别 计算出你的体重是否标准 gender = input('请输入您的性别(boy or girl):') height = input('请输入您的身高(单位cm):') he ...

  6. OGG_GoldenGate日常监控(案例)

    2014-03-11 Created By BaoXinjian  

  7. iOS9中怎样在日历App中创建一个随意时间之前開始的提醒(三)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 假设认为写的不好请多提意见,假设认为不错请多多支持点赞.谢谢! hopy ;) 四.创建随意时间之前開始的提醒 如今我们找到了指定源中的指定日 ...

  8. django中跨app引用model

    可能是自己水平的原因,总感觉跨django中app引用有点怪怪的,所以在自己没有达到另一个级别之前就先把正确的解决 方案记一下吧. 一.django中跨app引用model,以app02中的model ...

  9. thinkphp中获取参数值的方法

    以获取$type这个参数为例:一:通过传统方法:$_GET, $_POST    $type = intval($_GET['type'])这种方法需要自己写过滤规则,保证数据安全. 二:在Actio ...

  10. 为什么easyui的datagrid里getSelections还有getChecked只能获取一行值呢?

    http://jquery-easyui.wikidot.com/forum/t-280470 如果没有 idField属性,getSelections就只能获取一个行的值了 注意:如果用了前面说的& ...