zoukankan      html  css  js  c++  java
  • Codeforces 449C Jzzhu and Apples 贪心 (看题解)

    Jzzhu and Apples

    从大的质因子开始贪心, 如果有偶数个则直接组合, 如果是奇数个留下那个质数的两倍, 其余两两组合。

    #include<bits/stdc++.h>
    #define LL long long
    #define fi first
    #define se second
    #define mk make_pair
    #define PLL pair<LL, LL>
    #define PLI pair<LL, int>
    #define PII pair<int, int>
    #define SZ(x) ((int)x.size())
    #define ull unsigned long long
    
    using namespace std;
    
    const int N = 1e5 + 7;
    const int inf = 0x3f3f3f3f;
    const LL INF = 0x3f3f3f3f3f3f3f3f;
    const int mod = 1e9 + 7;
    const double eps = 1e-8;
    const double PI = acos(-1);
    
    int n;
    bool vis[N];
    bool prime[N];
    queue<int> que;
    vector<PII> ans;
    
    int main() {
        memset(prime, true, sizeof(prime));
        prime[0] = prime[1] = false;
        for(int i = 2; i < N; i++) {
            if(!prime[i]) continue;
            for(int j = i * 2; j < N; j += i)
                prime[j] = false;
        }
        scanf("%d", &n);
        for(int i = n; i >= 2; i--) {
            if(!prime[i]) continue;
            for(int j = 3 * i; j <= n; j += i)
                if(!vis[j]) que.push(j);
            que.push(i);
            if(2 * i <= n && !vis[2 * i]) que.push(2 * i);
            while(SZ(que) >= 2) {
                int x = que.front(); que.pop();
                int y = que.front(); que.pop();
                vis[x] = vis[y] = true;
                ans.push_back(mk(x, y));
            }
            while(SZ(que)) que.pop();
        }
        printf("%d
    ", SZ(ans));
        for(auto& t : ans) printf("%d %d
    ", t.fi, t.se);
        puts("");
        return 0;
    }
    
    /*
    */
  • 相关阅读:
    linux系统安装Mysql
    makefile通用模板
    makefile常用函数
    mysqlconnector安装
    linux添加默认路由route
    .h文件与.hpp文件的区别
    ubuntu20优化开机启动
    [javascript]js原型链以及原型链继承
    webpack4.*入门笔记
    图像编程:图片大小关系
  • 原文地址:https://www.cnblogs.com/CJLHY/p/10439494.html
Copyright © 2011-2022 走看看