CF 450E 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-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 数学+模拟的更多相关文章
- CF 990A. Commentary Boxes【数学/模拟】
[链接]:CF [题意]:对于一个数n,每次加一的代价是a,每次减一的代价是b,求被m整除时的最小代价. [分析]:分情况讨论,自己多举几个栗子. [代码]: #include<cstdio&g ...
- Codeforces 450E:Jzzhu and Apples(构造,数学)
E. Jzzhu and Apples time limit per test: 1 seconds memory limit per test: 256 megabytes input: stand ...
- 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 ...
- CF449 C. Jzzhu and Apples
/* http://codeforces.com/problemset/problem/449/C cf 449 C. Jzzhu and Apples 数论+素数+贪心 */ #include &l ...
- CF449C Jzzhu and Apples (筛素数 数论?
Codeforces Round #257 (Div. 1) C Codeforces Round #257 (Div. 1) E CF450E C. Jzzhu and Apples time li ...
- Codeforces 449C Jzzhu and Apples 贪心 (看题解)
Jzzhu and Apples 从大的质因子开始贪心, 如果有偶数个则直接组合, 如果是奇数个留下那个质数的两倍, 其余两两组合. #include<bits/stdc++.h> #de ...
- Codeforces 449.C Jzzhu and Apples
C. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard ...
- cf 443 D. Teams Formation](细节模拟题)
cf 443 D. Teams Formation(细节模拟题) 题意: 给出一个长为\(n\)的序列,重复\(m\)次形成一个新的序列,动态消除所有k个连续相同的数字,问最后会剩下多少个数(题目保证 ...
- Jzzhu and Apples CodeForces - 449C (构造,数学)
大意: 求从[1,n]范围选择尽量多的数对, 使得每对数的gcd>1 考虑所有除2以外且不超过n/2的素数p, 若p倍数可以选择的有偶数个, 直接全部划分即可 有奇数个的话, 余下一个2*p不划 ...
随机推荐
- JavaScript数据结构——字典和散列表的实现
在前一篇文章中,我们介绍了如何在JavaScript中实现集合.字典和集合的主要区别就在于,集合中数据是以[值,值]的形式保存的,我们只关心值本身:而在字典和散列表中数据是以[键,值]的形式保存的,键 ...
- jQuery插件之路(一)——试着给jQuery的一个Carousel插件添加新的功能
前几日在网上看到了一个关于Carousel插件的教学视频,于是也顺便跟着学习着做了一下.但是在做完之后发现,在别的网站上面看到类似的效果要比现在做的这个要多一个功能,也就是在底下会有一些按钮,当鼠标放 ...
- 逆向破解之160个CrackMe —— 004-005
CrackMe —— 004 160 CrackMe 是比较适合新手学习逆向破解的CrackMe的一个集合一共160个待逆向破解的程序 CrackMe:它们都是一些公开给别人尝试破解的小程序,制作 c ...
- java虚拟机学习笔记(六)---垃圾收集算法
主要讨论集中垃圾收集算法的思想及发展过程. 1.标记-清除法 最基础的收集算法是标记-清除法,算法分为标记和清除两个阶段:首先标记出所有需要回收的对象,在标记完成后统一回收所有被标记的对象,其标记过程 ...
- Netty学习(四)-TCP粘包和拆包
我们都知道TCP是基于字节流的传输协议.那么数据在通信层传播其实就像河水一样并没有明显的分界线,而数据具体表示什么意思什么地方有句号什么地方有分号这个对于TCP底层来说并不清楚.应用层向TCP层发送用 ...
- 第一次Git使用以及码云(Gitee)
下载安装Git,官网下载地址https://git-scm.com/downloads,我用的是Win10版,下载好后一路默认安装,安装时会给你自动添加环境变量,完成后打开cmd,输入git --ve ...
- Oralce PL/SQL 调用C
1.要把C写成扩展的形式 ex.c文件 int __declspec(dllexport) sum(int a,int b) { return a+b; } 2.把C代码编译成动态库(*.dll 或 ...
- pip安装时使用国内源,加快下载速度
国内源: 新版ubuntu要求使用https源,要注意. 清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:http://mirrors.aliyun.c ...
- Python RPC 之 gRPC
gRPC 简介: gRPC 是一款高性能.开源的 RPC 框架,产自 Google,基于 ProtoBuf 序列化协议进行开发,支持多种语言(Golang.Python.Java等),本篇只介绍 Py ...
- IoT时代:Wi-Fi“配网”技术剖析总结
导读 近年来,物联网市场竞争激烈,从物联网平台厂商,设备生产商,到服务提供商,都在涌入这片红海.预计到2020年,全球联网设备数量将达到260亿个,年复合增长率达到20%:全球联网设备带来的数据将达到 ...