QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#369772#7624. Mystery of PrimeSunlight9#WA 3ms10784kbC++202.9kb2024-03-28 17:46:272024-03-28 17:46:28

Judging History

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

  • [2024-03-28 17:46:28]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:10784kb
  • [2024-03-28 17:46:27]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN=5e5+7;
int n;
int a[MAXN],prime[MAXN*2],cnt=0;
int dp[MAXN/2][3][2];
bool vis[MAXN*2];
inline void pre()
{
    vis[1]=1;
    int lim=1e6;
    for(int i=2;i<=lim;i++)
    {
        if(!vis[i]) prime[++cnt]=i;
        for(int j=1;j<=cnt && i*prime[j]<=lim;j++)
        {
            vis[i*prime[j]]=1;
            if(i%prime[j]==0) break;
        }
    }
    return;
}
int main()
{
    pre();
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    memset(dp,0x3f,sizeof(dp));
    if(a[1]==1)
    {
        dp[1][2][0]=0;
        dp[1][1][1]=1;
        dp[1][0][1]=1;
    }
    else if(a[1]%2)
    {
        dp[1][1][0]=0;
        dp[1][2][1]=1;
        dp[1][0][1]=1;
    }
    else
    {
        dp[1][0][0]=0;
        dp[1][2][1]=1;
        dp[1][1][1]=1;
    }
    for(int i=2;i<=n;i++)
    {
        if(a[i]==1)
        {
            dp[i][2][0]=min(dp[i][2][0],dp[i-1][0][1]);
            if(!vis[1+a[i-1]]) dp[i][2][0]=min(dp[i][2][0],dp[i-1][0][0]);
            dp[i][2][0]=min(dp[i][2][0],min(dp[i-1][2][0],dp[i-1][2][1])+1);

            dp[i][0][1]=min(dp[i][0][1],min(dp[i-1][1][0],dp[i-1][1][1])+1);
            dp[i][0][1]=min(dp[i][0][1],min(dp[i-1][2][0],dp[i-1][2][1])+1);

            dp[i][1][1]=min(dp[i][1][1],min(dp[i-1][0][1],dp[i-1][0][0])+1);
        }
        else if(a[i]%2)
        {
            dp[i][1][0]=min(dp[i][1][0],dp[i-1][0][1]);
            if(!vis[a[i]+a[i-1]]) dp[i][1][0]=min(dp[i][1][0],dp[i-1][0][0]);

            dp[i][1][1]=min(dp[i][1][1],min(dp[i-1][0][0],dp[i-1][0][1])+1);
            
            dp[i][0][1]=min(dp[i][0][1],min(dp[i-1][1][0],dp[i-1][1][1])+1);
            dp[i][0][1]=min(dp[i][0][1],min(dp[i-1][2][0],dp[i-1][2][1])+1);

            dp[i][2][1]=min(dp[i][2][1],dp[i-1][0][1]+1);
            if(!vis[a[i-1]+1]) dp[i][2][1]=min(dp[i][2][1],dp[i-1][0][0]+1);
            dp[i][2][1]=min(dp[i][2][1],min(dp[i-1][2][0],dp[i-1][2][1])+1);
        }
        else
        {
            dp[i][0][0]=min(dp[i][0][0],dp[i-1][1][1]);
            if(!vis[a[i-1]+a[i]]) dp[i][0][0]=min(dp[i][0][0],dp[i-1][1][0]);
            if(!vis[a[i]+1]) dp[i][0][0]=min({dp[i][0][0],dp[i-1][2][0],dp[i-1][2][1]});

            dp[i][0][1]=min(dp[i][0][0],min(dp[i-1][1][1],dp[i-1][1][0])+1);
            dp[i][0][1]=min(dp[i][0][0],min(dp[i-1][2][1],dp[i-1][2][0])+1);

            dp[i][1][1]=min(dp[i][1][1],min(dp[i-1][0][1],dp[i-1][0][0])+1);

            dp[i][2][1]=min(dp[i][2][1],dp[i-1][0][1]+1);
            if(!vis[a[i-1]+1]) dp[i][2][1]=min(dp[i][2][1],dp[i-1][0][0]+1);
            dp[i][2][1]=min(dp[i][2][1],min(dp[i-1][2][0],dp[i-1][2][1])+1);
        }
    }
    int ans=1e9+7;
    for(int i=0;i<3;i++) ans=min(ans,min(dp[n][i][0],dp[n][i][1]));
    cout<<ans;
    return 0;
}
/*
6
1 5 1 4 4 1

9
30 6 7 12 15 8 20 17 14
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 10744kb

input:

6
1 5 1 4 4 1

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: 0
Accepted
time: 3ms
memory: 10784kb

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: 0ms
memory: 10760kb

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:

662

result:

wrong answer 1st numbers differ - expected: '733', found: '662'