結果

問題 No.1955 Not Prime
ユーザー 👑 platinumplatinum
提出日時 2022-04-13 10:17:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 183 ms / 2,000 ms
コード長 1,533 bytes
コンパイル時間 2,591 ms
コンパイル使用メモリ 222,292 KB
実行使用メモリ 35,016 KB
最終ジャッジ日時 2023-08-24 20:12:53
合計ジャッジ時間 6,526 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
11,756 KB
testcase_01 AC 15 ms
11,560 KB
testcase_02 AC 15 ms
11,560 KB
testcase_03 AC 15 ms
11,728 KB
testcase_04 AC 15 ms
11,736 KB
testcase_05 AC 15 ms
11,896 KB
testcase_06 AC 23 ms
11,568 KB
testcase_07 AC 117 ms
14,964 KB
testcase_08 AC 101 ms
14,584 KB
testcase_09 AC 180 ms
14,380 KB
testcase_10 AC 171 ms
11,780 KB
testcase_11 AC 150 ms
35,016 KB
testcase_12 AC 59 ms
12,272 KB
testcase_13 AC 181 ms
14,980 KB
testcase_14 AC 42 ms
12,844 KB
testcase_15 AC 40 ms
12,316 KB
testcase_16 AC 80 ms
14,588 KB
testcase_17 AC 149 ms
18,220 KB
testcase_18 AC 181 ms
14,908 KB
testcase_19 AC 183 ms
17,696 KB
testcase_20 AC 16 ms
11,564 KB
testcase_21 AC 64 ms
12,512 KB
testcase_22 AC 16 ms
11,612 KB
testcase_23 AC 16 ms
11,692 KB
testcase_24 AC 15 ms
11,668 KB
testcase_25 AC 15 ms
11,808 KB
権限があれば一括ダウンロードができます

ソースコード

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;
	assert(1 <= N && N <= 500);
	vv A(N, vector<int>(2));
	rep(i,N) cin >> A[i][0] >> A[i][1];
	rep(i,N){
		rep(j,2){
			assert(1 <= A[i][j] && A[i][j] < 1000);
		}
	}
	pair<vector<int>,vector<int>> E = Eratosthenes(1000005);
	vector<int> primes(1000010);
	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