結果

問題 No.2244 Integer Complete
ユーザー 👑 NachiaNachia
提出日時 2023-03-09 03:52:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 4,682 bytes
コンパイル時間 1,056 ms
コンパイル使用メモリ 91,944 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-18 06:08:16
合計ジャッジ時間 3,524 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 3 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 7 ms
4,348 KB
testcase_18 AC 8 ms
4,348 KB
testcase_19 AC 9 ms
4,348 KB
testcase_20 AC 8 ms
4,348 KB
testcase_21 AC 8 ms
4,348 KB
testcase_22 AC 7 ms
4,348 KB
testcase_23 AC 6 ms
4,348 KB
testcase_24 AC 6 ms
4,348 KB
testcase_25 AC 6 ms
4,348 KB
testcase_26 AC 7 ms
4,348 KB
testcase_27 AC 9 ms
4,348 KB
testcase_28 AC 8 ms
4,348 KB
testcase_29 AC 9 ms
4,348 KB
testcase_30 AC 9 ms
4,348 KB
testcase_31 AC 6 ms
4,348 KB
testcase_32 AC 6 ms
4,348 KB
testcase_33 AC 3 ms
4,348 KB
testcase_34 AC 6 ms
4,348 KB
testcase_35 AC 7 ms
4,348 KB
testcase_36 AC 7 ms
4,348 KB
testcase_37 AC 7 ms
4,348 KB
testcase_38 AC 7 ms
4,348 KB
testcase_39 AC 6 ms
4,348 KB
testcase_40 AC 6 ms
4,348 KB
testcase_41 AC 6 ms
4,348 KB
testcase_42 AC 6 ms
4,348 KB
testcase_43 AC 6 ms
4,348 KB
testcase_44 AC 6 ms
4,348 KB
testcase_45 AC 7 ms
4,348 KB
testcase_46 AC 6 ms
4,348 KB
testcase_47 AC 6 ms
4,348 KB
testcase_48 AC 6 ms
4,348 KB
testcase_49 AC 5 ms
4,348 KB
testcase_50 AC 5 ms
4,348 KB
testcase_51 AC 8 ms
4,348 KB
testcase_52 AC 7 ms
4,348 KB
testcase_53 AC 6 ms
4,348 KB
testcase_54 AC 6 ms
4,348 KB
testcase_55 AC 4 ms
4,348 KB
testcase_56 AC 4 ms
4,348 KB
testcase_57 AC 2 ms
4,348 KB
testcase_58 AC 6 ms
4,348 KB
testcase_59 AC 7 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "Main.cpp"

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <atcoder/modint>
#line 2 "nachia\\math\\prime-sieve-explicit.hpp"

#line 5 "nachia\\math\\prime-sieve-explicit.hpp"
#include <cassert>
#line 7 "nachia\\math\\prime-sieve-explicit.hpp"


namespace nachia{

namespace prime_sieve_explicit_internal{
    std::vector<bool> isprime = { false }; // a[x] := isprime(2x+1)

    void CalcIsPrime(int z){
        if((int)isprime.size() *2+1 < z+1){
            int new_z = isprime.size();
            while(new_z*2+1 < z+1) new_z *= 2;
            z = new_z-1;
            isprime.resize(z+1, true);
            for(int i=1; i*(i+1)*2<=z; i++) if(isprime[i]){
                for(int j=i*(i+1)*2; j<=z; j+=i*2+1) isprime[j] = false;
            }
        }
    }
    
    std::vector<int> prime_list = {2};
    int prime_list_max = 0;

    void CalcPrimeList(int z){
        while((int)prime_list.size() < z){
            if((int)isprime.size() <= prime_list_max + 1) CalcIsPrime(prime_list_max + 1);
            for(int p=prime_list_max+1; p<(int)isprime.size(); p++){
                if(isprime[p]) prime_list.push_back(p*2+1);
            }
            prime_list_max = isprime.size() - 1;
        }
    }

