大意: 求从[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. Spring,Struts2,MyBatis,Activiti,Maven,H2,Tomcat集成(一)——Maven,Tomcat,Spring集成

    1.  创建Maven Web工程 (1)       磁盘上创建Maven工程所需要的文件夹结构如下: (2)       在与src同级目录中创建pom.xml文件: <project xm ...

  2. Linux服务器配置---ftp配置

    FTP其他配置 在配置文件中,还有一些关于vsftpd的其他设置,这里列出来 # Example config file /etc/vsftpd/vsftpd.conf # Allow anonymo ...

  3. mysql删除有外链索引数据,Cannot delete or update a parent row: a foreign key constraint fails 问题的解决办法

    mysql删除有外链索引数据Cannot delete or update a parent row: a foreign key constraint fails 问题的解决办法查询:DELETE ...

  4. 2016NOI冬令营day4

    上午:随机算法/近似算法与随机算法的分析方法与应用实例 不懂,完全滑水QAQ :( 下午:计算理论与NP问题 只有讲2-sat和3-sat的时候能听懂,其他的基本都在滑水:( 晚上说是什么中学生学术训 ...

  5. 【运维技术】JENKINS管道部署容器化初探

    目标服务器安装docker参考官方文档 https://docs.docker.com/install/linux/docker-ce/centos/ (可选)在目标服务器上安装docker私服 ht ...

  6. 手撕vue-cli配置——webpack.base.conf.js篇

    在开始写webpack.base.conf.js(简称base)之前,我们先来看一下vue-loader.conf.js这个文件,毕竟在base中我们还会用到: 'use strict' //引入前一 ...

  7. Jsoup解析网页html

    Jsoup解析网页html 解析网页demo: 利用Jsoup获取截图中的数据信息: html代码片段: <!-- 当前基金档案\计算\定投\开户 start --> <div cl ...

  8. C++面向对象高级开发课程(第一周)

    0. 内存分区 计算机中的内存在用于编程时,被人为的进行了分区(Segment),分为: -“栈区”(Stack) -“堆区”(Heap) -全局区(静态 区,Static) -文字常量区和程序代码区 ...

  9. 04: Form 验证用户数据 & 生成html

    目录:Django其他篇 01:Django基础篇 02:Django进阶篇 03:Django数据库操作--->Model 04: Form 验证用户数据 & 生成html 05:Mo ...

  10. 20145317 网络对抗技术 逆向与Bof基础

    20145317 网络对抗技术 逆向与Bof基础 实践要求 1. 掌握NOP,JNE,JE,JMP,CMP汇编指令的机器码 2. 掌握反汇编与十六进制编程器 3. 能正确修改机器指令改变程序执行流程 ...