QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#550602 | #7624. Mystery of Prime | mhw# | WA | 1ms | 3620kb | C++23 | 2.3kb | 2024-09-07 13:34:47 | 2024-09-07 13:34:48 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const i64 P = 998244353;
i64 mul(i64 a, i64 b, i64 m) {
return static_cast<__int128>(a) * b % m;
}
i64 power(i64 a, i64 b, i64 m) {
i64 res = 1 % m;
for (; b; b >>= 1, a = mul(a, a, m))
if (b & 1)
res = mul(res, a, m);
return res;
}
bool isprime(i64 n) { // Miller-Rabin
if (n < 2) return false;
static constexpr int B[] = {2, 3, 5, 7, 11, 13, 17, 19, 23};
for (int p : B) {
if (n == p) return true;
if (n % p == 0) return false;
}
i64 m = (n - 1) >> __builtin_ctz(n - 1);
for (int p : B) {
i64 t = m, a = power(p, m, n);
while (t != n - 1 && a != 1 && a != n - 1) {
a = mul(a, a, n);
t *= 2;
}
if (a != n - 1 && t % 2 == 0) return false;
}
return true;
}
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
int dp[n][4] {};
memset(dp, 0x3f, sizeof(dp));
if (a[0] == 1) dp[0][3] = 0;
else dp[0][3] = 1;
dp[0][0] = 0;
dp[0][1] = dp[0][2] = 1;
for (int i = 1; i < n; i++) {
if (isprime(a[i] + a[i - 1])) {
dp[i][0] = min(dp[i][0], dp[i - 1][0]);
}
if (a[i] % 2) dp[i][0] = min(dp[i][0], dp[i - 1][2]);
else dp[i][0] = min(dp[i][0], dp[i - 1][1]);
if (a[i] == 1) {
dp[i][0] = min(dp[i][0], dp[i - 1][3]);
dp[i][0] = min(dp[i][0], dp[i - 1][2]);
dp[i][3] = min(dp[i][3], dp[i - 1][3]);
dp[i][3] = min(dp[i][3], dp[i - 1][2]);
if (isprime(a[i - 1] + 1)) {
dp[i][3] = min(dp[i][3], dp[i - 1][0]);
}
} else {
dp[i][3] = min(dp[i][3], dp[i - 1][3] + 1);
dp[i][3] = min(dp[i][3], dp[i - 1][2] + 1);
if (isprime(a[i - 1] + 1)) {
dp[i][3] = min(dp[i][3], dp[i - 1][0] + 1);
}
}
if (a[i - 1] % 2) {
dp[i][2] = min(dp[i][2], dp[i - 1][0] + 1);
} else {
dp[i][1] = min(dp[i][1], dp[i - 1][0] + 1);
}
dp[i][2] = min(dp[i][2], dp[i - 1][1] + 1);
dp[i][1] = min(dp[i][1], dp[i - 1][2] + 1);
dp[i][2] = min(dp[i][2], dp[i - 1][3] + 1);
}
cout << min({dp[n - 1][0], dp[n - 1][1], dp[n - 1][2], dp[n - 1][3]}) << '\n';
}
int main() {
ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);
int t = 1;
// cin >> t;
while(t--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3596kb
input:
6 1 5 1 4 4 1
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3620kb
input:
9 30 6 7 12 15 8 20 17 14
output:
4
result:
ok 1 number(s): "4"
Test #3:
score: -100
Wrong Answer
time: 1ms
memory: 3600kb
input:
1568 119 519 706 1003 1317 322 25 1466 816 525 1 1122 38 1511 774 515 274 780 647 230 1602 1051 810 1 1 1232 1 1202 1583 412 1111 168 309 1181 184 1260 491 764 809 1213 804 1470 1 1087 1235 1004 673 1338 1333 1392 1036 1539 268 1 712 727 297 404 1317 36 463 1067 1133 693 931 46 1 100 1608 965 1 1406...
output:
745
result:
wrong answer 1st numbers differ - expected: '733', found: '745'