Educational Codeforces Round 178 (Rated for Div. 2) D. Array and GCD

题意

给定数组 , 可以执行操作:

  1. 将数组某个元素减去1 , 并且获得一个coin ;
  2. 将数组某个元素加上1 , 并且获得一个coin
    求使得数组的每个元素都\(\ge 2\) 并且任意两个元素互质 , 需要删除原数组元素的最小值

思路

任意两个元素互质的最小为质数序

然后就比较简单了 , 降序累加每个元素比较和素数前缀和的大小即可

赛时由于错误"借用了"被删除元素的coin , 导致4wa

这是一个理解题意的问题 , 以后狂wa的时候一定要多看题目 , 看有没有这种坑

另外涉及多种操作(删除 + 增减)必须要看清楚先后顺序

另另外 , 必须要查看公告

代码

#include<bits/stdc++.h>
using namespace std;
#define int long long int
inline int read() {
    int ans = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-')f = -1;
        ch = getchar();
    }
    while (ch <= '9' && ch >= '0') {
        ans = ans * 10 + ch - '0';
        ch = getchar();
    }
    return ans * f;
}
vector<int>prime;

const int N = 5800090 , M = 4e5+10;

int sum[M];
bool vis[N];
void pre() {
    prime.push_back(0);
    for (int i = 2; i< N; i++) {
        if (!vis[i]) {
            prime.push_back(i);
            for (int j = i<<1; j< N; j+=i) {
                vis[j] = true;
            }
        }
    }
}
int a[N];
void solve() {
    int n =read();
    for (int i = 1; i<= n; i++)a[i] = read();
    sort(a+1,a+1+n,greater<int>());
    int cur = 0;
    int ans = 0;
    for (int i =1; i<= n;i++) {
        cur += a[i];
        if (cur >= sum[i]) ans = i;
        else break;
    }
    cout<<n-ans<<"\n";
}

signed main() {
    pre();
    for (int i = 1; i< prime.size(); i++) {
        sum[i] = sum[i-1] +prime[i];
    }
    int t=read();
    while (t--) solve();
    return 0;
}
posted @ 2025-04-29 18:46  Guaninf  阅读(57)  评论(0)    收藏  举报