大意: 求从[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 (构造,数学)的更多相关文章

  1. Codeforces 772C 构造 数学 + dp + exgcd

    首先我们能注意到两个数x, y (0 < x , y < m) 乘以倍数互相可达当且仅当gcd(x, m) == gcd(y, m) 然后我们可以发现我们让gcd(x, m)从1开始出发走 ...

  2. Codeforces 450E:Jzzhu and Apples(构造,数学)

    E. Jzzhu and Apples time limit per test: 1 seconds memory limit per test: 256 megabytes input: stand ...

  3. Codeforces 449C Jzzhu and Apples 贪心 (看题解)

    Jzzhu and Apples 从大的质因子开始贪心, 如果有偶数个则直接组合, 如果是奇数个留下那个质数的两倍, 其余两两组合. #include<bits/stdc++.h> #de ...

  4. 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 ...

  5. Codeforces 449.C Jzzhu and Apples

    C. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. CF 450E Jzzhu and Apples 数学+模拟

    E. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. CF449C Jzzhu and Apples (筛素数 数论?

    Codeforces Round #257 (Div. 1) C Codeforces Round #257 (Div. 1) E CF450E C. Jzzhu and Apples time li ...

  8. CF449 C. Jzzhu and Apples

    /* http://codeforces.com/problemset/problem/449/C cf 449 C. Jzzhu and Apples 数论+素数+贪心 */ #include &l ...

  9. Alice, Bob, Oranges and Apples CodeForces - 586E

    E - Alice, Bob, Oranges and Apples CodeForces - 586E 自己想的时候模拟了一下各个结果 感觉是不是会跟橘子苹果之间的比例有什么关系 搜题解的时候发现了 ...

随机推荐

  1. linux常用命令:systemctl 命令

    systemctl命令是系统服务管理器指令,它实际上将 service 和 chkconfig 这两个命令组合到一起. 1.命令格式: systemctl [参数] [服务] 2.命令功能: syst ...

  2. linux常用命令:find命令之xargs

    在使用 find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec执行.但有些系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出 ...

  3. python pip list 命令列出所有安装包和版本信息

    c:\Python27\Scripts>pip listDEPRECATION: The default format will switch to columns in the future. ...

  4. bzoj3524 [Poi2014]Couriers/2223 [Coci 2009]PATULJCI

    题目链接1 题目链接2 主席树模板题 两题有细节不同 #include<algorithm> #include<iostream> #include<cstdlib> ...

  5. Python之路----生成器函数进阶

    def generator(): print(123) yield 1 print(456) yield 2 g = generator() ret = g.__next__() print('*** ...

  6. Linux笔记 #07# 搭建机器学习环境

    环境: Debian 8.8 64位, 同样适用 win10 基本步骤: 安装 Python 安装必要的库 测试 一.安装 Python 延续之前的 搭建 Python 环境 选取折中版本 Pytho ...

  7. SNMP学习笔记之SNMPv3的配置和认证以及TroubleShooting

    0x00 增加snmp v3用户 增加用户的时候,必须要停止SNMP服务. # service snmpd stop # net-snmp-config --create-snmpv3-user -r ...

  8. SNMP学习笔记之SNMP 原理与实战详解

    原文地址:http://freeloda.blog.51cto.com/2033581/1306743 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法 ...

  9. JavaScript实现表单验证

    表单验证可以通过 JavaScript 来完成 以下示例代码用于判断表单字段(Name)值是否存在,如果存在,则弹出信息,否则阻止表单提交: <!DOCTYPE html> <htm ...

  10. 20165310 Java实验五《网络编程与安全》

    20165310 Java实验五<网络编程与安全> 任务一 题目:①编写MyBC.java实现中缀表达式转后缀表达式的功能:②编写MyDC.java实现从上面功能中获取的表达式中实现后缀表 ...