[codeforces] 449C Jzzhu and Apples
原题
质因数分解后贪心即可(最后贪2)
#include<cstdio>
#include<vector>
#include<stack>
#include<vector>
#include<cstring>
#define N 100010
using namespace std;
bool is[N],mk[N];
int prime[N],num=1,cnt,n,a,b;
stack <int> ans;
vector <pair <int,int> > v;
int main()
{
memset(is,1,sizeof(is));
for (int i=2;i<=100000;i++)
{
if (is[i]) prime[++cnt]=i;
for (int j=1;j<=cnt && i*prime[j]<=100000;j++)
{
is[i*prime[j]]=0;
if (i%prime[j]==0) break;
}
}
scanf("%d",&n);
for (int i=2;i<=cnt && prime[i]<=n;i++)
{
num=1;
while(!ans.empty()) ans.pop();
ans.push(prime[i]);
mk[prime[i]]=1;
for (int j=3*prime[i];j<=n;j+=prime[i])
{
if (mk[j]) continue;
mk[j]=1;
ans.push(j);
num++;
}
if (num&1 && 2*prime[i]<=n && !mk[2*prime[i]])
{
mk[2*prime[i]]=1;
ans.push(2*prime[i]);
}
while (ans.size()>1)
{
a=ans.top();
ans.pop();
b=ans.top();
ans.pop();
v.push_back(make_pair(a,b));
}
}
while(!ans.empty()) ans.pop();
num=0;
for (int i=2;i<=n;i+=2)
{
if (mk[i]) continue;
ans.push(i);
mk[i]=1;
}
while (ans.size()>1)
{
a=ans.top();
ans.pop();
b=ans.top();
ans.pop();
v.push_back(make_pair(a,b));
}
int s=v.size();
printf("%d\n",s);
for (int i=0;i<s;i++)
printf("%d %d\n",v[i].first,v[i].second);
return 0;
}
[codeforces] 449C Jzzhu and Apples的更多相关文章
- 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 ...
- Codeforces 450E:Jzzhu and Apples(构造,数学)
E. Jzzhu and Apples time limit per test: 1 seconds memory limit per test: 256 megabytes input: stand ...
- 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 ...
- CodeForces 450B Jzzhu and Sequences (矩阵优化)
CodeForces 450B Jzzhu and Sequences (矩阵优化) Description Jzzhu has invented a kind of sequences, they ...
- CF 450E Jzzhu and Apples 数学+模拟
E. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces C. Jzzhu and Cities(dijkstra最短路)
题目描述: Jzzhu and Cities time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
随机推荐
- 深入浅出:了解JavaScript的ES6、ES7新特性
参照阮一峰博客:http://es6.ruanyifeng.com/#README es6常见题:https://blog.csdn.net/qq_39207948/article/details/8 ...
- 第34-2题:LeetCode113. Path Sum II
题目 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum = 22, 5 / \ ...
- 爬虫学习(十五)——json解析
json与jsonpath 对象{}:jsonobject 对象:对象在js中表现为{}括起来的内容,数据结构为{key:value,key:value...}键值对的结构,在面向对象的结构中,key ...
- 原生Ajax发送请求
ajax get&post 1.使用get发送请求,会有请求缓存 1)什么叫请求缓存,请求信息相同浏览器不会再向服务器发送请求,导致访问服务器失败. 2)解决:将随机数添加到请求路径后面参数 ...
- RedHat6.4安装图形行化界面
1.1 打开电源进入RedHat shell命令行界面 1.2 查看系统镜像包括的所有软件包组信息 [root@zhongyi-test ~]# yum grouplist Loaded ...
- 【PHP】Maximum execution time of 30 seconds exceeded解决办法
Maximum execution time of 30 seconds exceeded,今天把这个错误的解决方案总结一下: 简单总结一下解决办法: 报错一:内存超限,具体报错语句忘了,简单说一下解 ...
- [CodeChef]RIN(最小割)
Description 有m门课可以在n个学期内学习,第i门课在第j个学期的收益是\(X_{i,j}\),一个学期可以学多门课,有的课之间有依赖关系,即必须先学a再学b,求最大收益.n,m<= ...
- ASP.NET 使用 MySQL
基本是通用的 C#与MySQL的交互, 先添加MySQL.Data.dll(位于MySQL安装目录下的Connector NET 8.0\Assemblies${version}目录下)引用, 之后代 ...
- 亲手搭建一个基于Asp.Net WebApi的项目基础框架1
目标:教大家搭建一个简易的前后端分离的项目框架. 目录: 1:关于项目架构的概念 2:前后端分离的开发模式 3:搭建框架的各个部分 这段时间比较闲,所以想把之前项目里用到的一些技术写到博客里来,分享给 ...
- Result Maps collection does not contain value for xxxx
这是mybatis查询返回值的错误,我在做一个查询数值的方法,但是我是这样写的: <select id="findSize" resultMap="long&quo ...