Easy Problem of Prime(线性筛法,巴德赫猜想,欧拉猜想)

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值