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. 用python绘制漂亮的图形

    先看效果,没有用任何绘图工具,只是运行了一段python代码. 代码如下: _ = ( 255, lambda V ,B,c :c and Y(V*V+B,B, c -1)if(abs(V)<6 ...

  2. c语言指针汇总

    1.指向单个变量的指针: ; int* p = &a; printf("%d", *p); 2.数组的指针 (1)一维数组的指针 ] = { ,,,, }; int *p; ...

  3. Java equal() 和 == 详细分析

    1 ==  返回值是true/false; (1) 基本数据类型比较的就是值(2)引用型数据类型就是地址值 public class Test1 { public static void main(S ...

  4. ext container的使用的场景

    container 是 panel 简化,他称之为容器,而panel则是面板. 如果不需要类似Ext.panel.Panel,Ext.window.Window和Ext.tab.Panel 等功能,则 ...

  5. 让techempower帮你通讯服务框架的性能

    在编写服务应用框架的时候一般都需要进行性能测试,但自己测试毕竟资源受限所以很难做更高性能上的测试.其实GitHub上有一个项目可以让开发人员提交自己的框架服务代码然后进行一个标准测试:现在已经有上百个 ...

  6. 【Java例题】5.5 两个字符串中最长公共子串

    5. 查找两个字符串中含有的最长字符数的公共子串. package chapter5; import java.util.Scanner; public class demo5 { public st ...

  7. Spring 常犯的十大错误,答应我 打死都不要犯好吗?

    1. 错误一:太过关注底层 我们正在解决这个常见错误,是因为 “非我所创” 综合症在软件开发领域很是常见.症状包括经常重写一些常见的代码,很多开发人员都有这种症状. 虽然理解特定库的内部结构及其实现, ...

  8. C#并发实战Parallel.ForEach使用

    前言:最近给客户开发一个伙食费计算系统,大概需要计算2000个人的伙食.需求是按照员工的预定报餐计划对消费记录进行检查,如有未报餐有刷卡或者有报餐没刷卡的要进行一定的金额扣减等一系列规则.一开始我的想 ...

  9. Zookeeper_阅读源码第一步_在 IDE 里启动 zkServer(集群版)

    上篇文章Zookeeper_阅读源码第一步_在 IDE 里启动 zkServer(单机版)讲了在 idea 里以单机的方式启动zookeeper,这篇介绍一下以集群的方式启动. 集群方式启动,才会真正 ...

  10. ‎Cocos2d-x 学习笔记(3.2) TransitionScene 过渡场景和场景切换的过程

    1. 简介 过渡场景TransitionScene直接继承了场景Scene.能够在场景切换过程中实现“过渡”效果,而不是让窗口在下一帧突然展示另一个场景. 2. create 构造函数: Transi ...