zoukankan      html  css  js  c++  java
  • 部分有关素数的题

    HDU 2012 素数判定

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2012

    对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括x,y)(-39<=x<y<=50),判定该表达式的值是否都为素数。

    Input输入数据有多组,每组占一行,由两个整数x,y组成,当x=0,y=0时,表示输入结束,该行不做处理。Output对于每个给定范围内的取值,如果表达式的值都为素数,则输出"OK",否则请输出“Sorry”,每组输出占一行。 
    Sample Input

    0 1
    0 0

    Sample Output

    OK

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <vector>
     6 #include <cstdlib>
     7 #include <iomanip>
     8 #include <cmath>
     9 #include <ctime>
    10 #include <map>
    11 #include <set>
    12 #include <queue>
    13 using namespace std;
    14 #define lowbit(x) (x&(-x))
    15 #define max(x,y) (x>y?x:y)
    16 #define min(x,y) (x<y?x:y)
    17 #define MAX 100000000000000000
    18 #define MOD 1000000007
    19 #define pi acos(-1.0)
    20 #define ei exp(1)
    21 #define PI 3.141592653589793238462
    22 #define INF 0x3f3f3f3f3f
    23 #define mem(a) (memset(a,0,sizeof(a)))
    24 typedef long long ll;
    25 ll gcd(ll a,ll b){
    26     return b?gcd(b,a%b):a;
    27 }
    28 bool cmp(int x,int y)
    29 {
    30     return x>y;
    31 }
    32 const int N=100005;
    33 const int mod=1e9+7;
    34 int prim(int x)
    35 {
    36     int flag=1;
    37     for(int i=2;i*i<=x;i++){
    38         if(x%i==0){
    39             flag=0;
    40             break;
    41         }
    42     }
    43     return flag;
    44 }
    45 int main()
    46 {
    47     std::ios::sync_with_stdio(false);
    48     int n,m;
    49     while(cin>>n>>m&&(n||m)){
    50         int l,flag=1;
    51         for(int i=n;i<=m;i++){
    52             l=i*i+i+41;
    53             if(!prim(l)){
    54                 flag=0;
    55                 break;
    56             }
    57         }
    58         if(flag) cout<<"OK"<<endl;
    59         else cout<<"Sorry"<<endl;
    60     }
    61     return 0;
    62 }
    View Code

    HDU 2521 反素数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2521

    反素数就是满足对于任意i(0<i<x),都有g(i)<g(x),(g(x)是x的因子个数),则x为一个反素数。现在给你一个整数区间[a,b],请你求出该区间的x使g(x)最大。 

    Input第一行输入n,接下来n行测试数据 
    输入包括a,b, 1<=a<=b<=5000,表示闭区间[a,b]. 
    Output输出为一个整数,为该区间因子最多的数.如果满足条件有多个,则输出其中最小的数. 
    Sample Input

    3
    2 3
    1 10
    47 359

    Sample Output

    2
    6
    240

    Hint

    2的因子为:1 2
    10的因子为:1 2 5 10

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <vector>
     6 #include <cstdlib>
     7 #include <iomanip>
     8 #include <cmath>
     9 #include <ctime>
    10 #include <map>
    11 #include <set>
    12 #include <queue>
    13 using namespace std;
    14 #define lowbit(x) (x&(-x))
    15 #define max(x,y) (x>y?x:y)
    16 #define min(x,y) (x<y?x:y)
    17 #define MAX 100000000000000000
    18 #define MOD 1000000007
    19 #define pi acos(-1.0)
    20 #define ei exp(1)
    21 #define PI 3.141592653589793238462
    22 #define INF 0x3f3f3f3f3f
    23 #define mem(a) (memset(a,0,sizeof(a)))
    24 typedef long long ll;
    25 ll gcd(ll a,ll b){
    26     return b?gcd(b,a%b):a;
    27 }
    28 bool cmp(int x,int y)
    29 {
    30     return x>y;
    31 }
    32 const int N=100005;
    33 const int mod=1e9+7;
    34 int prim(int x)
    35 {
    36     int t=0;
    37     for(int i=1;i*i<=x;i++){
    38         if(i*i==x) t++;
    39         else if(x%i==0) t+=2;
    40     }
    41     return t;
    42 }
    43 int main()
    44 {
    45     std::ios::sync_with_stdio(false);
    46     int t,a,b;
    47     cin>>t;
    48     while(t--){
    49         cin>>a>>b;
    50         int t=0,s=0;
    51         for(int i=a;i<=b;i++){
    52             if(prim(i)>t){
    53                 t=prim(i);
    54                 s=i;
    55             }
    56         }
    57         cout<<s<<endl;
    58     }
    59     return 0;
    60 }
    View Code

    HDU 2098 分拆素数和

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2098

    把一个偶数拆成两个不同素数的和,有几种拆法呢?

    Input输入包含一些正的偶数,其值不会超过10000,个数不会超过500,若遇0,则结束。Output对应每个偶数,输出其拆成不同素数的个数,每个结果占一行。Sample Input

    30
    26
    0

    Sample Output

    3
    2

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <vector>
     6 #include <cstdlib>
     7 #include <iomanip>
     8 #include <cmath>
     9 #include <ctime>
    10 #include <map>
    11 #include <set>
    12 #include <queue>
    13 using namespace std;
    14 #define lowbit(x) (x&(-x))
    15 #define max(x,y) (x>y?x:y)
    16 #define min(x,y) (x<y?x:y)
    17 #define MAX 100000000000000000
    18 #define MOD 1000000007
    19 #define pi acos(-1.0)
    20 #define ei exp(1)
    21 #define PI 3.141592653589793238462
    22 #define INF 0x3f3f3f3f3f
    23 #define mem(a) (memset(a,0,sizeof(a)))
    24 typedef long long ll;
    25 ll gcd(ll a,ll b){
    26     return b?gcd(b,a%b):a;
    27 }
    28 bool cmp(int x,int y)
    29 {
    30     return x>y;
    31 }
    32 const int N=10005;
    33 const int mod=1e9+7;
    34 int prim(int x)
    35 {
    36     int flag=1;
    37     for(int i=2;i*i<=x;i++){
    38         if(x%i==0){
    39             flag=0;
    40             break;
    41         }
    42     }
    43     if(flag) return 1;
    44     return 0;
    45 }
    46 int main()
    47 {
    48     std::ios::sync_with_stdio(false);
    49     int n;
    50     while(cin>>n&&n){
    51         int t=0;
    52         for(int i=2;i<n/2;i++){
    53             if(prim(i)&&prim(n-i)){
    54                 t++;
    55             }
    56         }
    57         cout<<t<<endl;
    58     }
    59     return 0;
    60 }
    View Code

    POJ 2262 Goldbach's Conjecture

    题目链接:http://poj.org/problem?id=2262

    In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conjecture: 
    Every even number greater than 4 can be 
    written as the sum of two odd prime numbers.

    For example: 
    8 = 3 + 5. Both 3 and 5 are odd prime numbers. 
    20 = 3 + 17 = 7 + 13. 
    42 = 5 + 37 = 11 + 31 = 13 + 29 = 19 + 23.

    Today it is still unproven whether the conjecture is right. (Oh wait, I have the proof of course, but it is too long to write it on the margin of this page.) 
    Anyway, your task is now to verify Goldbach's conjecture for all even numbers less than a million. 

    Input

    The input will contain one or more test cases. 
    Each test case consists of one even integer n with 6 <= n < 1000000. 
    Input will be terminated by a value of 0 for n.

    Output

    For each test case, print one line of the form n = a + b, where a and b are odd primes. Numbers and operators should be separated by exactly one blank like in the sample output below. If there is more than one pair of odd primes adding up to n, choose the pair where the difference b - a is maximized. If there is no such pair, print a line saying "Goldbach's conjecture is wrong."

    Sample Input

    8
    20
    42
    0
    

    Sample Output

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

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <vector>
     6 #include <cstdlib>
     7 #include <iomanip>
     8 #include <cmath>
     9 #include <ctime>
    10 #include <map>
    11 #include <set>
    12 #include <queue>
    13 using namespace std;
    14 #define lowbit(x) (x&(-x))
    15 #define max(x,y) (x>y?x:y)
    16 #define min(x,y) (x<y?x:y)
    17 #define MAX 100000000000000000
    18 #define MOD 1000000007
    19 #define pi acos(-1.0)
    20 #define ei exp(1)
    21 #define PI 3.141592653589793238462
    22 #define INF 0x3f3f3f3f3f
    23 #define mem(a) (memset(a,0,sizeof(a)))
    24 typedef long long ll;
    25 ll gcd(ll a,ll b){
    26     return b?gcd(b,a%b):a;
    27 }
    28 bool cmp(int x,int y)
    29 {
    30     return x>y;
    31 }
    32 const int N=10005;
    33 const int mod=1e9+7;
    34 int prim(int x)
    35 {
    36     int flag=1;
    37     for(int i=2;i*i<=x;i++){
    38         if(x%i==0){
    39             flag=0;
    40             break;
    41         }
    42     }
    43     if(flag) return 1;
    44     return 0;
    45 }
    46 int main()
    47 {
    48     std::ios::sync_with_stdio(false);
    49     int n;
    50     while(cin>>n&&n){
    51         for(int i=2;i<n;i++){
    52             if(prim(i)&&prim(n-i)){
    53                 cout<<n<<" = "<<i<<" + "<<n-i<<endl;
    54                 break;
    55             }
    56         }
    57     }
    58     return 0;
    59 }
    View Code

    题解:都很简单 莫得撒子好说的 ...

  • 相关阅读:
    maven(一 基本操作 命令 标签)
    springmvc学习(五)
    springmvc学习(四)
    springmvc学习(三)
    springmvc学习(二)
    一个简单的springmvc例子 入门(1)
    第一次记录
    字符设备驱动(六)按键poll机制
    字符设备驱动(五)按键优化休眠
    字符设备驱动(四)按键中断
  • 原文地址:https://www.cnblogs.com/wydxry/p/7299988.html
Copyright © 2011-2022 走看看