#include<bits/stdc++.h>
using namespace std;
#define int long long
vector<int> p;
bool vis[1000005];
int f[1000005];
bool is[1000005];
bool g[2000][2000];
int n;
int get (string s) {
int num = 0;
for (auto t : s)
num = num * 10 + t - '0';
return num;
}
bool check (int tar) {
for (int i = 2; i <= tar / i; i ++)
if (tar % i==0) return 0;
return 1;
}
void init() {
for (int i = 2; i <= 1000000; i ++) {
if (!vis[i]) {
p.push_back(i);
is[i] = 1;
}
for (int j = 0; p[j] * i <= 1000000; j ++) {
vis[p[j] * i] = 1;
if (i % p[j] == 0) break;
}
}
}
int ans;
vector<int> v;
bool ck (int i) {
for (auto t : v)
if (!g[t][i] || !g[i][t]) return 0;
return 1;
}
void dfs (int x, int cnt) {
if (cnt + n - x <= ans) return ;
ans = max (ans, cnt);
if (cnt == 5) {
for (auto t : v) {
cout << p[t - 1]<< ' ';
}
puts("");
}
for (int i = x + 1; i <= n; i ++) {
if (ck(i)) {
v.push_back(i);
dfs(i, cnt + 1);
v.pop_back();
}
}
}
signed main () {
init();
cout << p.size() << endl;
for (int i = 0; i < p.size(); i ++)
vis[i + 1] = p[i];
while(p[n] < 10000)
n ++;
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= n; j ++) {
if (i == j) continue;
auto t = get(to_string(p[i - 1])+ to_string(p[j - 1]));
if (t % 2 == 0) continue;
if (check(t)) {
g[i][j] = 1;
}
}
}
for (int i = 1; i <= n ;i ++) {
v.push_back(i);
dfs(i, 1);
v.pop_back();
}
cout << ans << endl;
return 0;
}
最大团算法
对于加入的点,我们要判断是否与当前所有点连接,如果连接则团数加1,否则不算入