Jzzhu and Apples CodeForces - 449C (构造,数学)
大意: 求从[1,n]范围选择尽量多的数对, 使得每对数的gcd>1
考虑所有除2以外且不超过n/2的素数p, 若p倍数可以选择的有偶数个, 直接全部划分即可
有奇数个的话, 余下一个2*p不划分, 其余全部划分
最后再将2的倍数全部划分一下即可
#include <iostream>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <cstdio>
#include <vector>
#define x first
#define y second
#define pb push_back
#define REP(i,a,n) for(int i=a;i<=n;++i)
using namespace std;
typedef pair<int,int> pii; const int N = 1e5+10;
int n;
int vis[N], p[N];
vector<pii> ans; void seive(int n) {
int mx = sqrt(n+0.5);
REP(i,2,mx) if (!vis[i]) {
for (int j=i*i; j<=n; j+=i) {
vis[j] = 1;
}
}
REP(i,3,n) if (!vis[i]) p[++*p]=i;
} int main() {
scanf("%d", &n);
seive(n/2);
memset(vis, 0, sizeof vis);
vector<int> ret;
REP(i,1,*p) {
vector<int> num;
for (int t=p[i]; t<=n; t+=p[i]) {
if (!vis[t]) num.pb(t);
}
while (num.size()>=2) {
int x = num.back();
num.pop_back();
vis[x] = 1;
int y = num.back();
num.pop_back();
vis[y] = 1;
if (y==2*p[i]) {
ret.pb(y);
y = p[i];
vis[y] = 1;
}
ans.pb(pii(x,y));
}
}
for (int t=2; t<=n; t<<=1) ret.pb(t);
while (ret.size()>=2) {
int x = ret.back();
ret.pop_back();
int y = ret.back();
ret.pop_back();
ans.pb(pii(x,y));
}
printf("%d\n", int(ans.size()));
for (auto t:ans) {
printf("%d %d\n",t.x,t.y);
}
}
Jzzhu and Apples CodeForces - 449C (构造,数学)的更多相关文章
- Codeforces 772C 构造 数学 + dp + exgcd
首先我们能注意到两个数x, y (0 < x , y < m) 乘以倍数互相可达当且仅当gcd(x, m) == gcd(y, m) 然后我们可以发现我们让gcd(x, m)从1开始出发走 ...
- Codeforces 450E:Jzzhu and Apples(构造,数学)
E. Jzzhu and Apples time limit per test: 1 seconds memory limit per test: 256 megabytes input: stand ...
- 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 ...
- Codeforces 449.C Jzzhu and Apples
C. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard ...
- CF 450E 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 ...
- CF449 C. Jzzhu and Apples
/* http://codeforces.com/problemset/problem/449/C cf 449 C. Jzzhu and Apples 数论+素数+贪心 */ #include &l ...
- Alice, Bob, Oranges and Apples CodeForces - 586E
E - Alice, Bob, Oranges and Apples CodeForces - 586E 自己想的时候模拟了一下各个结果 感觉是不是会跟橘子苹果之间的比例有什么关系 搜题解的时候发现了 ...
随机推荐
- nginx 配置https没有ssl_module以及一些错误
一:开始Nginx的SSL模块 1.1 Nginx如果未开启SSL模块,配置Https时提示错误 1 nginx: [emerg] the "ssl" parameter requ ...
- 使用 SSH 和 SFTP 协议
通过 SSH 和 SFTP 协议,我们能够访问其他设备,有效而且安全的传输文件等等. 几年前,我决定配置另外一台电脑,以便我能在工作时访问它来传输我所需要的文件.要做到这一点,最基本的一步是要求你的网 ...
- MySQL数据库----表与表之间的关系
表1 foreign key 表2 则表1的多条记录对应表2的一条记录,即多对一 利用foreign key的原理我们可以制作两张表的多对多,一对一关系 多对多: 表1的多条记录可以对应表2的一条记录 ...
- python之路----钻石继承
钻石继承 继承顺序 class A(object): def test(self): print('from A') class B(A): def test(self): print('from B ...
- 推荐:Java性能优化系列集锦
Java性能问题一直困扰着广大程序员,由于平台复杂性,要定位问题,找出其根源确实很难.随着10多年Java平台的改进以及新出现的多核多处理器,Java软件的性能和扩展性已经今非昔比了.现代JVM持续演 ...
- Notes of Head.First.HTML.and.CSS.2nd.Edition
What does the web server do? tirelessly waiting for requests from webbrowsers What does the web brow ...
- Python入门之获取当前所在目录的方法详解
#本文给大家讲解的是使用python获取当前所在目录的方法以及相关示例,非常的清晰简单,有需要的小伙伴可以参考下 sys.path 模块搜索路径的字符串列表.由环境变量PYTHONPATH初始化得到. ...
- python Django编写登录项目
Django 目录结构: __init__.py 文件: import pymysql pymysql.install_as_MySQLdb() 注意:如果 import pymysql 的时候报红, ...
- 02: tornado进阶篇
目录:Tornado其他篇 01: tornado基础篇 02: tornado进阶篇 03: 自定义异步非阻塞tornado框架 04: 打开tornado源码剖析处理过程 目录: 1.1 自定制t ...
- mysql-cluster 7.3.5安装部署
集群环境 管理节点 10.0.0.19 数据节点 10.0.0.12 10.0.0.17 sql节点 10.0.0.18 10.0.0.22 添加mysql用户 groupadd mysql user ...