結果

問題 No.1955 Not Prime
ユーザー 👑 emthrmemthrm
提出日時 2022-05-20 23:06:07
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 211 ms / 2,000 ms
コード長 3,970 bytes
コンパイル時間 2,950 ms
コンパイル使用メモリ 223,976 KB
実行使用メモリ 84,232 KB
最終ジャッジ日時 2023-10-20 13:59:40
合計ジャッジ時間 7,532 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 104 ms
84,232 KB
testcase_01 AC 105 ms
84,232 KB
testcase_02 AC 101 ms
84,232 KB
testcase_03 AC 103 ms
84,232 KB
testcase_04 AC 104 ms
84,232 KB
testcase_05 AC 102 ms
84,232 KB
testcase_06 AC 106 ms
84,232 KB
testcase_07 AC 153 ms
84,232 KB
testcase_08 AC 146 ms
84,232 KB
testcase_09 AC 178 ms
84,232 KB
testcase_10 AC 174 ms
84,232 KB
testcase_11 AC 211 ms
84,232 KB
testcase_12 AC 127 ms
84,232 KB
testcase_13 AC 183 ms
84,232 KB
testcase_14 AC 116 ms
84,232 KB
testcase_15 AC 117 ms
84,232 KB
testcase_16 AC 137 ms
84,232 KB
testcase_17 AC 170 ms
84,232 KB
testcase_18 AC 185 ms
84,232 KB
testcase_19 AC 184 ms
84,232 KB
testcase_20 AC 104 ms
84,232 KB
testcase_21 AC 130 ms
84,232 KB
testcase_22 AC 104 ms
84,232 KB
testcase_23 AC 106 ms
84,232 KB
testcase_24 AC 104 ms
84,232 KB
testcase_25 AC 104 ms
84,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

std::vector<int> prime_sieve(const int n, const bool get_only_prime) {
  std::vector<int> smallest_prime_factor(n + 1), prime;
  std::iota(smallest_prime_factor.begin(), smallest_prime_factor.end(), 0);
  for (int i = 2; i <= n; ++i) {
    if (smallest_prime_factor[i] == i) prime.emplace_back(i);
    for (const int p : prime) {
      if (i * p > n || p > smallest_prime_factor[i]) break;
      smallest_prime_factor[i * p] = p;
    }
  }
  return get_only_prime ? prime : smallest_prime_factor;
}

struct TwoSat {
  explicit TwoSat(const int n)
      : n(n), graph(n << 1), rgraph(n << 1), is_visited(n << 1), ids(n << 1) {
    order.reserve(n << 1);
  }

  int negate(const int x) const { return (n + x) % (n << 1); }

  void add_or(const int x, const int y) {
    graph[negate(x)].emplace_back(y);
    graph[negate(y)].emplace_back(x);
    rgraph[y].emplace_back(negate(x));
    rgraph[x].emplace_back(negate(y));
  }

  void add_if(const int x, const int y) { add_or(negate(x), y); }

  void add_nand(const int x, const int y) { add_or(negate(x), negate(y)); }

  void set_true(const int x) { add_or(x, x); }

  void set_false(const int x) { set_true(negate(x)); }

  std::vector<bool> build() {
    std::fill(is_visited.begin(), is_visited.end(), false);
    std::fill(ids.begin(), ids.end(), -1);
    order.clear();
    for (int i = 0; i < (n << 1); ++i) {
      if (!is_visited[i]) dfs(i);
    }
    for (int i = (n << 1) - 1, id = 0; i >= 0; --i) {
      if (ids[order[i]] == -1) rdfs(order[i], id++);
    }
    std::vector<bool> res(n);
    for (int i = 0; i < n; ++i) {
      if (ids[i] == ids[negate(i)]) return {};
      res[i] = ids[negate(i)] < ids[i];
    }
    return res;
  }

 private:
  const int n;
  std::vector<std::vector<int>> graph, rgraph;
  std::vector<bool> is_visited;
  std::vector<int> ids, order;

  void dfs(const int ver) {
    is_visited[ver] = true;
    for (const int dst : graph[ver]) {
      if (!is_visited[dst]) dfs(dst);
    }
    order.emplace_back(ver);
  }

  void rdfs(const int ver, const int cur_id) {
    ids[ver] = cur_id;
    for (const int dst : rgraph[ver]) {
      if (ids[dst] == -1) rdfs(dst, cur_id);
    }
  }
};

int main() {
  const vector<int> is_prime = prime_sieve(10001000, false);
  int n; cin >> n;
  vector<int> a(n), b(n); REP(i, n) cin >> a[i] >> b[i];
  TwoSat two_sat(n);
  REP(i, n) REP(j, n) {
    if (i == j) continue;
    const int v = stoi(to_string(a[i]) + to_string(a[j]));
    if (is_prime[v] == v) two_sat.add_if(i, j);
  }
  REP(i, n) REP(j, n) {
    const int v = stoi(to_string(a[i]) + to_string(b[j]));
    if (is_prime[v] == v) two_sat.add_if(i, two_sat.negate(j));
  }
  REP(i, n) REP(j, n) {
    const int v = stoi(to_string(b[i]) + to_string(a[j]));
    if (is_prime[v] == v) two_sat.add_if(two_sat.negate(i), j);
  }
  REP(i, n) REP(j, n) {
    if (i == j) continue;
    const int v = stoi(to_string(b[i]) + to_string(b[j]));
    if (is_prime[v] == v) two_sat.add_if(two_sat.negate(i), two_sat.negate(j));
  }
  cout << (two_sat.build().empty() ? "No\n" : "Yes\n");
  return 0;
}
0