大意: 求从[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. sp3485推荐电路(转)

    源: sp3485推荐电路 注意:转自电子发烧友 转:485通信自动收发电路 转: RS485收发的3种典型电路-重点-自动收发电路

  3. loj10009 P1717 钓鱼

    P1717 钓鱼 贪心+优先队列 先枚举最后走到哪个湖,然后用优先队列跑一遍贪心即可 #include<iostream> #include<cstdio> #include& ...

  4. Android MediaPlayer 操作

  5. linux平台关闭某个进程的脚本

    在开发LINUX平台下的程序时,经常需要为我们的开发的程序写启动程序和关闭程序的脚本. 启动脚本比较好做,关闭程序脚本如下: 具体思路是通过ps命令找到程序的进程ID号,然后通过Kill命令将程序Ki ...

  6. FTP-FileZilla

    服务器上安装FileZilla Server连接时报You appear to be behind a NAT router. Please configure the passive mode se ...

  7. dll和ocx的简单理解

    一.dll dll就是打包一些程序或者算法,根据我的理解分个类 1.算法的打包 比如打包C/C++的一些纯代码算法,计算平均值,极值,标准差....,只需要向外提供接口和入口参数,外部即可轻松调用 2 ...

  8. luogu P2114 [NOI2014]起床困难综合症 位运算 二进制

    建议去uoj那里去测,数据比较强 位运算的题目,就得一位一位的分开考虑 然后枚举初始值的最高位是0 是1 的最终攻击 (二进制内)最高位是1肯定比次位是1次次位是1次次次位是1···的大吧,显然 然后 ...

  9. v-model双向数据绑定

    v-model双向数据绑定的修饰符 .lazy:失去焦点时数据进行双向的绑定 v-model.lazy=”message ” .number:前面输入数字,后面接着字母自动去除掉.v-model. n ...

  10. each遍历小结

    JQ中的遍历函数 (逐个加工函数) 格式: $(‘.box p’).each(function(index,element){ })也可以写成 $.each(‘.box p’,function(ind ...