Codeforces Round #257 (Div. 1) C. Jzzhu and Apples (素数筛)
题目链接: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 (素数筛)的更多相关文章
- Codeforces Round #511 (Div. 2)-C - Enlarge GCD (素数筛)
传送门:http://codeforces.com/contest/1047/problem/C 题意: 给定n个数,问最少要去掉几个数,使得剩下的数gcd 大于原来n个数的gcd值. 思路: 自己一 ...
- Codeforces Round #257 (Div. 1)449A - Jzzhu and Chocolate(贪婪、数学)
主题链接:http://codeforces.com/problemset/problem/449/A ------------------------------------------------ ...
- Codeforces Round #257 (Div. 2) A. Jzzhu and Children(简单题)
题目链接:http://codeforces.com/problemset/problem/450/A ------------------------------------------------ ...
- 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 ...
- Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences (矩阵快速幂)
题目链接:http://codeforces.com/problemset/problem/450/B 题意很好懂,矩阵快速幂模版题. /* | 1, -1 | | fn | | 1, 0 | | f ...
- 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 ...
- Codeforces Round #257 (Div. 1) D - Jzzhu and Numbers 容斥原理 + SOS dp
D - Jzzhu and Numbers 这个容斥没想出来... 我好菜啊.. f[ S ] 表示若干个数 & 的值 & S == S得 方案数, 然后用这个去容斥. 求f[ S ] ...
- 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 ...
- 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 ...
随机推荐
- 【Todo】抽象渗漏法则 & 找到理想员工 & 软件开发成功 12 法则 & Joel on Software
Joel应该是个软件专家,这是他文章汇总的中文版本: http://local.joelonsoftware.com/wiki/Chinese_%28Simplified%29 其中有几篇值得好好看看 ...
- hdu 4968 Improving the GPA (水 暴力枚举)
题目链接 题意:给平均成绩和科目数,求可能的最大学分和最小学分. 分析: 枚举一下,可以达到复杂度可以达到10^4,我下面的代码是10^5,可以把最后一个循环撤掉. 刚开始以为枚举档次的话是5^10, ...
- Request.Querystring中文乱码问题解决
现象:近期项目中用到查询字符串传值,如果传递的是英文一切正常,但是传递中文时,使用request.querystring[]得到的是乱码. 原因:不知道为什么,可能是编码不一致问题 解决方法1:修改w ...
- 把 HttpHandler.ashx 修改为 异步编程 异步操作
在 ASP.NET 中,所有的处理程序类必须实现 IHttpHandler 接口或者实现 IHttpAsyncHandler 接口,这两个接口的区别是前者是一个同步接口,后者是一个异步处理模式的接口. ...
- DirectShow建立一个视频捕捉程序
DirectShow 提供了用应用程序从适当的硬件中捕捉和预览音/视频的能力.数据源包括:VCR,camera,TV tuner,microphone,或其他的数据源.一个应用程序可以立刻显示捕捉的数 ...
- UVa 11729 Commando War 突击战
你有 n 个部下,每个部下需要完成一个任务.第 i 个部下需要你花 Bi 分钟交待任务,然后他会立刻独立地.无间断地执行 Ji 分钟后完成任务.你需要选择交待任务的顺序,使得所有任务尽早执行完毕(即最 ...
- 【转】Android fill_parent和wrap_content分析
fill_parent设置一个顶部布局或控件强制性让它布满整个屏幕. wrap_content布局指根据视图内部内容自动扩展以适应其大小. 1. wrap_content <?xml versi ...
- Windows环境自动获取AWR报告
1.双击awr.cmd,通过cmd窗口运行awr.sql cmd.exe /c sqlplus lcam_1230/zcpzg1z_1230@54_orcl @awr.sql awr.cmd 2.aw ...
- Oracle 课程八之跟踪事件set event
一.Oracle跟踪文件 Oracle跟踪文件分为三种类型: 一种是后台报警日志文件,记录数据库在启动.关闭和运行期间后台进程的活动情况,如表空间创建.回滚段创建.某些alter命令.日志切换.错误消 ...
- xcode升级,报错 libxml/tree.h not found (Xcode4.6解决方案)
转:http://blog.csdn.net/yangxuanlun/article/details/8639075 Xcode升级到4.6以后,他妈的,libxml/tree.h找不到了,搞了大半天 ...