Acm Dima and Lisa的题解

D. Dima and Lisa time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Dima loves representing an odd number as the sum of multiple primes, and Lisa loves it when there are at most three primes. Help them to represent the given number as the sum of at most than three primes.

More formally, you are given an odd numer n. Find a set of numbers pi (1?≤?i?≤?k), such that

1?≤?k?≤?3 pi is a prime

The numbers pi do not necessarily have to be distinct. It is guaranteed that at least one possible solution exists.

Input The single line contains an odd number n (3?≤?n?<?109).

Output In the first line print k (1?≤?k?≤?3), showing how many numbers are in the representation you found.

In the second line print numbers pi in any order. If there are multiple possible solutions, you can print any of them.

Examples input 27 output 3 5 11 11

 

http://codeforces.com/problemset/problem/584/D

 

 

题意不难理解  可是还是有一点坑点的  我本来是两个for循环直接暴力的   可是这样会有bug   后来想到了哥德巴赫猜想   大于6的偶数都能分解为两个奇质数之和

所以除了3 4 5    其他的数    偶数的话  就先搞个2出来   奇数就先搞个3    剩下的数就一定可以分解了 

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;

long long ans[5];

int zhishu(long long n){
    
    for(int i=2;i<=sqrt(n);i++){
        
        if(n%i==0) return 0;
        
    }
    return 1;
    
}

//哥德巴赫猜想耶~~~   好神奇呀 

int main(){
    
    long long n;
    scanf("%lld",&n);
    if(n>=6){
    
        
        if(n%2==0) ans[1]=2;
        else ans[1]=3;
        for(int i=2;i<=n-ans[1];i++){
            
            if(zhishu(i)==1&&zhishu(n-ans[1]-i)==1){
                
                ans[2]=i;
                ans[3]=n-ans[1]-i;
                printf("3\n%lld %lld %lld\n",ans[1],ans[2],ans[3]);
                return 0;
                
            }
            if(i>2) i++;
            
        }
        
    }
    else{
        
        if(n==3) printf("1\n%lld\n",n);
        else if(n==4||n==5){
            
            n=n-2;
            printf("2\n2 %lld\n",n);
            
        }
        
    }
    
}

 

posted @ 2016-05-21 12:41  困困困  阅读(104)  评论(0编辑  收藏  举报