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不划 ...
随机推荐
- Maven项目的打包发布到Nexus私服和服务器
1.编写pom文件如下: <build> <plugins> <plugin> <groupId>org.apache.maven.plugins< ...
- 详解 Diff 算法以及循环要加 key 值问题
上一篇文章我简述了什么是 Virtual DOM,这一章我会详细讲 Diff 算法以及为什么在 React 和 Vue 中循环都需要 key 值. 什么是 DOM Diff 算法 Web 界面其实就是 ...
- kylin Retrieving hive dependency...
由于公司环境配置hive默认连接hiveserver2 ,不管hive cli 还是beeline cli都默认使用beeline cli,连接hive需要输入账号密码; 启动kylin 时会Retr ...
- Altium Designer16设置GND和VCC线宽规则的一种操作方法及注意事项
昨天看到学弟在画电路板,看到他设置电源线线宽时出了一点问题,设置的规则最开始有作用,后来重新从原理图导入更新PCB时,电源线变绿,规则设置点更新也没有用.接下来是操作步骤: 第一步:点击Design- ...
- JS构建多端应用
JS构建多端应用 一,需求与介绍 1.1,介绍 1,Taro 是一套遵循 React语法规范的 多端开发 解决方案.现如今市面上端的形态多种多样,Web.React-Native.微信小程序等各种端大 ...
- Unix-IO-同步,异步,阻塞,非阻塞-笔记篇
概念更正 https://www.zhihu.com/question/19732473 错误的四个象限分类 https://www.ibm.com/developerworks/cn/linux/l ...
- Docker进阶-容器监控cAdvisor+InfluxDB+Granfana
概述 前面文章介绍使用docker compose组合应用并利用scale快速对容器进行扩容. 由于docker compose启动的服务都在同一台宿主机上,对于一个宿主机上运行多个容器应用时,容器的 ...
- mybatis基础简介
1.mybatis的加载过程? 程序首先加载mybatis-config.xml文件,根据配置文件创建SQLSessionFactory对象: 然后通过SQLSessionFactory对象创建 ...
- Jenkins使用aqua-microscanner-plugin进行容器漏洞扫描
官方地址:https://github.com/jenkinsci/aqua-microscanner-plugin Step1 在jenkins安装"Aqua MicroScanner&q ...
- VR、AR、MR、CR 与 AI与SaaS、CRM、MRP与B2B、B2C、C2C、O2O、P2P
一.VR.AR.MR.CR VR ( Virtual Reality ),虚拟现实 AR(Augmented Reality),增强现实 MR(Mix Reality),混合现实 CR(Cinema ...