結果

問題 No.1955 Not Prime
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2022-05-21 02:55:57
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,040 bytes
コンパイル時間 2,484 ms
コンパイル使用メモリ 194,176 KB
実行使用メモリ 20,136 KB
最終ジャッジ日時 2023-10-20 15:30:57
合計ジャッジ時間 4,510 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
4,348 KB
testcase_01 AC 11 ms
4,348 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 10 ms
4,348 KB
testcase_05 WA -
testcase_06 AC 15 ms
4,348 KB
testcase_07 AC 65 ms
5,088 KB
testcase_08 AC 56 ms
4,824 KB
testcase_09 AC 98 ms
5,140 KB
testcase_10 AC 93 ms
4,348 KB
testcase_11 AC 116 ms
20,136 KB
testcase_12 WA -
testcase_13 AC 98 ms
5,124 KB
testcase_14 AC 25 ms
4,348 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 99 ms
6,220 KB
testcase_20 AC 11 ms
4,348 KB
testcase_21 AC 37 ms
4,348 KB
testcase_22 WA -
testcase_23 AC 11 ms
4,348 KB
testcase_24 AC 11 ms
4,348 KB
testcase_25 AC 11 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2022.05.21 02:55:54
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;


struct StronglyConnectedComponents {
private:
    vector<vector<int>> g, rg;
    vector<int> order; 
    vector<bool> visited;
    int V;

    void dfs(int v) {
        if (visited[v]) return;
        visited[v] = true;
        for (int u : g[v]) dfs(u);
        order.push_back(v);
    }

    void rdfs(int v, int c) {
        if (comp[v] != -1) return;
        comp[v] = c;
        for (int u : rg[v]) rdfs(u, c);
    }

public:
    vector<vector<int>> Graph; 
    vector<int> comp; 

    StronglyConnectedComponents(int v) : V(v) {
        g.resize(v);
        rg.resize(v);
        visited.resize(v);
        comp.resize(v);
    }
    
    void add_vertex(int n = 1) {
        V += n;
        g.resize(V);
        rg.resize(V);
        visited.resize(V);
        comp.resize(V);
    }

    void add_edge(int from, int to) {
        g[from].push_back(to);
        rg[to].push_back(from);
    }

    void build() {
        Graph = vector<vector<int>>();
        order = vector<int>();
        visited.assign(V, false);
        comp.assign(V, -1);
        for (int i = 0; i < V; i++) dfs(i);
        reverse(order.begin(), order.end());
        int number = 0;
        for (int i = 0; i < V; i++) {
            if (comp[order[i]] == -1) {
                rdfs(order[i], number++);
            }
        }
        Graph.resize(number);
        for (int i = 0; i < V; i++) {
            for (int v : g[i]) {
                if (comp[i] == comp[v]) continue;
                Graph[comp[i]].push_back(comp[v]);
            }
        }
    }
};

struct TwoSAT {
private:
    int _n;
    std::vector<bool> _answer;
    StronglyConnectedComponents scc;

    void at_most_one_naive(const vector<int> &x, const vector<bool> &t) {
        assert(x.size() == t.size());
        int _m = x.size();
        for (int i = 0; i < _m-1; i++) {
            for (int j = i+1; j < _m; j++) {
                add_clause(x[i],!t[i],x[j],!t[j]);
            }
        }
    }
public:
    TwoSAT():_n(0),scc(0){}
    explicit TwoSAT(int n) : _n(n), _answer(n), scc(2*n) {}
    void add_clause(int i, bool f, int j, bool g) {
        assert(0 <= i && i < _n);
        assert(0 <= j && j < _n);
        scc.add_edge(2 * i + (f ? 0 : 1), 2 * j + (g ? 1 : 0));
        scc.add_edge(2 * j + (g ? 0 : 1), 2 * i + (f ? 1 : 0));
    }
    void at_most_one(const vector<int> &x, const vector<bool> &t) {
        assert(x.size() == t.size());
        int _m = x.size();
        if (_m <= 6) {
            at_most_one_naive(x,t);
            return;
        }
        int _nn = _n;
        _n += _m;
        scc.add_vertex(_m*2);
        _answer.resize(_n);
        for (int i = 0; i < _m; i++) {
            add_clause(_nn+i,false,x[i],!t[i]);
            if (i) {
                add_clause(_nn+i,false,_nn+i-1,true);
                add_clause(x[i],!t[i],_nn+i-1,true);
            }
        }
    }
    bool satisfiable() {
        scc.build();
        auto id = scc.comp;
        for (int i = 0; i < _n; i++) {
            if (id[2 * i] == id[2 * i + 1]) return false;
            _answer[i] = id[2 * i] < id[2 * i + 1];
        }
        return true;
    }
    std::vector<bool> answer() { return _answer; }
};

struct Eratosthenes {
private:
    int N;

public:
    std::vector< bool > isprime;
    std::vector< int > prime;

    Eratosthenes(int n) : N(n) {
        isprime.resize(N + 1, true);
        isprime[0] = isprime[1] = false;
        for (int i = 2; i * i <= N; i++) {
            if (isprime[i]) {
                for (int j = i * i; j <= N; j += i) {
                    isprime[j] = false;
                }
            }
        }
        for (int i = 2; i <= N; i++) {
            if (isprime[i]) {
                prime.push_back(i);
            }
        }
    }
};

int main() {
    int n;cin >> n;
    vector<int> a(n),b(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    Eratosthenes era(1000000);
    TwoSAT two_sat(n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i == j) continue;
            string c;
            c = to_string(a[i])+to_string(a[j]);
            if (c.size() < 7 && era.isprime[stoi(c)]) {
                two_sat.add_clause(i,true,j,true);
            }
            c = to_string(a[i])+to_string(b[j]);
            if (c.size() < 7 && era.isprime[stoi(c)]) {
                two_sat.add_clause(i,true,j,false);
            }
            c = to_string(b[i])+to_string(a[j]);
            if (c.size() < 7 && era.isprime[stoi(c)]) {
                two_sat.add_clause(i,false,j,true);
            }
            c = to_string(b[i])+to_string(b[j]);
            if (c.size() < 7 && era.isprime[stoi(c)]) {
                two_sat.add_clause(i,false,j,false);
            }
        }
    }
    if (two_sat.satisfiable()) {
        puts("Yes");
    }
    else {
        puts("No");
    }
    return 0;
}
0