Codeforces 450E:Jzzhu and Apples(构造,数学)
E. Jzzhu and Apples
time limit per test: 1 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output
Jzzhu has picked \(n\) apples from his big apple tree. All the apples are numbered from \(1\) to \(n\). Now he wants to sell them to an apple store.
Jzzhu will pack his apples into groups and then sell them. Each group must contain two apples, and the greatest common divisor of numbers of the apples in each group must be greater than \(1\). Of course, each apple can be part of at most one group.
Jzzhu wonders how to get the maximum possible number of groups. Can you help him?
Input
A single integer \(n (1 ≤ n ≤ 10^5)\), the number of the apples.
Output
The first line must contain a single integer \(m\), representing the maximum number of groups he can get. Each of the next m lines must contain two integers — the numbers of apples in the current group.
If there are several optimal answers you can print any of them.
input
6
output
2
6 3
2 4
input
9
output
3
9 3
2 4
6 8
input
2
output
0
题意
给出正整数\(n\),求出\(\left[1,n\right]\)之间的正整数有多少对数字的最大公约数不等于\(1\),输出最多的组数,并按任意顺序输出这些数字
思路
要使\(gcd(x,y)>1\),那么\(x,y\)中的较小的数一定不大于\(n/2\),所以我们首先筛出来\([1,n/2]\)范围内的素数
筛出来素数之后,每次在取数的时候,要保证取完数之后,不会使总的符合要求的数对减少,所以我们从最大的素数(假设为\(x\))开始枚举,能够整除\(x\)的整数一定使最少的,而且不会影响到别的数对(感性理解一下:因为枚举到了\(n/2\),所以\(n/x<=3\),\(2\)的倍数还是很多的减少一个没什么影响,\(n/x=3\)时,大概只有\(n=9\)的时候,那么也是不会产生影响的)
然后去统计当前素数\(p\)的倍数个数\(num\),如果\(num\)是偶数,直接匹配(为了简便,就相邻的两个数组成一对)
如果\(num\)是奇数,我们可以将\(p\)的\(2\)倍和\(num\)倍交换位置,这样匹配完剩下的那个数可以去和\(2\)的倍数来匹配,这样可以达到最优
代码
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e6+10;
const int mod=1e9+7;
const int maxm=1e3+10;
using namespace std;
int vis[maxn];
int prime[maxn];
int cnt;
int a[maxn];
void get_prime(int n)
{
vis[0]=vis[1]=1;
for(int i=2;2*i<=n;i++)
if(!vis[i])
for(int j=2;j*i*2<=n;j++)
vis[j*i]=1;
for(int i=2;2*i<=n;i++)
if(!vis[i])
prime[cnt++]=i;
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("/home/wzy/in", "r", stdin);
freopen("/home/wzy/out", "w", stdout);
srand((unsigned int)time(NULL));
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
get_prime(n);
ms(vis,0);
vector<pair<int,int> >ve;
for(int i=cnt-1;i>=0;i--)
{
int tot=0;
for(int j=prime[i];j<=n;j+=prime[i])
if(!vis[j])
a[++tot]=j;
// 如果倍数有奇数个,交换第二个和最后一个
if(tot&1)
swap(a[2],a[tot]);
for(int j=1;j+1<=tot;j+=2)
{
vis[a[j]]=vis[a[j+1]]=1;
ve.push_back({a[j],a[j+1]});
}
}
cout<<ve.size()<<endl;
for(auto i:ve)
cout<<i.first<<" "<<i.second<<endl;
#ifndef ONLINE_JUDGE
cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
#endif
return 0;
}
Codeforces 450E:Jzzhu and Apples(构造,数学)的更多相关文章
- CF 450E Jzzhu and Apples 数学+模拟
E. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces 449C Jzzhu and Apples 贪心 (看题解)
Jzzhu and Apples 从大的质因子开始贪心, 如果有偶数个则直接组合, 如果是奇数个留下那个质数的两倍, 其余两两组合. #include<bits/stdc++.h> #de ...
- [codeforces] 449C Jzzhu and Apples
原题 质因数分解后贪心即可(最后贪2) #include<cstdio> #include<vector> #include<stack> #include< ...
- 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 ...
- 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 ...
- Codeforces C. Jzzhu and Cities(dijkstra最短路)
题目描述: Jzzhu and Cities time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
随机推荐
- 为 Rainbond Ingress Controller 设置负载均衡
Rainbond 作为一款云原生应用管理平台,天生带有引导南北向网络流量的分布式网关 rbd-gateway.rbd-gateway 组件,实际上是好雨科技团队开发的一种 Ingress Contro ...
- 学习Java的第四天
一.今日收获 1.java完全手册的第一章 2. 1.6节了解了怎么样用记事本开发java程序 与用Eclipse开发 2.完成了对应例题 二.今日难题 1.一些用法容易与c++的混淆 2.语句还 ...
- 关于stm32不常用的中断,如何添加, 比如timer10 timer11等
首先可以从keil中找到 比如找到定时器11的溢出中断,如上图是26 然后,配置定时器11 溢出中断的时候,我就在:下面填上这个变量. 之后要写中断服务函数,也就是发生中断后要跳转到的函数. 需要知道 ...
- linux-源码软件管理-yum配置
总结如下:1.源码配置软件管理2.配置yum本地源和网络源及yum 工作原理讲解3.计算机硬盘介绍 1.1 源码管理软件 压缩包管理命令: # 主流的压缩格式包括tar.rar.zip.war.gzi ...
- 【Linux】【Services】【Web】Haproxy
1. 概念 1.1. 官方网站 http://www.haproxy.org/ 2. 安装 yum安装 yum -y install haproxy keepalived 配置haproxy日志,修改 ...
- 用graphviz可视化决策树
1.安装graphviz. graphviz本身是一个绘图工具软件,下载地址在:http://www.graphviz.org/.如果你是linux,可以用apt-get或者yum的方法安装.如果是w ...
- 【HarmonyOS】【DevEco Studio】NOTE04:How to Jump to a Page(页面间的跳转)
页面创建与基本设置 创建页面 创建两个新页面,分别为AbilityPage1.AbilityPage2 设置页面基本内容 以AbilityPage1为例 导包 import com.example.m ...
- Git忽略提交规则 .gitignore文件
在使用Git的过程中,我们喜欢有的文件比如日志,临时文件,编译的中间文件等不要提交到代码仓库,这时就要设置相应的忽略规则,来忽略这些文件的提交.简单来说一个场景:在你使用git add .的时候,遇到 ...
- Apache Log4j 远程代码执行漏洞源码级分析
漏洞的前因后果 漏洞描述 漏洞评级 影响版本 安全建议 本地复现漏洞 本地打印 JVM 基础信息 本地获取服务器的打印信息 log4j 漏洞源码分析 扩展:JNDI 危害是什么? GitHub 项目 ...
- supermarket(uaf)!!!!
在这道题目我花费了很长的时间去理解,因为绕进了死圈子 例行检查我就不放了 关键处在于选择5 使用了realloc,却没有让结构体指针node-> description正确指回去 (11条消息) ...