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.

Examples
Input
6
Output
2
6 3
2 4
Input
9
Output
3
9 3
2 4
6 8
Input
2
Output
0
题目大意:将编号为1~n的数两两分为一组,使得每组中的两个数gcd不为1,求最大组数.
分析:比较容易想到将数分为两大组.一组是2的倍数,一组是素数p以及p的倍数,在这两个互相制约的大组里选数拼起来.既然互相制约,那么就先分收益大的,这两个大组中的每两个数都可以拼成一个小组,如果先分第一个大组,那么第二个大组有的数就不能选,可能对于多个p组成的集合里面的数的个数都是奇数,性价比不高.所以先选第二个大组.对于每个质数p,现将p和p*3,p*4,.....这些数两两配对.如果最后还有一个数没有配对,就将它和p*2配对.这样第二大组的收益就最高了,接下来让第一大组尽量地分就好了.
至于为什么分成这样两个大组.一是要让每个分出来的大组中的任意两个数都能组成一个小组,满足题目给出的条件.二是这两个大组尽量不相交.除了2的质数都是奇数.一些偶数同时被分在两个大组是不可避免的,奇数如果不是质数,那么肯定存在于之前的一个质数p的集合中,否则就作为p.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; int tot,n,prime[],tot2,vis[],a[],b[],cnt1,cnt2,tot3,notuse[];
bool use[]; struct node
{
int x,y;
} e[]; void init()
{
for (int i = ; i <= n; i++)
{
if (!vis[i])
prime[++tot2] = i;
for (int j = ; j <= tot2; j++)
{
int t = prime[j] * i;
if (t > n)
break;
vis[t] = ;
if (i % prime[j] == )
break;
}
}
} int main()
{
scanf("%d",&n);
init();
for (int i = ; i <= tot2; i++)
{
memset(a,,sizeof(a));
cnt1 = ;
a[++cnt1] = prime[i];
for (int j = ; j * prime[i] <= n; j++)
if (!use[j * prime[i]])
a[++cnt1] = j * prime[i];
if ( * prime[i] <= n && !use[ * prime[i]])
{
if (cnt1 % == )
a[++cnt1] = * prime[i];
else
{
use[ * prime[i]] = ;
notuse[++tot3] = * prime[i];
}
}
for (int j = ; j + <= cnt1; j += )
{
e[++tot].x = a[j];
e[tot].y = a[j + ];
use[a[j]] = use[a[j + ]] = ;
}
}
for (int i = ; i * <= n; i++)
if (!use[i * ])
notuse[++tot3] = i * ;
for (int i = ; i + <= tot3; i += )
{
e[++tot].x = notuse[i];
e[tot].y = notuse[i + ];
}
printf("%d\n",tot);
for (int i = ; i <= tot; i++)
printf("%d %d\n",e[i].x,e[i].y); return ;
}

Codeforces 449.C Jzzhu and Apples的更多相关文章

  1. Codeforces 450E:Jzzhu and Apples(构造,数学)

    E. Jzzhu and Apples time limit per test: 1 seconds memory limit per test: 256 megabytes input: stand ...

  2. Codeforces 449 B. Jzzhu and Cities

    堆优化dijkstra,假设哪条铁路能够被更新,就把相应铁路删除. B. Jzzhu and Cities time limit per test 2 seconds memory limit per ...

  3. CF449 C. Jzzhu and Apples

    /* http://codeforces.com/problemset/problem/449/C cf 449 C. Jzzhu and Apples 数论+素数+贪心 */ #include &l ...

  4. Codeforces 449C Jzzhu and Apples 贪心 (看题解)

    Jzzhu and Apples 从大的质因子开始贪心, 如果有偶数个则直接组合, 如果是奇数个留下那个质数的两倍, 其余两两组合. #include<bits/stdc++.h> #de ...

  5. Codeforces Round #257 (Div. 2) E题:Jzzhu and Apples 模拟

    E. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. CF449C Jzzhu and Apples (筛素数 数论?

    Codeforces Round #257 (Div. 1) C Codeforces Round #257 (Div. 1) E CF450E C. Jzzhu and Apples time li ...

  7. CF 450E Jzzhu and Apples 数学+模拟

    E. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. Codeforces Round #257 (Div. 1) C. Jzzhu and Apples (素数筛)

    题目链接:http://codeforces.com/problemset/problem/449/C 给你n个数,从1到n.然后从这些数中挑选出不互质的数对最多有多少对. 先是素数筛,显然2的倍数的 ...

  9. Jzzhu and Apples CodeForces - 449C (构造,数学)

    大意: 求从[1,n]范围选择尽量多的数对, 使得每对数的gcd>1 考虑所有除2以外且不超过n/2的素数p, 若p倍数可以选择的有偶数个, 直接全部划分即可 有奇数个的话, 余下一个2*p不划 ...

随机推荐

  1. vim—多行注释、取消多行注释

    多行注释 命令模式: (1)将光标放在要注释的行首,按下组合键ctrl + v ,然后按上下键选取要注释的行. (2)按下大i键,然后插入要注释的符号 # (3)按ESC键,退出后,就会全部注释. 取 ...

  2. sprint3最终演示及团队贡献分

    团队名:在考虑 团队项目:复利计算 项目演示: 之前的功能都有演示过就不再一一截图,把我们新增加的功能说一下 首先用户进入我们的网页可以登录或者注册,注册的用户可以直接输入用户名及密码登录,没有注册的 ...

  3. 《我是IT小小鸟》阅读心得

    虽然读这本书是老师布置的作业,但是读了几页后就被书中的内容所吸引住了.或许是因为我也是学这个专业的,所以书中的一些内容让我觉得非常的有兴趣.作为一个学习软件工程的大一学生还没真正的认识到这个专业的深奥 ...

  4. 第13章 学习shell script

    由于博客园中dollar符号有别的意义,所以文中的dollar符号使用¥表示 第一个script [root@localhost script]# cat -n sh01.sh #!/bin/bash ...

  5. 【week10】规格说明书练习-吉林市1日游

    假设我们全班同学及教师去吉林省吉林市1日游,请为这次活动给出规格说明书. 版本:1.0 编订:于淼 团队:2016级计算机技术全体同学 日期:2016/11/19 1.引言 1.1 编写目的 1.2 ...

  6. 【Linux 命令】- more和less

    more命令 more功能类似 cat ,cat命令是整个文件的内容从上到下显示在屏幕上. more会以一页一页的显示方便使用者逐页阅读,而最基本的指令就是按空白键(space)就往下一页显示,按 b ...

  7. PHP用抛物线的模型实现微信红包生成算法的程序源码

    <?php /* *Author:Kermit *Time:2015-8-26 *Note:红包生成随机算法 */ header("Content-type:text/html;cha ...

  8. Django之contenttypes的应用

    Django contenttypes 应用 简介 contenttypes 是Django内置的一个应用,可以追踪项目中所有app和model的对应关系,并记录在ContentType表中. 每当我 ...

  9. Robotium之“去哪儿旅行”

    Robotium基于APK自动化测试,只有APK文件,没有源代码. Eclipse 默认的debug keystore可以在Windows->Preferences->Android-&g ...

  10. webgl学习笔记一-绘图单点

    写在前面   WebGl(全称:Web Graphics Library : web图形库) 是基于OpenGL ES 2.0的3D绘图协议.   WebGL完美地解决了现有的Web交互式三维动画的两 ...