-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prime_Sum.cpp
104 lines (79 loc) · 1.84 KB
/
Prime_Sum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* Title : Prime Sum
* Author : Tridib Samanta
* Link : https://www.hackerrank.com/challenges/prime-sum/problem
**/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll mod_mul(ll a, ll b, ll m) {
ll res = 0;
ll y = a % m ;
while (b > 0) {
if (b & 1)
res = (res + y) % m ;
y = (y + y) % m;
b >>= 1;
}
return res % m ;
}
ll mod_expo(ll a, ll p, ll m) {
ll res = 1;
ll y = a % m ;
while ( p > 0 ) {
if (p & 1)
res = mod_mul(res, y, m) ;
y = mod_mul(y, y, m);
p >>= 1;
}
return res % m ;
}
bool miller_rabin(ll p, ll iter)
{
if (p < 2)
return false ;
if (p % 2 == 0 && p != 2)
return false ;
ll s = p - 1 ;
while (s % 2 == 0) {
s >>= 1;
}
for (ll i = 1; i <= iter; ++i) {
ll a = rand() % (p - 1) + 1;
ll temp = s ;
ll m1 = mod_expo(a, temp, p);
while (m1 != 1 && m1 != p - 1 && temp != p - 1) {
m1 = mod_mul(m1, m1, p);
temp *= 2;
}
if (temp % 2 == 0 && m1 != p - 1)
return false ;
}
return true ;
}
int main() {
int tc;
cin >> tc;
while (tc--) {
ll n, k;
cin >> n >> k;
if (n < (2 * k)) {
cout << "No" << '\n';
continue;
}
if (k == 1) {
cout << (miller_rabin(n, 5) ? "Yes" : "No") << '\n';
}
else if (k == 2) {
if (n % 2 == 0)
cout << "Yes" << '\n';
else {
cout << (miller_rabin(n - 2, 5) ? "Yes" : "No") << '\n';
}
}
else {
cout << "Yes" << '\n';
}
}
return 0;
}