嘟嘟嘟

这道题正解是怎么对的其实我也不清楚,总之靠感性理解吧。

首先当然要把1到n / 2的素数都筛出来,因为两两能配对的数一定都是这些素数的倍数。这也就说明对于(n / 2, n]的素数,他们一定不能配对,所以就不用筛他们了。

筛完后我们考虑怎么配对,对于一个素数的所有倍数xi,他们任意两个都可以配对,但是可能配对完后得到的总配对数会减少。因此我们从最大的素数开始两两配对,感性理解就是越大的素数的倍数就越少,因此先尽量满足配对方案少的素数,然后再处理配对方案多的素数。

还有一点,就是当xi的倍数中没有配对的数是奇数个的时候,就会多出来一个,那么我们应该把哪一个丢出来呢?还是按照上面贪心的方法想:丢出来的数成功配对的可能性越大越好,即他的最小质因子越小越好。那么就应该丢掉2 * xi

讲 完 了

 #include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-;
const int maxn = 1e5 + ;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = ans * + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) x = -x, putchar('-');
if(x >= ) write(x / );
putchar(x % + '');
} bool vis[maxn];
struct Node
{
int x, y;
}ans[maxn];
int n, cnt = ; int prime[maxn], v[maxn];
void init(int n)
{
for(int i = ; i <= n; ++i)
{
if(!v[i]) v[i] = i, prime[++prime[]] = i;
for(int j = ; i * prime[j] <= n && j <= prime[]; ++j)
{
if(prime[j] > v[i]) break;
v[i * prime[j]] = prime[j];
}
}
} int main()
{
n = read();
init(n >> );
for(int i = prime[]; i; --i)
{
int flg = ; int las = ;
for(int j = prime[i]; j <= n; j += prime[i]) if(!vis[j]) flg ^= ; //记录奇偶
for(int j = prime[i]; j <= n; j += prime[i])
{
if(j == (prime[i] << ) && flg && prime[i] != ) continue;
if(las && !vis[j])
{
vis[j] = ;
ans[++cnt] = (Node){las, j};
las = ;
}
else if(!las && !vis[j]) las = j, vis[j] = ;
}
}
write(cnt); enter;
for(int i = ; i <= cnt; ++i) write(ans[i].x), space, write(ans[i].y), enter;
return ;
}

CF449C Jzzhu and Apples的更多相关文章

  1. CF449C Jzzhu and Apples (筛素数 数论?

    Codeforces Round #257 (Div. 1) C Codeforces Round #257 (Div. 1) E CF450E C. Jzzhu and Apples time li ...

  2. CF449 C. Jzzhu and Apples

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

  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. Codeforces 450E:Jzzhu and Apples(构造,数学)

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

  8. CF449C:Jzzhu and Apples

    题意简述 给出正整数n,你要把1-n之间的正整数分成尽可能多组,使得每一组两个数的最大公约数大于1;输出能分成最多组的个数,并按任意顺序输出每组的两个数. 很妙的一道题. 首先我们考虑去处理每个质数的 ...

  9. CF449C:Jzzhu and Apples——题解

    https://vjudge.net/problem/CodeForces-449C 题目大意:1-n编号的苹果两两一对,他们的最大公约数不为1,求这些对的最大匹配. ———————————————— ...

随机推荐

  1. Android应用捕获全局异常自定义处理

    [2016-06-30]最新的全局异常处理DRCrashHandler已经集成在DR_support_lib库中 具体请看: https://coding.net/u/wrcold520/p/DR_s ...

  2. js动态实现时分秒

    <div id="time" style="color: #96C2DD;</div>      <script type="text/ ...

  3. oracle 错误实例分析(ORA-01078)

    01,问题描述 心血来潮想看一下启动数据库的alert log.然后把数据库给关闭了,同时也在监听日志文件    下面可谓是详细的描述了整个关机过程,也看到了无数的error [root@node1 ...

  4. C# 判读取得字符编码格式

    FileStream fs1 = new FileStream(folder + strPath, FileMode.Open); byte[] bytes = new byte[fs1.Length ...

  5. 3d旋转卡牌

    做成向中心缩放就行了,和旋转效果一样的

  6. 数据段描述符和代码段描述符(二)——《x86汇编语言:从实模式到保护模式》读书笔记11

    这篇博文,我们编写一个C语言的小程序,来解析数据段或者代码段描述符的各个字段.这样我们阅读原书的代码就会方便一点,只要运行这个小程序,就可以明白程序中定义的数据段或者代码段的描述符了. 这段代码,我用 ...

  7. [Scala] Currying

    Currying是一種函數式編程技巧, 指的是把接受多個參數的函數變換成接受一個單一參數的函數. 以一個簡單的例子在Scala中實現.. def f(a:Int, b:Int)={ a+b } //f ...

  8. HDU 1800——Flying to the Mars——————【字符串哈希】

    Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  9. nyoj1032——Save Princess——————【set应用】

    Save Princess 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Yesterday, the princess was kidnapped by a de ...

  10. HTTP和HTTPS的区别?

    HTTP1.1(Hypertext Transfer Protocol Vertion 1.1)超文本传输协议-版本1.1它是用来在Internet上传送超文本的传送协议.它是运行在Tcp/Ip协议族 ...