水一个链接
算法1
(线性筛质数) $O(n)$
C++ 代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010;
int prime[N], cnt, al[26];
bool st[N];
void work(int n)
{
for (int i = 2; i <= n; i ++) {
if (!st[i]) prime[cnt ++] = i;
for (int j = 0; j < cnt && prime[j] <= n / i; j ++)
{
st[prime[j] * i] = true;
if (i % prime[j] == 0) break;
}
}
}
int main()
{
string s;
cin >> s;
for (int i = 0; i < s.size(); i ++) {
al[s[i] - 'a'] ++;
}
int maxi = 0, mini = 1e9;
for (int i = 0; i < 26; i ++) {
if (al[i]) maxi = max(maxi, al[i]);
if (al[i]) mini = min(mini, al[i]);
}
int res = maxi - mini;
if (st[res] || res < 2) puts("No Answer"), puts("0");
else puts("Lucky Word"), cout << res << endl;
return 0;
}