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.

Examples
input

Copy
6
output

Copy
2
6 3
2 4
input

Copy
9
output

Copy
3
9 3
2 4
6 8
input

Copy
2
output

Copy
0

题目要求的是在1-n范围内最多有多少对数的最大公约数大于1
因为要求最大,所以偶数对和奇数对要尽量不凑成对
考虑到加双倍后奇数任然是奇数,所以我们可以通过加倍的方式找出公约数大于1的对数
然后就是细节的处理了
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll mod = 1e9 + ;
const ll maxn = 1e5 + ;
ll vis[maxn], is[maxn];
vector<ll> a;
vector< pair< ll, ll > > E;
int main() {
std::ios::sync_with_stdio(false);
ll n;
while( cin >> n ) {
memset( vis, , sizeof(vis) );
memset( is, , sizeof(is) );
E.clear();
for( ll i = ; i <= n; i += ) {
vis[i] = ;
}
for( ll i = ; i <= n; i += ) { //找出奇数的倍数
if( !vis[i] ) {
if( i* > n ) {
break;
}
a.clear();
for( ll j = i; j <= n; j += *i ) {
vis[j] = ;
if( !is[j] ) {
is[j] = ;
a.push_back(j);
}
}
for( ll j = a.size()-; j > ; j -= ) {
E.push_back( make_pair( a[j], a[j-] ) );
is[a[j]] = is[a[j-]] = ;
}
if( a.size() & ) { //如果有单独无法找到配对的奇数,用他的而倍数与之配对
E.push_back( make_pair( a[], a[]* ) );
is[a[]] = is[a[]*] = ;
}
}
}
if( n & ) {
n --;
}
ll flag = , t;
for( ll i = n; i > ; i -= ) { //除去奇数后剩余的偶数对
if( !is[i] ) {
if( !flag ) {
flag = ;
t = i;
} else {
flag = ;
E.push_back( make_pair( i, t ) );
}
}
}
cout << E.size() << endl;
for( ll i = ; i < E.size(); i ++ ) {
cout << E[i].first << " " << E[i].second << endl;
}
}
return ;
}

CF 450E Jzzhu and Apples 数学+模拟的更多相关文章

  1. CF 990A. Commentary Boxes【数学/模拟】

    [链接]:CF [题意]:对于一个数n,每次加一的代价是a,每次减一的代价是b,求被m整除时的最小代价. [分析]:分情况讨论,自己多举几个栗子. [代码]: #include<cstdio&g ...

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

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

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

  4. CF449 C. Jzzhu and Apples

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

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

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

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

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

  7. Codeforces 449.C Jzzhu and Apples

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

  8. cf 443 D. Teams Formation](细节模拟题)

    cf 443 D. Teams Formation(细节模拟题) 题意: 给出一个长为\(n\)的序列,重复\(m\)次形成一个新的序列,动态消除所有k个连续相同的数字,问最后会剩下多少个数(题目保证 ...

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

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

随机推荐

  1. AI and Robot

    Have you ever seen a movie called "Ex Machina"?  I like this movie very much. Artificial i ...

  2. 完全零基础在Linux中安装 JDK

    完全零基础在Linux中安装 JDK 总体思路:先确定没有Java程序了 — 然后创建相应路径文件夹 — 下载JDK — 解压到当前路径 — 自定义文件名称 — 配置环境变量 — 检查是否安装成功 第 ...

  3. kube-proxy源码解析

    kubernetes离线安装包,仅需三步 kube-proxy源码解析 ipvs相对于iptables模式具备较高的性能与稳定性, 本文讲以此模式的源码解析为主,如果想去了解iptables模式的原理 ...

  4. Android 使用 DiffUtil 处理 RecyclerView 数据更新问题

    背景 RecyclerView.Adapter#notifyDataSetChanged() 会每次刷新整个布局: 每次手动调用 RecyclerView.Adapter#notifyItemXx 系 ...

  5. POI通用导出Excel数据(包括样式设计)

    前言 前一段时间我写过通用的导入Excel,前几天也写了导出pdf格式的,还有我之前搞得导出Word,我在之前的博客也都介绍了导出和导入是一个道理,无非是一个获取一个是赋值.昨天有一位同仁看了我的Ex ...

  6. Tomcat中文乱码问题

    新从官网下载的Tomcat7和Tomcat8,在运行的时候都会有乱码的问题,就此发现问题,我们就给它就地正法! 经过初步的分析,问题产生的大概原因是由于Tomcat的log日志模块不识别中文的问题, ...

  7. K8S搭建-1 Master 2 Workers(dashboard+ingress)

    本文讲述k8s最新版的搭建(v1.15.2) 分如下几个topic步骤: 各个节点的基本配置 master节点的构建 worker节点的构建 安装dashboard 安装ingress 常见命令 do ...

  8. Hibernate中Criteria的完整用法2

    Criteria的完整用法 QBE (Query By Example) Criteria cri = session.createCriteria(Student.class); cri.add(E ...

  9. 开发者工具conloseLog的使用

  10. 二阶段js 入门知识点 自我总结复习

    二阶段自我总复习   1.javascript基础 :  客户端   安全性   跨平台   脚本语言 三大结构:  顺序 .选择.循环                    顺序:运算符和表达式  ...