Codeforces Round #257 (Div. 2) E题:Jzzhu and Apples 模拟
1 second
256 megabytes
standard input
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?
A single integer n (1 ≤ n ≤ 105),
the number of the apples.
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.
6
2
6 3
2 4
9
3
9 3
2 4
6 8
2
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 模拟的更多相关文章
- 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 ...
- Codeforces Round #257 (Div. 2) A题
A. Jzzhu and Children time limit per test 1 second memory limit per test 256 megabytes input standar ...
- 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 ...
- 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 ...
- Codeforces Round #378 (Div. 2) D题(data structure)解题报告
题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...
- Codeforces Round #612 (Div. 2) 前四题题解
这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...
- Codeforces Round #713 (Div. 3)AB题
Codeforces Round #713 (Div. 3) Editorial 记录一下自己写的前二题本人比较菜 A. Spy Detected! You are given an array a ...
- 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 ...
- 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 ...
随机推荐
- 【laravel54】如果开启了自带的时间戳(Y-h-m H:s:m),getInsertId一定要手动加上created_at 和 updated_at字段填充
[laravel54]如果开启了自带的时间戳(Y-h-m H:s:m),getInsertId一定要手动加上created_at 和 updated_at字段填充
- iOS UIKit Dynamics入门 UIKit动力学,实现重力、连接、碰撞、悬挂等动画效果
本文为转载文章 版权归原文所有 什么是UIKit动力学(UIKit Dynamics) 其实就是UIKit的一套动画和交互体系.我们现在进行UI动画基本都是使用CoreAnimation或者UIVie ...
- dokuwiki语法
dokuwiki是一个php写的维基系统,它的插件中包含markdown插件.但是markdown语法跟dokuwiki语法混着用会出现一些bug.所以还是学一下dokuwiki的语法吧. dokuw ...
- 系统加速解决方案之Windows XP
系统加速解决方案之Windows XP 在使用Windows XP的过程中,系统速度会随着时间的推移越来越慢,你可重装系统,但重装后,那么多的应用软件也要重新安装,如何在不安装系统的前提下提升Wind ...
- pthread到Win32thread
一.什么是线程. 线程(thread)是为了提高系统内程序的并发(concurrency)执行程度而提出来的概念,它是比进程更小的能够独立运行的基本单位.在引入线程的系统中,线程是处理器调度(sche ...
- 【Linux】Linux文件属性
既然说要了解Linux的文件属性,那么有个重要的也是常用的指令就必须要先介绍一下:就是『 ls 』这一个查看文件的命令!在以root的身份登入Linux之后,下达『 ls -al 』,结果如下所示: ...
- struts2 ValueStack详解,页面获取值el表达式、ognl表达式
http://www.blogjava.net/max/archive/2007/04/28/114417.html 我想用的是el表达式! http://www.cnblogs.com/belief ...
- 创建C3P0数据源
[Author]: kwu 创建C3P0数据源,实际开发中直接用JDBC连接数据库是非常少的.一般使用数据源的形式,C3P0是开源的数据源,实际项目用得非常多: 1.添加maven的支持 <de ...
- PYNQ = Python + ZYNQ —— ZYNQ部分功能的Python化
PYNQ优点:1. Python用于ZYNQ开发,Python库和FPGA硬件库可以直接调用,极大加快开发进程.缩短开发周期.降低开发难度,更方便.快捷:2. 用PYNQ开发,当Pytho ...
- CCTableView(一)
#ifndef __TABLEVIEWTESTSCENE_H__ #define __TABLEVIEWTESTSCENE_H__ #include "cocos2d.h" #in ...