結果

問題 No.1955 Not Prime
ユーザー platinum
提出日時 2022-04-12 21:23:24
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 925 ms / 2,000 ms
コード長 1,423 bytes
コンパイル時間 2,778 ms
コンパイル使用メモリ 223,748 KB
最終ジャッジ日時 2025-01-28 17:31:03
ジャッジサーバーID
(参考情報)
judge4 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/twosat>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)

using namespace std;
using namespace atcoder;
using LL = long long;
using P = pair<int,int>;
using vv = vector<vector<int>>;
const int INF = (int)1e9;
const LL LINF = (LL)1e18;

pair<vector<int>, vector<int>> Eratosthenes(const int N){
   vector<int> divisor(N + 1);
   vector<int> res;
   for(int i = 2; i <= N; i++){
	   if(divisor[i] == 0){
		   divisor[i] = 1;
		   res.emplace_back(i);
		   for(int j = 2 * i; j <= N; j += i) divisor[j] = i;
	   }
   }
   return pair<vector<int>, vector<int>>(res, divisor);
}

int main(){
	int N;
	cin >> N;
	vv A(N, vector<int>(2));
	rep(i,N) cin >> A[i][0] >> A[i][1];
	pair<vector<int>,vector<int>> E = Eratosthenes(1000005);
	map<int,int> primes;
	for(auto p : E.first) primes[p]++;
	two_sat ts(N);
	rep(i,N){
		rep(j,N){
			if(i == j){
				rep(u,2){
					string s = to_string(A[i][u]) + to_string(A[j][u^1]);
					int a = stoi(s);
					if(primes[a]) ts.add_clause(i, u, j, u);
				}
			}
			else{
				rep(u,2){
					rep(v,2){
						string s1 = to_string(A[i][u]) + to_string(A[j][v^1]);
						string s2 = to_string(A[j][v]) + to_string(A[i][u^1]);
						int a = stoi(s1), b = stoi(s2);
						if(primes[a] or primes[b]) ts.add_clause(i, u, j, v);
					}
				}
			}
		}
	}
	bool ans = ts.satisfiable();
	if(ans) cout << "Yes" << endl;
	else cout << "No" << endl;

	return 0;
}
0