QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#730188#7624. Mystery of Primegates_orzWA 3ms5040kbC++202.7kb2024-11-09 19:06:592024-11-09 19:06:59

Judging History

你现在查看的是最新测评结果

  • [2024-11-09 19:06:59]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:5040kb
  • [2024-11-09 19:06:59]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

using LL=long long;
const int N=3e5+10;
const int M=N*2;
const LL INF=1e18;
const int mod=998244353;
int n,m;
int prime[N], num;
int vis[N];

void find_prime(int n) {
    int i, j;
    for (i = 2; i < n; i++) {
        if (!vis[i])prime[++num] = i;
        for (j = 1; j <= num && prime[j] <= n / i; j++) {
            vis[i * prime[j]] = 1;
            if (i % prime[j] == 0)break;
        }
    }
}
void solve() {
    find_prime(200010);
    cin>>n;
    vector<int>a(n+1);
    vector<vector<int>>dp(n+1,vector<int>(4,-1));
    for(int i=1;i<=n;i++) {
        cin>>a[i];
    }
    //不变
    //改为1
    //改为偶数
    //改为>1的奇数
    dp[1][0]=0,dp[1][1]=0,dp[1][2]=0,dp[1][3]=0;
    if(a[1]!=1)dp[1][1]=1;
    if(a[1]%2!=0)dp[1][2]=1;
    if(a[1]%2!=1||a[1]==1)dp[1][3]=1;
    auto change=[](int &x,int y) {
        if(y==-1)return;
        x==-1?x=y:x=min(x,y);
    };
    for(int i=2;i<=n;i++) {
        //不变
        if(dp[i-1][0]!=-1) {
            if(!vis[a[i-1]+a[i]]) {
                change(dp[i][0],dp[i-1][0]);
            }
        }
        if(dp[i-1][1]!=-1) {
            if(!vis[1+a[i]]) {
                change(dp[i][0],dp[i-1][1]);
            }
        }
        if(dp[i-1][2]!=-1) {
            if(a[i]&1) {
                change(dp[i][0],dp[i-1][2]);
            }
        }
        if(dp[i-1][3]!=-1) {
            if(a[i]%2==0) {
                change(dp[i][0],dp[i-1][3]);
            }
        }

        //改为1
        if (dp[i - 1][0] != -1) {
            if (!vis[1 + a[i - 1]]) {
                change(dp[i][1], dp[i-1][0]+(a[i]!=1));
            }
        }
        if(dp[i-1][1]!=-1) {
            change(dp[i][1],dp[i-1][1]+(a[i]!=1));
        }
        if(dp[i-1][2]!=-1) {
            change(dp[i][1],dp[i-1][2]+(a[i]!=1));
        }

        //改为偶数
        if(dp[i-1][0]!=-1) {
            if(a[i-1]&1) {
                change(dp[i][2],dp[i-1][0]+1);
            }
        }
        if(dp[i-1][1]!=-1) {
            change(dp[i][2],dp[i-1][1]+1);
        }
        if(dp[i-1][3]!=-1) {
            change(dp[i][2],dp[i-1][3]+1);
        }

        //改为>1奇数
        if(a[i-1]%2==0&&dp[i-1][0]!=-1) {
            change(dp[i][3],dp[i-1][0]+1);
        }
        if(dp[i-1][2]!=-1) {
            change(dp[i][3],dp[i-1][2]+1);
        }

    }
    int res=-1;
    change(res,dp[n][0]);
    change(res,dp[n][1]);
    change(res,dp[n][2]);
    change(res,dp[n][3]);
    cout<<res<<"\n";
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int T=1;
    while(T--)solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 4408kb

input:

6
1 5 1 4 4 1

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: 0
Accepted
time: 1ms
memory: 4408kb

input:

9
30 6 7 12 15 8 20 17 14

output:

4

result:

ok 1 number(s): "4"

Test #3:

score: 0
Accepted
time: 2ms
memory: 4772kb

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:

733

result:

ok 1 number(s): "733"

Test #4:

score: 0
Accepted
time: 0ms
memory: 4856kb

input:

3261
489 1809 920 1061 1423 2330 420 1090 1387 1272 2238 979 1 2788 1885 1739 2291 1 1225 2076 2355 2008 1753 1438 2047 1148 1294 2179 2763 2074 1259 272 769 2104 2829 643 905 2155 2432 2328 1369 1 786 832 1 1775 885 867 803 2830 1 2088 2625 2735 2028 1489 105 1647 2844 2365 1521 1295 2318 2929 1 27...

output:

1501

result:

ok 1 number(s): "1501"

Test #5:

score: 0
Accepted
time: 2ms
memory: 4664kb

input:

4861
753 448 1399 1244 3435 3244 69 4110 3901 2335 3418 3242 2207 191 379 282 2896 1 1068 1 66 3517 1992 1 642 2103 3205 162 2971 1168 699 1972 1823 2856 2567 1772 246 2075 3623 1574 3105 2797 4121 1 3583 763 3567 3730 2043 2347 3483 8 347 405 1 1 3918 1270 2821 2566 1093 1586 683 3578 3879 1854 314...

output:

2283

result:

ok 1 number(s): "2283"

Test #6:

score: 0
Accepted
time: 0ms
memory: 4696kb

input:

6388
1 1372 1768 3823 1 1996 5172 3926 5734 1 5234 5219 4671 976 3880 4779 4721 5488 549 3080 1900 1001 1732 1129 4383 2071 4035 2798 1 1065 5222 65 351 1 2038 376 1 5423 3204 1 2256 3992 2365 5184 3522 1958 3969 5644 4263 3790 2064 3653 20 5103 5648 1404 14 2525 4306 796 4413 4018 1 4828 6013 1763 ...

output:

3015

result:

ok 1 number(s): "3015"

Test #7:

score: 0
Accepted
time: 0ms
memory: 4720kb

input:

7037
5377 66 1267 1689 3544 5122 5995 2503 1174 4380 1 4937 7077 3803 6290 82 3105 3612 2369 1 2214 4702 7575 3148 1 3569 3287 7882 3739 7306 2774 2088 1 7226 6299 6454 7485 310 3051 1 4663 6758 1606 6919 6609 7243 1984 6330 4339 7046 3909 4910 4131 3829 5722 1 6216 5827 2205 3580 2689 4030 1973 1 6...

output:

3297

result:

ok 1 number(s): "3297"

Test #8:

score: 0
Accepted
time: 2ms
memory: 5040kb

input:

9795
3928 1555 4412 1 5069 7890 2761 1 7087 4174 1976 1905 1 252 377 2654 9457 191 5272 7469 5598 1640 2139 3397 6892 2441 1234 4951 1803 1 1 3010 2584 1239 3550 422 1 1548 5321 4027 6445 6202 8609 2276 8255 6696 3612 1621 8256 9001 648 5181 7970 6694 9825 6934 1 2914 2347 1 4874 9396 312 2124 3409 ...

output:

4614

result:

ok 1 number(s): "4614"

Test #9:

score: 0
Accepted
time: 2ms
memory: 4808kb

input:

11372
11482 11131 9002 7055 1 1 5112 11417 10956 1 4918 576 7024 8866 1 1 3060 840 9478 9925 3895 8076 2034 935 9528 11555 1598 3929 4598 2413 6404 6032 2255 1280 8715 4081 7197 6863 4464 4568 1 3466 7074 7813 1990 11019 4988 7173 6826 11271 2632 2223 530 6028 8215 10794 1122 5655 10912 2317 1926 19...

output:

5375

result:

ok 1 number(s): "5375"

Test #10:

score: -100
Wrong Answer
time: 3ms
memory: 4804kb

input:

12891
6490 2963 111 9520 5713 5735 7392 10432 2341 7481 9170 3947 3594 3935 8030 1 2186 7965 3824 119 1 1 8932 10099 3298 1 1 8526 1094 4963 8718 6634 5065 8604 1 5557 9766 1804 1268 2543 2373 9414 6027 1094 5183 2562 1051 7801 1372 8959 3700 9639 5472 349 1 4297 1482 6205 9221 1847 1 8616 1 3822 68...

output:

6094

result:

wrong answer 1st numbers differ - expected: '6095', found: '6094'