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不划 ...
随机推荐
- Saving James Bond - Easy Version (MOOC)
06-图2 Saving James Bond - Easy Version (25 分) This time let us consider the situation in the movie & ...
- vue中的样式
一.使用class样式: CSS部分: <style> .green{ color:green; } .italic{ font-style:italic; } .thin{ ; } .a ...
- mysql系列学习
# 帮助help create user;# 查看当前用户select user();# 创建用户create user 'egon'@'localhost' identified by '123'; ...
- I understand that you would like to know about the Amazon Giveaway
Dear Seller, Greetings from Amazon Seller Support. From your mail, I understand that you would like ...
- DOM实战
作者声明:本博客中所写的文章,都是博主自学过程的笔记,参考了很多的学习资料,学习资料和笔记会注明出处,所有的内容都以交流学习为主.有不正确的地方,欢迎批评指正 视频来源:https://www.bil ...
- 《Linux内核与分析》第五周
20135130王川东 一.给MenuOS增加time和time-asm命令 命令:1.强制删除:rm menu -rf 2.克隆:git clone (后跟需要克隆数据所在的位置) 3.自动编译,自 ...
- 希尔排序(java实现)
上篇blog中介绍的直接插入排序,希尔排序就是对直接插入排序的一个优化.比如有这么一种情况:对一个无序数组进行从小到大的排序,但是数组的最后一个位置的数是最小的,我们要把它挪到第一个位置,其他位置的都 ...
- P4tutorial实战
Tutorial样例实战 GitHub仓库地址 参考博客 实验一:SIGCOMM_2015/Sourse_Routing 实验环境: OS:Ubuntu16.04 bmv2:behavioral-mo ...
- SDN前瞻 传统网络的缺陷
引言 在网络发展速度如此之快的今天,传统网络的架构充满了危机,主要有这四个问题: 传统网络部署管理困难. 分布式架构瓶颈出现. 流量控制难真正实现. 设备不可编程. 现在的网络厂商 种类繁多的网络厂商 ...
- mysql hql异常
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; nested ex ...