QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#546329#7624. Mystery of PrimeArvinWA 4ms9876kbC++204.6kb2024-09-03 23:22:332024-09-03 23:22:33

Judging History

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

  • [2024-09-03 23:22:33]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:9876kb
  • [2024-09-03 23:22:33]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define ld long double
#define lll  __int128
#define dhm std::fixed<<std::setprecision
#define prq priority_queue
using ll = long long;
using ull = unsigned long long;
int T;
template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using max_heap = priority_queue<T, vector<T>, less<T>>;
struct Sieve {
	std::vector<int> primes;
	std::vector<int> factor;
	std::vector<bool> is_prime;
	Sieve(int n) : factor(n + 1, -1), is_prime(n + 1, true) {
		is_prime[0] = is_prime[1] = false;
		for (int i = 2; i <= n; i++) {
			if (is_prime[i]) {
				primes.push_back(i);
				factor[i] = i;
			}
			for (int j = 0; j < (int)primes.size() && i * primes[j] <= n; j++) {
				is_prime[i * primes[j]] = false;
				factor[i * primes[j]] = primes[j];
				if (i % primes[j] == 0) break;
			}
		}
	}
} seive(3e5 + 1);
int dp[100005][5];//0 不变 1 变奇数 2 变偶数 3 变1
void solve(){
	memset(dp,0x7f,sizeof(dp));
	int n;
	std::cin>>n;
	std::vector<int>a(n+1);
	for(int i = 1; i <= n ; i ++) {
		std::cin>>a[i];
	}
	if(a[1]==1) {
		dp[1][0]=0;
		dp[1][1]=0;
		dp[1][2]=1;
		dp[1][3]=0;
	}
	else if(a[1]%2==1&&a[1]!=1) {
		dp[1][0]=0;
		dp[1][1]=0;
		dp[1][2]=1;
		dp[1][3]=1;
	}
	else if(a[1]%2==0) {
		dp[1][0]=0;
		dp[1][1]=1;
		dp[1][2]=0;
		dp[1][3]=1;
	}
	for(int i=2;i<=n;i++) {
		if(seive.is_prime[a[i]+a[i-1]]) {
			if(a[i]==1) {
				dp[i][0]=min({dp[i][0],dp[i-1][3],dp[i-1][2],dp[i-1][0]});
				if(a[i-1]==1) {
					dp[i][1]=min({dp[i][1],dp[i-1][2]+1});
					dp[i][2]=min({dp[i][2],dp[i-1][1]+1,dp[i-1][0]+1});
				}
				else {
					dp[i][1]=min({dp[i][1],dp[i-1][0]+1,dp[i-1][2]+1});
					dp[i][2]=min({dp[i][2],dp[i-1][1]+1,dp[i-1][3]+1});
				}
				dp[i][3]=min({dp[i][3],dp[i-1][0],dp[i-1][3],dp[i-1][2]});
			}
			else if(a[i]%2==1) {
				dp[i][0]=min({dp[i][0],dp[i-1][0],dp[i-1][2]});
				dp[i][1]=min({dp[i][1],dp[i-1][0]+1,dp[i-1][2]+1});
				dp[i][2]=min({dp[i][2],dp[i-1][1]+1,dp[i-1][3]+1});
				if(seive.is_prime[a[i-1]+1]) {
					dp[i][3]=min({dp[i][3],dp[i-1][0]+1,dp[i-1][2]+1,dp[i-1][3]+1});
				}
				else {
					dp[i][3]=min({dp[i][3],dp[i-1][2]+1,dp[i-1][3]+1});
				}
			}
			else {
				dp[i][0]=min({dp[i][0],dp[i-1][0],dp[i-1][1]});
				dp[i][2]=min({dp[i][2],dp[i-1][0]+1,dp[i-1][1]+1,dp[i-1][3]+1});
				dp[i][1]=min(dp[i][1],dp[i-1][2]+1);
				if(seive.is_prime[a[i]+1]) {
					dp[i][0]=min(dp[i][0],dp[i-1][3]);
				}
				if(a[i-1]==1) dp[i][3]=min({dp[i][3],dp[i-1][2]+1,dp[i-1][0]+1,dp[i-1][3]+1});
				else dp[i][3]=min({dp[i][3],dp[i-1][2]+1,dp[i-1][3]+1});
			}
		}
		else {
			if(a[i-1]==1) {
				dp[i][1]=min(dp[i][1],dp[i-1][2]+1);
				dp[i][2]=min({dp[i][2],dp[i-1][0]+1,dp[i-1][1]+1,dp[i-1][3]+1});
				dp[i][3]=min({dp[i][3],dp[i-1][0]+1,dp[i-1][2]+1,dp[i-1][3]+1});
				if(a[i]%2==1) {
					dp[i][0]=min(dp[i][0],dp[i-1][2]);
				}
				else {
					dp[i][0]=min(dp[i][0],dp[i-1][1]);
				}
			}
			else if(a[i-1]%2==1) {
				dp[i][1]=min(dp[i][1],dp[i-1][2]+1);
				dp[i][2]=min({dp[i][2],dp[i-1][0]+1,dp[i-1][1]+1,dp[i-1][3]+1});
				if(a[i]==1) {
					dp[i][0]=min({dp[i][0],dp[i-1][2],dp[i-1][3]});
					dp[i][3]=min({dp[i][3],dp[i-1][2],dp[i-1][3]});
				}
				else if(a[i]%2==1) {
					dp[i][0]=min(dp[i][0],dp[i-1][2]);
					dp[i][3]=min({dp[i][3],dp[i-1][2]+1,dp[i-1][3]+1});
				}
				else {
					dp[i][3]=min({dp[i][3],dp[i-1][2]+1,dp[i-1][3]+1});
					if(seive.is_prime[a[i]+1]) dp[i][0]=min({dp[i][0],dp[i-1][1],dp[i-1][3]});
					else dp[i][0]=min(dp[i][0],dp[i-1][1]);
				}
			}
			else {
				dp[i][2]=min({dp[i][2],dp[i-1][1]+1,dp[i-1][3]+1});
				dp[i][1]=min({dp[i][1],dp[i-1][2]+1,dp[i-1][0]+1});
				if(a[i]==1){
					dp[i][0]=min({dp[i][0],dp[i-1][2],dp[i-1][3]});
					dp[i][3]=min({dp[i][3],dp[i-1][2],dp[i-1][3]});
				}
				else if(a[i]%2==1) {
					dp[i][0]=min(dp[i][0],dp[i-1][2]);
					if(seive.is_prime[a[i-1]+1]) dp[i][3]=min({dp[i][3],dp[i-1][2]+1,dp[i-1][0]+1,dp[i-1][3]+1});
					else dp[i][3]=min({dp[i][3],dp[i-1][2]+1,dp[i-1][3]+1});
				}
				else {
					if(seive.is_prime[a[i]+1]) dp[i][0]=min({dp[i][0],dp[i-1][1],dp[i-1][3]});
					else dp[i][0]=min(dp[i][0],dp[i-1][1]);
					if(seive.is_prime[a[i-1]+1]) dp[i][3]=min({dp[i][3],dp[i-1][2]+1,dp[i-1][0]+1});
					else dp[i][3]=min(dp[i][3],dp[i-1][2]+1);
				}
			}
		}
		// cout<<dp[i][0]<<" "<<dp[i][1]<<" "<<dp[i][2]<<" "<<dp[i][3]<<endl;
	}
	cout<<min({dp[n][0],dp[n][1],dp[n][2],dp[n][3]})<<endl;
}
signed main(){
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
    T = 1;
	// std::cin>>T;
	while(T --)
	solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 9588kb

input:

6
1 5 1 4 4 1

output:

2

result:

ok 1 number(s): "2"

Test #2:

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

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: 9876kb

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:

734

result:

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