zoukankan      html  css  js  c++  java
  • [CF959D]Mahmoud and Ehab and another array construction task题解

    解法
    非常暴力的模拟。
    一开始吧(1 -> 2 imes 10^6)全部扔进一个set里,如果之前取得数都是与原数组相同的,那么lower_bound一下找到set中大于等于它的数,否则直接取set中最小的数,然后枚举该数的所有质因数及其倍数,类似埃拉托斯特尼筛法删掉它们(如果它们在集合中的话)。
    P.S. 不要用STL algorithm的lower_bound,极慢,用STL内部自带的lower_bound,快很多!

    代码

    #include <cstdio> 
    #include <set>
    #include <algorithm>
    #define ll long long
    
    using namespace std;
    
    inline int read(){
        int x = 0; int zf = 1; char ch = ' ';
        while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
        if (ch == '-') zf = -1, ch = getchar();
        while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;
    }
    
    int a[100005];
    set<int> st;
    
    int main(){
        int n = read();
        for (int i = 1; i <= n; ++i)
            a[i] = read();
        for (int i = 2; i <= 2e6; ++i)
            st.insert(i);
        bool flg = 0; int val; set<int>::iterator it;
        for (int i = 1; i <= n; ++i){
            (!flg) ? it = st.lower_bound(a[i]) : it = st.begin();
            val = *it;
            if (val > a[i]) flg = 1;
            if (i != 1) printf(" ");
            printf("%d", val);
            st.erase(val);
            for (int j = 2; j * j <= val; ++j){
                if (!(val % j)){
                    for (int k = j; k <= 2e6; k += j)
                        st.erase(k);
                }
                while (!(val % j))
                    val /= j;
            }
            if (val > 1)
                for (int k = val; k <= 2e6; k += val)
                    st.erase(k);
        }
        return 0;
    }
    
  • 相关阅读:
    k8s中文网
    python range用法
    python 日志滚动 分文件
    python 语法
    flask 中文编码解码
    python的杨辉三角
    mysql8.0.4以后修改密码方式变更
    flask学习视频
    oralce的lag和lead函数
    JNI 各类数据类型处理
  • 原文地址:https://www.cnblogs.com/linzhengmin/p/10895945.html
Copyright © 2011-2022 走看看