题目链接:http://codeforces.com/problemset/problem/449/C

给你n个数,从1到n。然后从这些数中挑选出不互质的数对最多有多少对。

先是素数筛,显然2的倍数的个数是最多的,所以最后处理。然后处理3,5,7,11...的倍数的数,之前已经挑过的就不能再选了。要是一个素数p的倍数个数是奇数,就把2*p给2

的倍数。这样可以满足p倍数搭配的对数是最优的。最后处理2的倍数就行了。

 #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + ;
bool prime[N] , vis[N];
int p[N / ];
vector <int> G[N]; void init() {
int index = ;
prime[] = true;
for(int i = ; i < N ; ++i) {
if(!prime[i]) {
p[++index] = i;
for(int j = i * ; j < N ; j += i)
prime[j] = true;
}
}
} int main()
{
init();
int n;
scanf("%d" , &n);
if(n < ) {
printf("0\n");
return ;
}
int cnt = ;
for(int i = ; p[i] * <= n ; ++i) {
for(int j = p[i] ; j <= n ; j += p[i]) {
if(!vis[j]) {
vis[j] = true;
G[p[i]].push_back(j);
}
}
if(G[p[i]].size() >= && (G[p[i]].size() % ))
G[].push_back(p[i] * );
cnt += G[p[i]].size() / ;
}
for(int i = ; i <= n ; i += ) {
if(!vis[i])
G[].push_back(i);
}
cnt += G[].size() / ;
printf("%d\n" , cnt);
for(int i = ; i < G[].size() ; i += )
printf("%d %d\n" , G[][i - ] , G[][i]);
for(int i = ; p[i] * <= n ; ++i) {
if(G[p[i]].size() % ) {
printf("%d %d\n" , G[p[i]][] , G[p[i]][]);
for(int j = ; j < G[p[i]].size() ; j += )
printf("%d %d\n" , G[p[i]][j - ] , G[p[i]][j]);
}
else {
for(int j = ; j < G[p[i]].size() ; j += )
printf("%d %d\n" , G[p[i]][j - ] , G[p[i]][j]);
}
}
return ;
}

Codeforces Round #257 (Div. 1) C. Jzzhu and Apples (素数筛)的更多相关文章

  1. Codeforces Round #511 (Div. 2)-C - Enlarge GCD (素数筛)

    传送门:http://codeforces.com/contest/1047/problem/C 题意: 给定n个数,问最少要去掉几个数,使得剩下的数gcd 大于原来n个数的gcd值. 思路: 自己一 ...

  2. Codeforces Round #257 (Div. 1)449A - Jzzhu and Chocolate(贪婪、数学)

    主题链接:http://codeforces.com/problemset/problem/449/A ------------------------------------------------ ...

  3. Codeforces Round #257 (Div. 2) A. Jzzhu and Children(简单题)

    题目链接:http://codeforces.com/problemset/problem/450/A ------------------------------------------------ ...

  4. 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 ...

  5. Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences (矩阵快速幂)

    题目链接:http://codeforces.com/problemset/problem/450/B 题意很好懂,矩阵快速幂模版题. /* | 1, -1 | | fn | | 1, 0 | | f ...

  6. Codeforces Round #257 (Div. 2) B Jzzhu and Sequences

    Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...

  7. Codeforces Round #257 (Div. 1) D - Jzzhu and Numbers 容斥原理 + SOS dp

    D - Jzzhu and Numbers 这个容斥没想出来... 我好菜啊.. f[ S ] 表示若干个数 & 的值 & S == S得 方案数, 然后用这个去容斥. 求f[ S ] ...

  8. Codeforces Round #257 (Div. 2) C. Jzzhu and Chocolate

    C. Jzzhu and Chocolate time limit per test 1 second memory limit per test 256 megabytes input standa ...

  9. Codeforces Round #257 (Div. 2) A. Jzzhu and Children

    A. Jzzhu and Children time limit per test 1 second memory limit per test 256 megabytes input standar ...

随机推荐

  1. NGUI监听事件

    using UnityEngine; using System.Collections; public class UIDataHandler : MonoBehaviour { public UII ...

  2. Flexigrid自定义显示数据列

    近期在搞ExtJs,发现ExJs的Grid相当的强大,后来又搞Jquery时,就对原来的表格不怎么满意了,于是,花了点时间,从网上找了个Grid插件,这个插件功能是比较强大,什么行排序.筛选.分页都有 ...

  3. UVA 11374 Airport Express(最短路)

    最短路. 把题目抽象一下:已知一张图,边上的权值表示长度.现在又有一些边,只能从其中选一条加入原图,使起点->终点的距离最小. 当加上一条边a->b,如果这条边更新了最短路,那么起点st- ...

  4. 使用Python脚本强化LLDB调试器

    LLDB是Xcode自带的调试器,作为一个iOS应用开发程序员,平时我在开发应用时会使用LLDB来调试代码.在逆向应用时,也会用到LLDB来跟踪应用的执行过程. LLDB还内置了一个Python解析器 ...

  5. Builder模式在Java中的应用(转)

    在设计模式中对Builder模式的定义是用于构建复杂对象的一种模式,所构建的对象往往需要多步初始化或赋值才能完成.那么,在实际的开发过程中,我们哪些地方适合用到Builder模式呢?其中使用Build ...

  6. [转](多实例)mysql-mmm集群

    一.需求说明 最近一直在学习mysql-mmm,想以后这个架构也能用在我们公司的业务上,我们公司的业务是单机多实例部署,所以也想把mysql-mmm部署成这样,功夫不负有心人,我成功了,和大家分享一下 ...

  7. 字符串截取数字和点击radio显示不同内容

    <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> & ...

  8. Eclipse小技巧<一>

    Eclipse是一款特别好用的开源开发工具,基于插件的特性使其能够进行各种语言的开发.非常喜欢eclipse里的编码风格,感觉这个开发工具十分灵活,又有很多开发的小技巧能够提高开发效率,每次学到一个t ...

  9. MySQL基础之第2章 Windows平台下安装与配置MySQL

    2.1.msi安装包 2.1.1.安装 特别要注意的是,安装前要删除原来的my.ini和原来的data目录,改名也行,不然在最后一步会“apply security settings”报个1045错误 ...

  10. iOS - NSLog、UncaughtException日志保存到文件

    转:http://blog.csdn.net/marujunyy/article/details/12005767 对于真机,日志没法保存,不好分析问题.所以有必要将日志保存到应用的Docunment ...