One day, Brz was studying prime factorization of positive integers, which struck Mandy as a surprise: isn’t it something that she had totally grasped in the second grade? So she told Brz the traditional prime factorization by multiplication was old-fashioned, and now she was more interested in prime factorization by addition. To introduce, Mandy showed Brz an easy problem as follows:
Let f(n) be the least number of prime numbers whose sum is exactly n, calculate Pn i=2 f(i). For example, f(2) = 1, f(6) = 2, since 2 = 2 and 6 = 3 + 3. It can be proved that there aren’t fewer prime numbers that satisfy the condition.
Since Brz had never studied the field of prime factorization by addition, he was confused. Can you help him find the answer
Since Brz had never studied the field of prime factorization by addition, he was confused. Can you help him find the answer
Output Q lines. The i-th line contains one integer representing the answer to the i-th query.
3 3 4 5
2 4 5
1 线性筛法:将所有的素数找出
2 欧拉猜想:大于2 的所有偶数都可以写成两个素数相加
3 衍生:对于所有数(2除外)有如下规律:(3-最少可以写成3个素数相加)
:
#include<bits/stdc++.h>
using namespace std;
int av[10000006]= {0};
int flag[10000006]= {0};
const int N = 1E7 + 10;
int primes[N];
int cnt=0;
bool st[N];
void get_primes(int n)
{
for(int i=2;i<=n;i++)
{
if(!st[i]) primes[cnt++]=i;
for(int j=0;j<cnt&&i*primes[j]<=n;j++)
{
st[i*primes[j]]=true;
if(i%primes[j]==0) break;
}
}
}
int main()
{
get_primes(N);
int n;
cin>>n;
for(int i=0;i<cnt;i++)
{
flag[primes[i]]++;
}
av[2]=1;
for(int i=3; i<1e7+5; i++)
{
if(i%2==0)
av[i]=av[i-1]+2;
else
{
if(flag[i])
{
av[i]=av[i-1]+1;
flag[i]++;
}
else if(flag[i-2]) av[i]=av[i-1]+2;
else av[i]=av[i-1]+3;
}
}
while(n--)
{
int a;scanf("%d",&a);
printf("%d\n",av[a]);
}
return 0;
}