题目链接: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. npm在项目目录安装插件需要使用sudo

    今天使用node的npm安装插件的时候遇到一个问题,那就是在项目目录里面安装插件的时候,必须使用超级用户(sudo)执行才会安装成功,否则会报如下错误: 以安装 gulp-uglify 为例 $ np ...

  2. Linux“Bash”漏洞大爆发

    9月25日,国外曝出一个“毁灭级”的Bash漏洞,黑客可利用此漏洞远程执行任意命令,完全控制目标系统! 该漏洞编号为CVE-2014-6271,主要存在于bash 1.14 - 4.3版本中,受影响的 ...

  3. BNU 4188 Superprime Rib【BFS】

    题意:给出n,输出n位超级质数,超级质数的定义为“依次去掉右边一位后仍然为质数的数” 因为一个n位质数去掉右边一位数之后仍然为质数,说明它是由n-1位超级质数演变而来的, 同理,n-1位超级质数也由n ...

  4. 使php支持mbstring库

    多国语言并存就意味着多字节,PHP内置的字符串长度函数strlen无法正确处理中文字符串,它得到的只是字符串所占的字节数.对于GB2312的中文编码,strlen得到的值是汉字个数的2倍,而对于UTF ...

  5. 对于GLM的理解,与方差分析的对比

    最近遇到一个问题,如果因变量为一个连续变量(如胰岛素水平),主要考察的变量为分组变量(如正常血糖组,前糖尿病组,糖尿病组三组),现在的目的是想看调整多种变量(包括多个连续性变量和分类变量)后,胰岛素水 ...

  6. 04day2

    中位数 排序 [问题描述] 给出 1~n 的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是 b.中位数是指把所有元素从小到大排列后,位于中间的数.n<=100000 [输入] 第一 ...

  7. RTP封装h264

    网络抽象层单元类型 (NALU): NALU头由一个字节组成,它的语法如下: +---------------+      |0|1|2|3|4|5|6|7|      +-+-+-+-+-+-+-+ ...

  8. Android 超仿Path时间轴和扇形菜单的效果

    网上看到的  ,仅此记录一下,用到的时候便于查找 效果如下: 源码下载地址 :  http://download.csdn.net/detail/abc13939746593/7251933

  9. Ios 程序封装,安装流程

    转:http://www.myexception.cn/operating-system/1436560.html Ios 程序打包,安装流程 一.发布测试,是指将你的程序给   * 你的测试人员,因 ...

  10. CoreData 基本操作方法封装

    转:http://blog.csdn.net/marujunyy/article/details/18500523 为了方便使用CoreData 封装了几个扩展类,使用方法和类文件如下: //首先需要 ...