Codeforces Round #257 (Div. 1) C

Codeforces Round #257 (Div. 1) E

CF450E

C. Jzzhu and Apples
time limit per test

1 second

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 ≤ 105), 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.

Sample test(s)
Input
6
Output
2
6 3
2 4
Input
9
Output
3
9 3
2 4
6 8
Input
2
Output
0

题意:有N个苹果,编号为1~N。现要将苹果组成若干对,每对苹果最小公约数不为1。求最多能分成多少对,输出各对的组成。

题解:先筛素数,然后搞。

首先我们怕的是乱选了两个数组成了公约数不为1的一对,但是这导致了总对数减少(这两个数分别被别的数需要,它们组成一对了不是最优解)。为了防止这种情况,我们要想办法让总对数不会减少。

我们发现2的倍数们非常碉炸,任意2个就能组成1对,所以我们先弄其他的数,最后再搞2的倍数。

我们发现一个质数x的1倍、2倍、3倍、……?倍中未使用的数组成的集合,也可以任意两两组合,但是如果在1~n之间,这个集合的元素个数是奇数,就会多一个。为了不造成多余的影响,我们把2*x作为多出来的一个,扔到2的倍数中去。这样,各种集合的多出来的一个,肯定能找到配对。把我们使用的数标记一下,防止搞其他质数的时候重复用一个数。

先搞完3到小于等于(N/2)的质数(大于N/2,它的倍数的集合就只有它自己了,没法玩),然后搞2的倍数,把之前扔进来的2*x们和其他2*y(y是合数)组成一个大集合,两两配对,最后再多出来一个也没办法了,这已经是最多的配对了。

解不唯一,我们这样搞肯定能找到最多的对数,碉炸。

代码:

 //#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usll unsigned ll
#define mz(array) memset(array, 0, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) prllf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("1biao.out","w",stdout)
#define mp make_pair
#define pb push_back const long N = ;
int prime[N],pn = ;
bool isnp[N];
void shai() {
int i,j;
memset(prime,,sizeof(prime));
memset(isnp,,sizeof(isnp));
isnp[]=,isnp[]=;
pn=;
for(i = ; i < N ; i ++) {
if(! isnp[i])
prime[pn ++]=i;
//关键处1
for(j = ; j < pn && i * prime[j] < N ; j ++) {
isnp[i * prime[j]] = ;
if( !(i % prime[j] ) ) //关键处2
break;
}
}
} int n;
vector<int>a,a2;
vector<pair<int,int> >v;
bool used[N];
int main() {
int i,j,k;
int l,r,mid;
int pre;
int ans;
shai();
while(scanf("%d",&n)!=EOF) {
ans=;
v.clear();
a2.clear();
memset(used,,sizeof(used)); for(i=; prime[i]<=n/; i++) {
a.clear();
a.pb(prime[i]);
for(j=*prime[i]; j<=n; j+=prime[i]) if(!used[j]) a.pb(j);
if(a.size()%==) a2.pb(*prime[i]);
else a.pb(*prime[i]);
int maxj=a.size();
for(j=; j+<maxj; j+=) {
v.pb(mp(a[j],a[j+]));
used[a[j]]=;
used[a[j+]]=;
}
} if(n>=)a2.pb();
if(n>=)a2.pb();
for(i=; i+i<=n; i++) {
if(!used[i+i] && isnp[i]) a2.pb(i+i);
}
int maxi=a2.size();
for(i=; i+<maxi; i+=) v.pb(mp(a2[i],a2[i+]));
printf("%d\n",v.size());
maxi=v.size();
REP(i,maxi) printf("%d %d\n",v[i].first,v[i].second);
}
return ;
}

CF449C Jzzhu and Apples (筛素数 数论?的更多相关文章

  1. CF449C Jzzhu and Apples

    嘟嘟嘟 这道题正解是怎么对的其实我也不清楚,总之靠感性理解吧. 首先当然要把1到n / 2的素数都筛出来,因为两两能配对的数一定都是这些素数的倍数.这也就说明对于(n / 2, n]的素数,他们一定不 ...

  2. CF449 C. Jzzhu and Apples

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

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

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

  4. Codeforces 449.C Jzzhu and Apples

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

  5. 【板子】gcd、exgcd、乘法逆元、快速幂、快速乘、筛素数、快速求逆元、组合数

    1.gcd int gcd(int a,int b){ return b?gcd(b,a%b):a; } 2.扩展gcd )extend great common divisor ll exgcd(l ...

  6. 洛谷P3383 【模板】线性筛素数

    P3383 [模板]线性筛素数 256通过 579提交 题目提供者HansBug 标签 难度普及- 提交  讨论  题解 最新讨论 Too many or Too few lines 样例解释有问题 ...

  7. poj3126 筛素数+bfs

    //Accepted 212 KB 16 ms //筛素数+bfs #include <cstdio> #include <cstring> #include <iost ...

  8. 洛谷 P3383 【模板】线性筛素数

    P3383 [模板]线性筛素数 题目描述 如题,给定一个范围N,你需要处理M个某数字是否为质数的询问(每个数字均在范围1-N内) 输入输出格式 输入格式: 第一行包含两个正整数N.M,分别表示查询的范 ...

  9. POJ2689-Prime Distance-区间筛素数

    最近改自己的错误代码改到要上天,心累. 这是迄今为止写的最心累的博客. Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total S ...

随机推荐

  1. 【BZOJ-3174】拯救小矮人 贪心 + DP

    3174: [Tjoi2013]拯救小矮人 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 686  Solved: 357[Submit][Status ...

  2. 【BZOJ-4569】萌萌哒 ST表 + 并查集

    4569: [Scoi2016]萌萌哒 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 459  Solved: 209[Submit][Status] ...

  3. BZOJ1012 [JSOI2008]最大数maxnumber

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  4. JSP教程推荐

    从搭建环境到上手JSP再到Servlet. http://www.runoob.com/jsp/jsp-tutorial.html

  5. Oracle技术嘉年华

    只有把一件事情做好,才会获得更多的机会! 短期的努力,成效并不明显,但是自己的成长一定能够感受到! 嘉年华的收获: 遗憾: 总结: 展望:

  6. 数据结构算法C语言实现(二)---2.3线性表的链式表示和实现之单链表

    一.简述 [暂无] 二.头文件 #ifndef _2_3_part1_H_ #define _2_3_part1_H_ //2_3_part1.h /** author:zhaoyu email:zh ...

  7. python时间模块-time和datetime

    时间模块 python 中时间表示方法有:时间戳,即从1975年1月1日00:00:00到现在的秒数:格式化后的时间字符串:时间struct_time 元组. struct_time元组中元素主要包括 ...

  8. 加州大学伯克利分校Stat2.2x Probability 概率初步学习笔记: Final

    Stat2.2x Probability(概率)课程由加州大学伯克利分校(University of California, Berkeley)于2014年在edX平台讲授. PDF笔记下载(Acad ...

  9. FZU 2193 So Hard (有限小数转换最简分数)(想法题)

    题目链接: 传送门 So Hard Time Limit: 1000MS     Memory Limit: 65536K 题目描述 请将有限小数化为最简分数. 输入 一个整数n 表示需要转化的小数个 ...

  10. python合并2个字典

    2种方式,update()和items()方式 In [14]: a Out[14]: {'a': 1, 'b': 2, 'c': 3} In [15]: c = {'d': 4} In [16]: ...