zoukankan      html  css  js  c++  java
  • ural 1356. Something Easier 哥德巴赫猜想

    1356. Something Easier

    Time limit: 1.0 second
    Memory limit: 64 MB
     
    “How do physicists define prime numbers? Very easily: prime numbers are the number 2 and all the odd numbers greater than 2. They may show that this definition corresponds to the mathematical one: 3 is prime, 5 is prime, 7 is prime… 9? 9 is certainly not prime. Then: 11 is prime, 13 is prime. So 9 is the experiment mistake.”
    From mathematical analysis course
    Once physicist and mathematician argued how many prime numbers one needed for the purpose that their sum was equal to N. One said that it wasn’t known and the other that 3 was always enough. The question is how many.

    Input

    The first line contains T, an amount of tests. Then T lines with integer N follow (0 ≤ T ≤ 20; 2 ≤ N ≤ 109).

    Output

    For each test in a separate line you should output prime numbers so that their sum equals to N. An amount of such prime numbers is to be minimal possible.

    Sample

    inputoutput
    7
    2
    27
    85
    192
    14983
    3
    7
    
    2
    23 2 2
    2 83
    11 181
    14983
    3
    7
    

    Problem Author: Aleksandr Bikbaev
    Problem Source: USU Junior Championship March'2005

    思路:(1) 当n为偶数的时候, n > 4  n可以分解成两个素数的和

       (2)当n为奇数的时候, 如果n是素数就直接输出;

       如果n-2也是素数, 可以直接输出 2 n-2;

       那么把奇数n分解为三个奇素数;

    很好奇这么高的复杂度,竟然跑的这么快啊

     1 #include <iostream>
     2 #include <cstdio>
     3 using namespace std;
     4 
     5 bool fun(int x){
     6     if(x <= 1) return false;
     7     for(int i = 2; i*i <= x; i++){
     8         if(x%i == 0){
     9             return false;
    10         }
    11     }
    12     return true;
    13 }
    14 
    15 int main() {
    16     //freopen("in.txt", "r", stdin);
    17     int t, n;
    18     scanf("%d", &t);
    19     while(t--) {
    20         scanf("%d", &n);
    21         if(n%2 == 0){
    22             if(n == 2){
    23                 puts("2");
    24             }else if(n == 4){
    25                 puts("2 2");
    26             }else {
    27                 for(int i = 3; i < n; i += 2){
    28                     if(fun(i) && fun(n-i)){
    29                         printf("%d %d
    ", i, n-i);
    30                         break;
    31                     }
    32                 }
    33             }
    34         }else {
    35             if(fun(n)) printf("%d
    ", n);
    36             else {
    37                 if(fun(n-2)){
    38                     printf("2 %d
    ", n-2);
    39                 }else {
    40                     int flag = 1;
    41                     for(int i = 3; i < n; i += 2){
    42                         for(int j = 3; j < n - i; j += 2){
    43                             if(fun(i) && fun(j) && fun(n-i-j)){
    44                                 if(flag) {
    45                                 printf("%d %d %d
    ", i, j, n-i-j);
    46                                 flag = 0;
    47                                 break;
    48                                 }
    49                             }
    50                         }
    51                         if(!flag) break;
    52                     }
    53                 }
    54             }
    55         }
    56     }
    57     return 0;
    58 }
  • 相关阅读:
    思考:如何保证服务稳定性?
    svn:Item is out of date解决办法
    MAC OS 10.15 Lucene 源码分析环境搭建
    防止数据重复提交的6种方法(超简单)!
    6种快速统计代码执行时间的方法,真香!
    漫画:Integer 竟然有 6 种比较方式?
    IDEA 不为人知的 5 个骚技巧!真香!
    自由职业半年之后,我又滚回职场了...
    为什么建议你使用枚举?
    ESP8266
  • 原文地址:https://www.cnblogs.com/cshg/p/5894619.html
Copyright © 2011-2022 走看看