zoukankan      html  css  js  c++  java
  • Goldbach’s Conjecture(信息学奥赛一本通 1622)

    【题目描述】

    原题来自:Ulm Local,题面详见:POJ 2262

    哥德巴赫猜想:任何大于 44 的偶数都可以拆成两个奇素数之和。 比如:

    8=3+5
    20=3+17=7+13
    42=5+37=11+31=13+29=19+23

    你的任务是:验证小于 106 的数满足哥德巴赫猜想。

    【输入】

    多组数据,每组数据一个 n

    读入以 0 结束。

    【输出】

    对于每组数据,输出形如 n=a+b,其中 a,b 是奇素数。若有多组满足条件的 a,b,输出 b−a 最大的一组。

    若无解,输出 Goldbachs conjecture is wrong.

    【输入样例】

    8
    20
    42
    0

    【输出样例】

    8 = 3 + 5
    20 = 3 + 17
    42 = 5 + 37

    【提示】

    数据范围与提示:

    对于全部数据,6≤n≤106 。


    典型模板题...用欧拉筛真的肥肠简单

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int N=1e6+1;
     4 int a[N],n,cnt,v[N];
     5 int read()
     6 {
     7     int x=0,f=1;
     8     char ch=getchar();
     9     while(ch<'0'||ch>'9')
    10     {
    11         if(ch=='-') f=-1;
    12         ch=getchar();
    13     }
    14     while(ch>='0'&&ch<='9')
    15     {
    16         x=x*10+ch-'0';
    17         ch=getchar();
    18     }
    19     return x*f;
    20 }
    21 void write(int x)
    22 {
    23     if(x<0)
    24     {
    25         putchar('-');
    26         x=-x;
    27     }
    28     if(x>9) write(x/10);
    29     putchar(x%10+'0');
    30 }
    31 void pre()
    32 {
    33     for(int i=2;i<=1000000;i++)
    34     {
    35         if(!v[i])a[++cnt]=i;
    36         for(int j=1;j<=cnt&&a[j]*i<=1000000;j++)
    37         {
    38             v[i*a[j]]=1;
    39             if(i%a[j]==0)break;
    40         }
    41     }
    42 }
    43 int main()
    44 {
    45     pre();
    46     while(1)
    47     {
    48         n=read();
    49         if(!n)break;
    50         bool flag=0;
    51         for(int i=2;i<=cnt&&i<n;i++)
    52             if(!v[n-a[i]])
    53             {
    54                 printf("%d = %d + %d
    ",n,a[i],n-a[i]);
    55                 flag=1;break;
    56             }
    57         if(!flag)puts("Goldbach's conjecture is wrong.");
    58     }
    59     
    60     return 0;
    61 }
  • 相关阅读:
    虚拟机下unbuntu上网
    Ubuntu 用户切换和管理
    Brew程序模拟器上运行出现中文乱码
    struts2+spring+hibernate实例
    ubuntu ip设置
    JavaScript常用总结
    C++动态创建二维数组和清空cin缓冲
    🍖类的组合
    🍖鸭子类型
    🍖类的多态与多态性
  • 原文地址:https://www.cnblogs.com/ljy-endl/p/11378239.html
Copyright © 2011-2022 走看看