部分有关素数的题
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
Sample Output
OK1 #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
Sample Output
2 6 240Hint
2的因子為:1 2 10的因子為:1 2 5 101 #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 0Sample Output
3 21 #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 0Sample Output
8 = 3 + 5 20 = 3 + 17 42 = 5 + 371 #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
題解:都很簡單 莫得撒子好說的 ...
轉載于:https://www.cnblogs.com/shixinzei/p/7299988.html
總結
- 上一篇: TP5与TP3.X对比
- 下一篇: Head first java chap