CF449C Jzzhu and Apples

传送门

这道题好像一开始想到了差不多的做法orz?后来都不大敢相信就是这么做的……有点瞎搞。

后来看了CF的官方题解,感觉还是挺有道理的。首先对于1和大于n/2的质数肯定是不行的,我们直接忽略。然后,对于每一个质数的倍数,我们肯定是把他们组合在一起更优。如果这些数有奇数个,那我们就把质数的2倍挑出来(因为2倍是最容易组合的!)

如果有偶数个就直接组合。

最后你肯定剩下的全都是偶数,那么随便组合就行啦!

直接用两个栈模拟即可。

看一下代码。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<set>
#include<queue>
#define pr pair<int,int>
#define mp make_pair
#define fi first
#define sc second
#define rep(i,a,n) for(int i = a;i <= n;i++)
#define per(i,n,a) for(int i = n;i >= a;i--)
#define enter putchar(‘\n‘)

using namespace std;
typedef long long ll;
const int M = 200005;
const int INF = 1000000009;

int read()
{
    int ans = 0,op = 1;
    char ch = getchar();
    while(ch < ‘0‘ || ch > ‘9‘)
    {
    if(ch == ‘-‘) op = -1;
    ch = getchar();
    }
    while(ch >= ‘0‘ && ch <= ‘9‘)
    {
    ans *= 10;
    ans += ch - ‘0‘;
    ch = getchar();
    }
    return ans * op;
}

int n,p[M],stack[M],tot,cur,sta[M],cnt,now;
bool np[M],vis[M];
pr ans[M];

void euler()
{
    np[1] = 1;
    rep(i,2,n)
    {
    if(!np[i]) p[++tot] = i;
    for(int j = 1;i * p[j] <= n;j++)
    {
        np[i*p[j]] = 1;
        if(!(i%p[j])) break;
    }
    }
}

int main()
{
    n = read();
    euler();
    rep(i,2,tot)
    {
    if(p[i] > n>>1) break;
    rep(j,1,n/p[i]) if(j != 2 && !vis[p[i] * j]) sta[++cur] = p[i] * j,vis[p[i] * j] = 1;
    if(cur&1)
    {
        sta[++cur] = p[i] << 1,vis[p[i] << 1] = 1;
        while(cur) ans[++cnt].fi = sta[cur--],ans[cnt].sc = sta[cur--];
    }
    else
    {
        stack[++now] = p[i] << 1,vis[p[i] << 1] = 1;
        while(cur) ans[++cnt].fi = sta[cur--],ans[cnt].sc = sta[cur--];
    }
    }
    rep(j,1,n>>1) if(!vis[j<<1]) stack[++now] = j << 1;
    if(now&1) now--;
    while(now) ans[++cnt].fi = stack[now--],ans[cnt].sc = stack[now--];
    printf("%d\n",cnt);
    rep(i,1,cnt) printf("%d %d\n",ans[i].fi,ans[i].sc);
    return 0;
}

原文地址:https://www.cnblogs.com/captain1/p/9781303.html

时间: 2024-10-04 23:06:48

CF449C Jzzhu and Apples的相关文章

CF449C Jzzhu and Apples (筛素数 数论?

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 tre

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 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

CodeForces 449C Jzzhu and Apples 数学+素数

这道题目晚上本来就花了很多把都××了,着实觉得自己思路没错啊,回顾一下思路,给你n个数,分成两两组合一对,分成最多组如何分,但是组合的两个数 不能互素,所以呢 偶数肯定是好的了,所以先放着,先把素数给搞定,10^5所以枚举所有包含该素数因子的数,如果刚好分组则最好,不然的话其中有偶数的踢掉一个给下面的偶数处理部分,最后再处理偶数的部分,这样肯定满足组数最多,完全没有问题,后来方法确实是没问题啊,只是代码有问题,我靠!真是脑残!,今天看到一位大牛的想法,我跟他是一样的,只是代码写搓了,后来改了又改

Codeforces 449C Jzzhu and Apples(构造)

题目链接:Codeforces 449C Jzzhu and Apples 题目大意:Jzzhu从苹果树上获得n个苹果,标号从1~n,现在要将他们以两个为一组卖给商家,要求一组中的两个苹果的编号最大公约数大于1,分的组数尽量多. 解题思路:枚举公约数d,只枚举素数,因为合数的可以在更小的素数被枚举.将所有没用过并且编号为d的倍数的苹果拿出来,两两组队,如果个数为奇数,那么就将2d留出来.因为d>2,所以第2个肯定是2d.并且2d是2的倍数. #include <cstdio> #incl

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

CodeForces 450

A - Jzzhu and Children Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description There are n children in Jzzhu's school. Jzzhu is going to give some candies to them. Let's number all the children from 1 to n. The i-th

Pick apples 第三届acm省赛

Description Once ago, there is a mystery yard which only produces three kinds of apples. The number of each kind is infinite. A girl carrying a big bag comes into the yard. She is so surprised because she has never seen so many apples before. Each ki

HDU 5303 Delicious Apples(贪心 + 背包 2015多校啊)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 Problem Description There are n apple trees planted along a cyclic road, which is L metres long. Your storehouse is built at position 0 on that cyclic road. The ith tree is planted at position xi,

【矩阵快速幂 】Codeforces 450B - Jzzhu and Sequences (公式转化)

[题目链接]click here~~ [题目大意] Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, please calculate fn modulo1000000007(109?+?7). [解题思路] /*A - Jzzhu and Sequences Codeforces 450B - Jzzhu and Sequences ( 矩阵快速幂 )