백준 알고리즘 7789번 : 텔레프라임

Zoo Da·2021년 11월 28일
0

백준 알고리즘

목록 보기
270/337
post-thumbnail

링크

https://www.acmicpc.net/problem/7789

sol1) 밀러라빈 소수판별법

#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define int int64_t
using namespace std;

typedef long long ll;
typedef __int128 i128;

namespace miller_rabin {
	ll mul(ll x, ll y, ll mod) { return (i128)x * y % mod; }
	ll _pow(ll x, ll y, ll p) {
		ll ret = 1, piv = x % p;
		while (y) {
			if (y & 1) ret = mul(ret, piv, p);
			piv = mul(piv, piv, p);
			y >>= 1;
		}
		return ret;
	}
	bool miller_rabin(ll x, ll a) {
		if (x % a == 0) return 0;
		ll d = x - 1;
		while (1) {
			ll tmp = _pow(a, d, x);
			if (d & 1) return (tmp != 1 && tmp != x - 1);
			else if (tmp == x - 1) return 0;
			d >>= 1;
		}
	}
	bool isprime(ll x) {
		for (auto& i : { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37 }) {
			if (x == i) return 1;
			if (x > 40 && miller_rabin(x, i)) return 0;
		}
		if (x <= 40) return 0;
		return 1;
	}
}

int32_t main() {
  fastio;
  int a, b, c; cin >> a >> b;
	c = stoi(to_string(b) + to_string(a));
	cout << (miller_rabin::isprime(a) && miller_rabin::isprime(c) ? "Yes" : "No") << '\n';
}

... 그냥 파이썬를 쓰자

profile
메모장 겸 블로그

0개의 댓글