zoukankan      html  css  js  c++  java
  • Dima and Lisa(哥德巴赫猜想)

    Problem description

    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. 1 ≤ k ≤ 3
    2. pi is a prime
    3. $sum_{i=1}^k p_i = n $

    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.

    Input

    27

    Output

    3
    5 11 11

    Note

    A prime is an integer strictly larger than one that is divisible only by one and by itself.

    解题思路:将一个奇数拆分成1~3个素数,暴力即过!

    哥德巴赫猜想:随便取某一个奇数,比如77,可以把它写成三个素数之和,即77=53+17+7;再任取一个奇数,比如461,可以表示成461=449+7+5,也是三个素数之和,461还可以写成257+199+5,仍然是三个素数之和。例子多了,即发现“任何大于5的奇数都是三个素数之和。”当然此题只是要求可以拆分成1~3个素数,并不要求一定是3个素数,譬如461本身就是素数,则此时直接输出1 461即可。

    AC代码(31ms):

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 bool isprime(int x){
     4     if(x<=1)return false;
     5     for(int i=2;i*i<=x;++i)
     6         if(x%i==0)return false;
     7     return true;
     8 }
     9 int main(){
    10     int n;cin>>n;bool flag=false;
    11     if(isprime(n))cout<<"1
    "<<n<<endl;//如果本身是素数,直接输出即可
    12     else{
    13         for(int i=3;i<=n;i+=2){//从3开始按奇数来枚举
    14             if(isprime(i)){
    15                 int tmp=n-i;
    16                 if(isprime(tmp)){cout<<"2
    "<<i<<' '<<tmp<<endl;break;}
    17                 for(int j=3;j<=n;j+=2)//从3开始按奇数来枚举
    18                     if(isprime(j) && isprime(tmp-j)){cout<<"3
    "<<i<<' '<<j<<' '<<(tmp-j)<<endl;flag=true;break;}
    19                 if(flag)break;
    20             }
    21         }
    22     }
    23     return 0;
    24 }
  • 相关阅读:
    临摹帖
    PC+PLC通过Modbus协议构建工控系统
    mvn打包时出现一个子模块找不到另一个子模块的情况
    T-SQL——函数——字符串操作函数
    T-SQL——透视PIVOT动态获取待扩展元素集
    T-SQL——数据透视和逆透视
    T-SQL——关于表数据的复制插入
    T-SQL——关于SQL读取Excel文件
    T-SQL——关于SqlPrompt的使用
    微信小程序--聊天室小程序(云开发)
  • 原文地址:https://www.cnblogs.com/acgoto/p/9157876.html
Copyright © 2011-2022 走看看