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 ...
随机推荐
- poj 1054 The Troublesome Frog (暴力搜索 + 剪枝优化)
题目链接 看到分类里是dp,结果想了半天,也没想出来,搜了一下题解,全是暴力! 不过剪枝很重要,下面我的代码 266ms. 题意: 在一个矩阵方格里面,青蛙在里面跳,但是青蛙每一步都是等长的跳, 从一 ...
- hdu 4143 A Simple Problem (变形)
题目 题意:给n,求x; 直接枚举肯定超时, 把给的式子变形, (y+x)(y-x) = n; 令y-x = b, y+x = a; 枚举b, b 的范围肯定是sqrt(n), y = (a+b)/ ...
- MVC的项目使用html编辑器UEditorMINI
一个MVC的项目中有个发布新闻的页面需要用到一个html的编辑器,网上看到UEditor评价貌似还不错, 因为我用到的功能比较简单,就下载了MINI版本的, 使用的过程在这里总结一下. 关于UEdit ...
- ListView 使用
1. 不使用xml 文件 动态创建 Listview 并且绑定 ArrayList ListView listView = new ListView(this); listView.setAdapte ...
- Java Socket(2): 异常处理
1 超时 套接字底层是基于TCP的,所以socket的超时和TCP超时是相同的.下面先讨论套接字读写缓冲区,接着讨论连接建立超时.读写超时以及JAVA套接字编程的嵌套异常捕获和一个超时例子程序的抓包示 ...
- winform实现自动更新并动态调用form实现
winform实现自动更新并动态调用form实现 标签: winform作业dllbytenull服务器 2008-08-04 17:36 1102人阅读 评论(0) 收藏 举报 分类: c#200 ...
- .net 接口返回json格式示例
1.新建 InterfaceTestPro1 项目: FILE - New - Project... - Web - ASP.NET Web Forms Application name:Interf ...
- 典型的 SQL 注入过程(转)
无意间发现某站点存在 SQL 注入漏洞,于是利用这个漏洞提权并获取服务器控制权.这个案例很典型,像是教科书式的典型入侵步骤,下面就以这个案例展示从 SQL 注入到获取目标服务器控制权限的全过程. 发现 ...
- LightOJ 1427 -Repository(ac自动机)
题意: 求每个模式串在母串中出现的次数 #include <map> #include <set> #include <list> #include <cma ...
- Survival(ZOJ 2297状压dp)
题意:有n个怪,已知杀死第i个怪耗费的血和杀死怪恢复的血,和杀死boss耗的血,血量不能超过100,若过程中血小于0,则失败,问 是否能杀死boss(boss最后出现). 分析:就是求杀死n个怪后剩余 ...