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>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;
typedef pair<int, int> pii;
const int maxn = 1e5;

bool iscomp[maxn+5], vis[maxn+5];

void prime_table(int n) {
    for (int i = 2; i * i <= n; i++) {
        if (iscomp[i])
            continue;
        for (int j = i * i; j <= n; j += i)
            iscomp[j] = 1;
    }
}

int main () {
    int n;
    scanf("%d", &n);
    prime_table(n);

    vector<int> g;
    vector<pii> ans;

    for (int i = n / 2; i > 1; i--) {
        if (iscomp[i])
            continue;
        g.clear();

        for (int j = i; j <= n; j += i) {
            if (vis[j] == 0)
                g.push_back(j);
        }
        if (g.size() & 1)
            swap(g[1], g[g.size()-1]);
        for (int i = 0; i < g.size() - 1; i += 2) {
            ans.push_back(make_pair(g[i], g[i+1]));
            vis[g[i]] = vis[g[i+1]] = 1;
        }
    }

    printf("%lu\n", ans.size());
    for (int i = 0; i < ans.size(); i++)
        printf("%d %d\n", ans[i].first, ans[i].second);
    return 0;
}
时间: 2024-08-02 07:24:29

Codeforces 449C Jzzhu and Apples(构造)的相关文章

CodeForces 449C Jzzhu and Apples 数学+素数

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

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 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 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 ( 矩阵快速幂 )

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 23C Oranges and Apples 抽屉原理

题目链接:点击打开链接 #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <iostream> #include <map> #include <set> #include <math.h> using namespace std; #define inf 10000000 #define l

Codeforces 429B Working out bfs构造

题目链接:点击打开链接 题意:给定n*m的矩阵 有一个人a从左上角走到右下角,只能↓或→走 另一个人b从左下角走到右上角,只能↑或→走 使得2个人的路径有且仅有一个格子是相交的. 统计2个人的权值和(相交格子的权值和不计) 问最大的权值和是多少. 思路: 首先转换一下题意,也就是找一个格子与4个角落连不相交的线. 我们观察相交的那个格子,那个格子的上下左右必然对应着一个角落. (i,j)点,那么(i-1,j)必然对应左上角或右上角的其中一个角落. 这样(i,j)点的4个相邻格子各自对应一个角落(

Codeforces 346C Number Transformation II 构造

题目链接:点击打开链接 = = 990+ms卡过 #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> #include<vector> #include<set> using namespace std; #define N 100010 #define L(x) (x<<1) #define R(x) (x<<

Codeforces 449B Jzzhu and Cities(最短路)

题目链接:Codeforces 449B Jzzhu and Cities 题目大意:Jzzhu是一个国家的总统,这个国家有N座城市,以1为首都,已经存在了M条公路,给定M条路.并且还有K条铁轨,铁轨均有首都出发,连接si,距离为yi.现在Jzzhu想要节省经费,拆除一些铁轨,问说最多能拆除多少个铁轨,要求每座城市与首都的最短距离不变. 解题思路:最短路,多加一个标记数组,每个si标记1,如果这些点的最短距离被更新,则将对应si的标记清为0,最后统计一下剩余的标记,即为不能拆除的铁轨. #inc