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 自己想的时候模拟了一下各个结果 感觉是不是会跟橘子苹果之间的比例有什么关系 搜题解的时候发现了 ...
随机推荐
- mysql分类和事务回滚
主要内容: ***数据定义语言DDL重点 ***数据操纵语言DML重点 数据查询语言DQL重点 ---事务控制语言TCL ---数据库控制语言DCL ---主键(primary key) ---数据冗 ...
- Python入门之Python的单例模式和元类
一.单例模式 单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在. 当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上 ...
- P1283 平板涂色
P1283 平板涂色 dfs 记忆化搜索 将矩阵转化为图求解,然后我们发现这是个DAG,于是就可以愉快地跑搜索了. 进行dfs时,我们可以用类似拓扑排序的方法.每次将上面所有矩形都被刷过(入度in[ ...
- Mysql的序列
Mysql的序列 Mysql自带的序列:字段设置为int,属性里面选上“自动增长”即可: 在插入数据的时候可以不插入该字段的值,mysql会自动处理:
- 贪婪算法(Greedy Algorithm)
Greedy Algorithm <数据结构与算法--C语言描述> 图论涉及的三个贪婪算法 Dijkstra 算法 Prim 算法 Kruskal 算法 Greedy 经典问题:coin ...
- jquery插件--问题类(新增&&删除)简易版
HTML: <!doctype html> <head> <meta charset="utf-8" /> <script src=&qu ...
- Linq 对List的一些操作
代码: public class Person { public int ID { get; set; } public string Name { get; set; } public int Ag ...
- css未知大小的图片居中
未知大小的图片在指定大小的div盒子中垂直水平居中: 无需要JS <style> .box { /*垂直居中*/ display: table-cell; vertical-align:m ...
- 02_Flume1.6.0安装及单节点Agent实践
Flume1.6.0的安装1.上传Flume-1.6.0-tar.gz到待部署的所有机器 以我的为例: /usr/local/src/ 2.解压得到flume文件夹 # tar -x ...
- UVa 10881 蚂蚁
https://vjudge.net/problem/UVA-10881 题意: 一根长度为L厘米的木棍上有n只蚂蚁,每只蚂蚁要么朝左爬,要么朝右爬,速度为1厘米/秒.当两只蚂蚁相撞时,二者同时掉头. ...