    void CalcPrimeListUntil(int z){
        if(prime_list_max < z){
            CalcIsPrime(z);
            for(int p=prime_list_max+1; p<(int)isprime.size(); p++){
                if(isprime[p]) prime_list.push_back(p*2+1);
            }
            prime_list_max = isprime.size() - 1;
        }
    }

}


bool IsprimeExplicit(int n){
    using namespace prime_sieve_explicit_internal;
    if(n == 2) return true;
    if(n % 2 == 0) return false;
    CalcIsPrime(n);
    return isprime[(n-1)/2];
}

int NthPrimeExplicit(int n){
    using namespace prime_sieve_explicit_internal;
    CalcPrimeList(n);
    return prime_list[n];
}

int PrimeCountingExplicit(int n){
    using namespace prime_sieve_explicit_internal;
    if(n < 2) return 0;
    CalcPrimeListUntil(n);
    auto res = std::upper_bound(prime_list.begin(), prime_list.end(), n) - prime_list.begin();
    return (int)res;
}

// [l, r)
std::vector<bool> SegmentedSieveExplicit(long long l, long long r){
    assert(0 <= l); assert(l <= r);
    long long d = r - l;
    if(d == 0) return {};
    std::vector<bool> res(d, true);
    for(long long p=2; p*p<=r; p++) if(IsprimeExplicit(p)){
        long long il = (l+p-1)/p, ir = (r+p-1)/p;
        if(il <= p) il = p;
        for(long long i=il; i<ir; i++) res[i*p-l] = false;
    }
    if(l < 2) for(long long p=l; p<2 && p<r; p++) res[l-p] = false;
    return res;
}


} // namespace nachia
#line 8 "Main.cpp"
using namespace std;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
#define rep(i,n) for(int i=0; i<(int)(n); i++)
const i64 INF = 1001001001001001001;

using Modint = atcoder::static_modint<998244353>;

int main(){
    // 正整数 n について、 p in range (n^2, (n+1)^2) なる 素数 p が存在する(という未解決の予想があり、制約の範囲では成立を認めてもよい)
    // 10^9 以下の値で終わる素数砂漠の最大長は 281 (アルゴ式の記述および Wikipedia より)

    int N, M; cin >> N >> M;
    vector<int> A(40000);
    vector<int> B(40000);
    rep(i,N){ int a; cin >> a; A[a] = 1; }
    rep(i,M){ int a; cin >> a; B[a] = 1; }

    if(A[1] == 0 || B[1] == 0){ cout << "1\n"; return 0; }
    
    i64 mex = 1;
    while(A[mex] == 1 || B[mex]) mex++;
    i64 ans = INF;
    i64 ansmin = mex * mex;
    auto tab = nachia::SegmentedSieveExplicit(mex*mex, (mex+1)*(mex+1));
    rep(i,tab.size()) if(tab[i]){ ans = mex*mex+i; break; }

    // ans は素数
    // ans - ansmin <= 281
    // ans <= 9x10^8

    vector<int> Flag(ans - ansmin + 1);

    vector<i64> sq(40000);
    rep(i,40000) sq[i] = (i64)i * i;
    auto checkPlayable = [&](i64 a, i64 b) -> void {
        int a1 = upper_bound(sq.begin(), sq.end(), a) - sq.begin(); a1--;
        int b1 = upper_bound(sq.begin(), sq.end(), b) - sq.begin(); b1--;
        // if playable
        if((A[a1] && B[b1]) || (A[b1] && B[a1])) Flag[a*b-ansmin] = 1;
    };

    for(i64 d=2; d*d<=ans; d++){
        // ansmin <= d * e < ans を探索
        i64 el = (ansmin - 1) / d + 1;
        i64 er = (ans - 1) / d;
        for(i64 e=el; e<=er; e++) checkPlayable(d, e);
    }

    for(int i=0; i<(int)Flag.size(); i++) if(Flag[i] == 0){ ans = ansmin + i; break; }

    cout << ans << '\n';
    return 0;
}



struct ios_do_not_sync{
    ios_do_not_sync(){
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    }
} ios_do_not_sync_instance;

0