結果
| 問題 |
No.1955 Not Prime
|
| コンテスト | |
| ユーザー |
emthrm
|
| 提出日時 | 2022-05-20 23:06:07 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 222 ms / 2,000 ms |
| コード長 | 3,970 bytes |
| コンパイル時間 | 2,678 ms |
| コンパイル使用メモリ | 216,364 KB |
| 最終ジャッジ日時 | 2025-01-29 11:30:34 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
#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;
}
emthrm