Codeforces 449.C 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的数两两分为一组,使得每组中的两个数gcd不为1,求最大组数.
分析:比较容易想到将数分为两大组.一组是2的倍数,一组是素数p以及p的倍数,在这两个互相制约的大组里选数拼起来.既然互相制约,那么就先分收益大的,这两个大组中的每两个数都可以拼成一个小组,如果先分第一个大组,那么第二个大组有的数就不能选,可能对于多个p组成的集合里面的数的个数都是奇数,性价比不高.所以先选第二个大组.对于每个质数p,现将p和p*3,p*4,.....这些数两两配对.如果最后还有一个数没有配对,就将它和p*2配对.这样第二大组的收益就最高了,接下来让第一大组尽量地分就好了.
至于为什么分成这样两个大组.一是要让每个分出来的大组中的任意两个数都能组成一个小组,满足题目给出的条件.二是这两个大组尽量不相交.除了2的质数都是奇数.一些偶数同时被分在两个大组是不可避免的,奇数如果不是质数,那么肯定存在于之前的一个质数p的集合中,否则就作为p.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; int tot,n,prime[],tot2,vis[],a[],b[],cnt1,cnt2,tot3,notuse[];
bool use[]; struct node
{
int x,y;
} e[]; void init()
{
for (int i = ; i <= n; i++)
{
if (!vis[i])
prime[++tot2] = i;
for (int j = ; j <= tot2; j++)
{
int t = prime[j] * i;
if (t > n)
break;
vis[t] = ;
if (i % prime[j] == )
break;
}
}
} int main()
{
scanf("%d",&n);
init();
for (int i = ; i <= tot2; i++)
{
memset(a,,sizeof(a));
cnt1 = ;
a[++cnt1] = prime[i];
for (int j = ; j * prime[i] <= n; j++)
if (!use[j * prime[i]])
a[++cnt1] = j * prime[i];
if ( * prime[i] <= n && !use[ * prime[i]])
{
if (cnt1 % == )
a[++cnt1] = * prime[i];
else
{
use[ * prime[i]] = ;
notuse[++tot3] = * prime[i];
}
}
for (int j = ; j + <= cnt1; j += )
{
e[++tot].x = a[j];
e[tot].y = a[j + ];
use[a[j]] = use[a[j + ]] = ;
}
}
for (int i = ; i * <= n; i++)
if (!use[i * ])
notuse[++tot3] = i * ;
for (int i = ; i + <= tot3; i += )
{
e[++tot].x = notuse[i];
e[tot].y = notuse[i + ];
}
printf("%d\n",tot);
for (int i = ; i <= tot; i++)
printf("%d %d\n",e[i].x,e[i].y); return ;
}
Codeforces 449.C Jzzhu and Apples的更多相关文章
- Codeforces 450E:Jzzhu and Apples(构造,数学)
E. Jzzhu and Apples time limit per test: 1 seconds memory limit per test: 256 megabytes input: stand ...
- Codeforces 449 B. Jzzhu and Cities
堆优化dijkstra,假设哪条铁路能够被更新,就把相应铁路删除. B. Jzzhu and Cities time limit per test 2 seconds memory limit per ...
- CF449 C. Jzzhu and Apples
/* http://codeforces.com/problemset/problem/449/C cf 449 C. Jzzhu and Apples 数论+素数+贪心 */ #include &l ...
- Codeforces 449C Jzzhu and Apples 贪心 (看题解)
Jzzhu and Apples 从大的质因子开始贪心, 如果有偶数个则直接组合, 如果是奇数个留下那个质数的两倍, 其余两两组合. #include<bits/stdc++.h> #de ...
- 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 ...
- CF449C Jzzhu and Apples (筛素数 数论?
Codeforces Round #257 (Div. 1) C Codeforces Round #257 (Div. 1) E CF450E C. Jzzhu and Apples time li ...
- CF 450E Jzzhu and Apples 数学+模拟
E. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #257 (Div. 1) C. Jzzhu and Apples (素数筛)
题目链接:http://codeforces.com/problemset/problem/449/C 给你n个数,从1到n.然后从这些数中挑选出不互质的数对最多有多少对. 先是素数筛,显然2的倍数的 ...
- Jzzhu and Apples CodeForces - 449C (构造,数学)
大意: 求从[1,n]范围选择尽量多的数对, 使得每对数的gcd>1 考虑所有除2以外且不超过n/2的素数p, 若p倍数可以选择的有偶数个, 直接全部划分即可 有奇数个的话, 余下一个2*p不划 ...
随机推荐
- DeepLearning - Regularization
I have finished the first course in the DeepLearnin.ai series. The assignment is relatively easy, bu ...
- 无人驾驶技术之Kalman Filter原理介绍
基本思想 以K-1时刻的最优估计Xk-1为准,预测K时刻的状态变量Xk/k-1,同时又对该状态进行观测,得到观测变量Zk,再在预测与观之间进行分析,或者说是以观测量对预测量进行修正,从而得到K时刻的最 ...
- 支持向量机SVM 初识
虽然已经学习了神经网络和深度学习并在几个项目之中加以运用了,但在斯坦福公开课上听吴恩达老师说他(在当时)更喜欢使用SVM,而很少使用神经网络来解决问题,因此来学习一下SVM的种种. 先解释一些概念吧: ...
- Thunder团队第七周 - Scrum会议4
Scrum会议4 小组名称:Thunder 项目名称:i阅app Scrum Master:翟宇豪 工作照片: 宋雨在照相,所以不在相片中. 参会成员: 王航:http://www.cnblogs.c ...
- Android开发随笔2
昨天:对anroid的系统架构了解比如:基于linux内核,整合库函数和java编译器并且为上层提供封装好的api和一些基本系统级应用 创建一个安卓的模拟器 了解了ddms的作用和内容 利用已有的工具 ...
- Lucene 常用名词解析
索引的创建:IndexWriter: 用于创建索引Directory: 这个可以用来定义我们的索引是存放在内存中还是在硬盘上Analyzer: 分词器 有几种()这个地方需要好好解释下Document ...
- QMetaEnum利用Qt元数据实现枚举(enum)类型值及字符串转换
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QMetaEnum利用Qt元数据实现枚举(enum)类型值及字符串转换 本文地址:ht ...
- pip使用国内镜像源
windows版 1.在windows文件管理器中,输入 %APPDATA% 2.在该目录下新建pip文件夹,然后到pip文件夹里面去新建个pip.ini文件 3.在新建的pip.ini文件中输入以下 ...
- 【经典数据结构】Trie
在计算机科学中,trie,又称前缀树或字典树,是一种有种树,用于保存关联数组,其中的键通常是字符串.与二叉查找树不同,键不是直接保存在节点中,而是由节点在树中的位置决定.一个节点的所有子孙都有相同的前 ...
- Android四大组件之Intent
Intent不是android几大组件框架,但是确实是android 各大组件之间沟通的桥梁. 尤其Intent对于activity有很大的关系. 一下是我个人对task以及backstack的总结.