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. Spring Cloud(一):服务治理技术概览【Finchley 版】

    Spring Cloud(一):服务治理技术概览[Finchley 版]  发表于 2018-04-14 |  更新于 2018-05-07 |  Spring Cloud Netflix 是 Spr ...

  2. Python 日志记录与程序流追踪(基础篇)

    日志记录(Logging) More than print: 每次用 terminal debug 时都要手动在各种可能出现 bug 的地方 print 相关信息来确认 bug 的位置: 每次完成 d ...

  3. Beta发布——视频博客

    1.视频链接 视频上传至优酷自频道,地址链接:http://v.youku.com/v_show/id_XMzkzNzAxNDk2OA==.html?spm=a2hzp.8244740.0.0 2.视 ...

  4. gitLab服务器搭建+ rundeck自动化部署

    git服务器搭建 https://blog.csdn.net/gx_1_11_real/article/details/79406427 rundeck   部署 https://blog.csdn. ...

  5. python爬虫调用搜索引擎及图片爬取实战

    实战三-向搜索引擎提交搜索请求 关键点:利用搜索引擎提供的接口 百度的接口:wd="要搜索的内容" 360的接口:q="要搜索的内容" 所以我们只要把我们提交给 ...

  6. C++对象内存布局测试总结

    C++对象内存布局测试总结 http://hi.baidu.com/����/blog/item/826d38ff13c32e3a5d6008e8.html 上文是半年前对虚函数.虚拟继承的理解.可能 ...

  7. TCP系列46—拥塞控制—9、SACK下的快速恢复与Limited transmit

    一.概述 1.SACK下的特殊处理过程 SACK下的拥塞控制处理是linux中拥塞控制的实现依据,再次强调一遍RFC6675的重要性,linux中拥塞控制主体框架的实现是与RFC6675一致的,所以如 ...

  8. PAT 甲级 1081 Rational Sum (数据不严谨 点名批评)

    https://pintia.cn/problem-sets/994805342720868352/problems/994805386161274880 Given N rational numbe ...

  9. Spring Cloud 架构 五大神兽的功能

    什么是微服务 微服务的概念源于2014年3月Martin Fowler所写的一篇文章“Microservices”. 微服务架构是一种架构模式,它提倡将单一应用程序划分成一组小的服务,服务之间互相协调 ...

  10. mysql 8 server windows 安装经验分享

    windows下安装一般分为文件/msi安装文件 本章我们说的是文件行的mysql server 安装 下载地址:https://dev.mysql.com/downloads/mysql/ 下载完后 ...