QOJ.ac
QOJ
ID | 提交记录ID | 题目 | Hacker | Owner | 结果 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|
#977 | #636444 | #9457. Prime Set | hos_lyric | ucup-team008 | Failed. | 2024-10-13 22:24:09 | 2024-10-13 22:24:09 |
詳細信息
Extra Test:
Accepted
time: 568ms
memory: 18668kb
input:
4 3000 1499 41 42 251 252 461 462 671 672 881 882 1091 1092 1301 1302 1511 1512 1721 1722 1931 1932 2141 2142 2351 2352 2561 2562 2771 2772 2981 2982 3191 3192 3401 3402 3611 3612 3821 3822 4031 4032 4241 4242 4451 4452 4661 4662 4871 4872 5081 5082 5291 5292 5501 5502 5711 5712 5921 5922 6131 6132 ...
output:
2998 3000 3000 1000
result:
ok 4 lines
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#636444 | #9457. Prime Set | ucup-team008# | AC ✓ | 279ms | 10772kb | C++17 | 4.4kb | 2024-10-12 23:55:27 | 2024-10-13 19:28:19 |
answer
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
typedef vector<int> vi;
#define f first
#define s second
#define derr if(0) cerr
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << flush;
// END NO SAD
template<class Fun>
class y_combinator_result {
Fun fun_;
public:
template<class T>
explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}
template<class ...Args>
decltype(auto) operator()(Args &&...args) {
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
template<class T>
bool updmin(T& a, T b) {
if(b < a) {
a = b;
return true;
}
return false;
}
template<class T>
bool updmax(T& a, T b) {
if(b > a) {
a = b;
return true;
}
return false;
}
typedef int64_t ll;
typedef long double ld;
const int SZ = 2000005;
bool sieve[SZ];
void rsolve() {
int n, k;
cin >> n >> k;
vector<int> v(n);
for(auto& x: v) cin >> x;
vector<int> dp(n, -39);
vector<vector<int>> edges(n);
auto matching = [&]() -> int {
vector<int> seen(n);
int cn = 0;
auto dfs = y_combinator([&](auto self, int curr) -> bool {
seen[curr] = cn;
for(int out: edges[curr]) {
if(seen[out] == cn) continue;
seen[out] = cn;
if(dp[out] == -1 || (dp[out] >= 0 && self(dp[out]))) {
dp[out] = curr;
dp[curr] = out;
return true;
}
}
return false;
});
int ret = 0;
for(int i = 0; i < n; i++) {
cn++;
if(dp[i] == -1) ret += dfs(i);
}
return ret;
};
for(int i = 0; i < n; i++) for(int j = i+1; j < n; j++) {
if(!sieve[v[i]+v[j]]) {
edges[i].pb(j);
edges[j].pb(i);
dp[i] = -1;
dp[j] = -1;
}
}
int ret = matching();
if(ret >= k) cout << 2*k << "\n";
else {
int unmatched = 0;
for(int i = 0; i < n; i++) {
if(dp[i] == -1) unmatched++;
}
cout << 2*ret+min(unmatched, k-ret) << "\n";
}
}
void solve() {
sieve[0] = true;
sieve[1] = true;
for(int i = 2; i * i < SZ; i++) {
for(int j = i*i; j < SZ; j += i) sieve[j] = true;
}
int t;
cin >> t;
while(t--) rsolve();
}
// what would chika do
// are there edge cases (N=1?)
// are array sizes proper (scaled by proper constant, for example 2* for koosaga tree)
// integer overflow?
// DS reset properly between test cases
// are you doing geometry in floating points
// are you not using modint when you should
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}