結果

問題 No.1955 Not Prime
ユーザー 👑 NachiaNachia
提出日時 2022-05-20 23:16:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 89 ms / 2,000 ms
コード長 4,413 bytes
コンパイル時間 1,578 ms
コンパイル使用メモリ 104,152 KB
実行使用メモリ 21,864 KB
最終ジャッジ日時 2023-10-20 14:09:52
合計ジャッジ時間 3,304 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,372 KB
testcase_01 AC 3 ms
4,372 KB
testcase_02 AC 2 ms
4,372 KB
testcase_03 AC 2 ms
4,372 KB
testcase_04 AC 2 ms
4,372 KB
testcase_05 AC 2 ms
4,372 KB
testcase_06 AC 6 ms
4,372 KB
testcase_07 AC 56 ms
5,376 KB
testcase_08 AC 46 ms
4,848 KB
testcase_09 AC 82 ms
4,588 KB
testcase_10 AC 85 ms
4,372 KB
testcase_11 AC 59 ms
21,864 KB
testcase_12 AC 26 ms
4,372 KB
testcase_13 AC 89 ms
5,384 KB
testcase_14 AC 17 ms
4,372 KB
testcase_15 AC 17 ms
4,372 KB
testcase_16 AC 36 ms
4,856 KB
testcase_17 AC 71 ms
8,196 KB
testcase_18 AC 87 ms
5,384 KB
testcase_19 AC 83 ms
6,108 KB
testcase_20 AC 3 ms
4,372 KB
testcase_21 AC 29 ms
4,372 KB
testcase_22 AC 3 ms
4,372 KB
testcase_23 AC 2 ms
4,372 KB
testcase_24 AC 3 ms
4,372 KB
testcase_25 AC 3 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <array>
#include <atcoder/modint>
#include <atcoder/scc>

#include <cassert>


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


}
using namespace std;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
#define rep(i,n) for(int i=0; i<(int)(n); i++)

const i64 INF = 1001001001001001001;

using modint = atcoder::static_modint<998244353>;


int main(){
    int N; cin >> N;
    vector<string> A(N);
    vector<string> B(N);
    rep(i,N) cin >> A[i] >> B[i];

    atcoder::scc_graph scc(N*2);
    rep(i,N) rep(j,N){
        bool prohibit_pp = false;
        prohibit_pp = prohibit_pp || (B[j].back() != 0 && nachia::IsprimeExplicit(stoi(A[i]+B[j])));
        prohibit_pp = prohibit_pp || (B[i].back() != 0 && nachia::IsprimeExplicit(stoi(A[j]+B[i])));
        if(prohibit_pp){
            scc.add_edge(i, N+j);
            scc.add_edge(j, N+i);
        }
    }
    rep(i,N) rep(j,N){
        bool prohibit_nn = false;
        prohibit_nn = prohibit_nn || (A[j].back() != 0 && nachia::IsprimeExplicit(stoi(B[i]+A[j])));
        prohibit_nn = prohibit_nn || (A[i].back() != 0 && nachia::IsprimeExplicit(stoi(B[j]+A[i])));
        if(prohibit_nn){
            scc.add_edge(N+i, j);
            scc.add_edge(N+j, i);
        }
    }
    rep(i,N) rep(j,N) if(i != j){
        bool prohibit_pn = false;
        prohibit_pn = prohibit_pn || (A[j].back() != 0 && nachia::IsprimeExplicit(stoi(A[i]+A[j])));
        prohibit_pn = prohibit_pn || (B[i].back() != 0 && nachia::IsprimeExplicit(stoi(B[j]+B[i])));
        if(prohibit_pn){
            scc.add_edge(i, j);
            scc.add_edge(N+j, N+i);
        }
    }

    auto g = scc.scc();

    vector<int> G(2*N);
    rep(i,g.size()) for(int v : g[i]) G[v] = i;
    
    bool ans = true;
    rep(i,N) if(G[i] == G[N+i]) ans = false;
    cout << (ans ? "Yes\n" : "No\n");
    return 0;
}


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


0