LDUOJ-Varoland continent (prime sieve and Goldbach conjecture)

(Link to the original question) [http://47.110.142.74/contest/1681/problem/0]
Question meaning:
Give a number n, the sequence is 1~n. Now divide the sequence into k groups, so that the sum of the numbers in each group is prime and k is the smallest. If k is equal, the first smaller number is output first.
Idea:
Goldbach conjecture, an even number can be written as the sum of two prime numbers, and an odd number can be written as the sum of three prime numbers. This is for sure.
The problem limit k is the smallest, so we should first consider whether an odd number can be written as the sum of two prime numbers.
It is equivalent to judging 1+2+……+n, after sifting prime numbers, enumerating one or two of them, and judging whether it is reasonable.
Pay attention to several situations, such as when n is 2.
Code:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;typedef unsigned long long ull;
typedef pair<ll,ll>PLL;typedef pair<int,int>PII;typedef pair<double,double>PDD;
#define I_int ll
inline ll read(){
    
    ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){
    
    if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){
    
    x=x*10+ch-'0';ch=getchar();}return x*f;}
#define read read()
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)
ll ksm(ll a,ll b,ll p){
    
    ll res=1;while(b){
    
    if(b&1)res=res*a%p;a=a*a%p;b>>=1;}return res;}
#define PI acos(-1)
#define x first
#define y second
const int N=1e7+7;
int pri[N],cnt;
bool st[N];
void prim(int x){
    
    
    for(int i=2;i<=x;i++){
    
    
        if(!st[i]) pri[cnt++]=i;
        for(int j=0;pri[j]<=x/i;j++){
    
    
            st[pri[j]*i]=true;
            if(i%pri[j]==0) break;
        }
    }
}
int main(){
    
    
    prim(1e7);
    ll n=read;
    if(!n||n==1){
    
    
        puts("-1");
        return 0;
    }
    if(n==2){
    
    
        puts("1");
        puts("3");
        return 0;
    }
    n=(n+1)*n/2;
    if(n%2==0){
    
    
        puts("2");
        for(int i=0;i<cnt;i++)
            if(n-pri[i]>0&&!st[n-pri[i]]){
    
    
                int a[2];
                a[0]=pri[i],a[1]=n-pri[i];
                //sort(a,a+1);
                printf("%d %d\n",a[0],a[1]);
                return 0;
            }
    }
    else{
    
    
        bool flag=0;
        for(int i=0;i<cnt;i++)
            if(n-pri[i]>0&&!st[n-pri[i]]){
    
    
                int a[2];
                a[0]=pri[i],a[1]=n-pri[i];
                //sort(a,a+1);
                printf("2\n%d %d\n",a[0],a[1]);
                flag=1;
                return 0;
            }
        if(flag) return 0;
        puts("3");
        for(int i=0;i<cnt;i++)
            for(int j=i;j<cnt;j++)
                if(n-pri[i]-pri[j]>0&&!st[n-pri[i]-pri[j]]){
    
    
                    int a[3];
                    a[0]=pri[i],a[1]=pri[j],a[2]=n-pri[i]-pri[j];
                   // sort(a,a+2);
                    printf("%d %d %d\n",a[0],a[1],a[2]);
                    return 0;
                }
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45675097/article/details/114198831