QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#553158 | #7624. Mystery of Prime | TYU0_0 | WA | 1ms | 3676kb | C++14 | 1.5kb | 2024-09-08 10:03:09 | 2024-09-08 10:03:09 |
Judging History
answer
#include <bits/stdc++.h>
const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;
int n, a[N], f[N][4];
int main()
{
std::cin >> n;
for (int i = 1; i <= n; i++)
std::cin >> a[i];
for (int i = 1; i <= n; i++)
f[i][0] = f[i][1] = f[i][2] = f[i][3] = INF;
f[1][0] = 0;
f[1][1] = !(a[1] == 1);
f[1][2] = !(a[1] % 2);
f[1][3] = (a[1] % 2 == 1 && a[1] > 1) ? 0 : 1;
auto is_prime = [&](const int &x)
{
for (int i = 2; i * i <= x; i++)
if (x % i == 0)
return false;
return true;
};
for (int i = 1; i <= n; i++)
{
if (is_prime(a[i] + a[i + 1]))
f[i + 1][0] = std::min(f[i + 1][0], f[i][0]);
if (a[i] % 2 == 1)
f[i + 1][2] = std::min(f[i + 1][2], f[i][0] + 1);
if (a[i] % 2 == 0)
f[i + 1][3] = std::min(f[i + 1][3], f[i][0] + 1);
if (is_prime(a[i] + 1))
f[i + 1][1] = std::min(f[i + 1][1], f[i][0] + 1);
if (is_prime(a[i + 1] + 1))
f[i + 1][0] = std::min(f[i + 1][0], f[i][1]);
f[i + 1][2] = std::min(f[i + 1][2], f[i][1] + 1);
f[i + 1][1] = std::min(f[i + 1][1], f[i][1] + 1);
if (a[i + 1] % 2 == 1)
f[i + 1][0] = std::min(f[i + 1][0], f[i][2]);
f[i + 1][1] = std::min(f[i + 1][1], f[i][2] + 1);
f[i + 1][3] = std::min(f[i + 1][3], f[i][2] + 1);
if (a[i + 1] % 2 == 0)
f[i + 1][0] = std::min(f[i + 1][0], f[i][3]);
f[i + 1][2] = std::min(f[i + 1][2], f[i][3] + 1);
}
int ans = f[n][0];
for (int i = 1; i <= 3; i++)
ans = std::min(ans, f[n][i]);
std::cout << ans << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3676kb
input:
6 1 5 1 4 4 1
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3540kb
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: 3664kb
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:
732
result:
wrong answer 1st numbers differ - expected: '733', found: '732'