poj 2191 Mersenne Composite Numbers 筛法判素数
Mersenne Composite Numbers
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2256 | Accepted: 1068 |
Description
One of the world-wide cooperative computing tasks is the "Grand Internet Mersenne Prime Search" -- GIMPS -- striving to find ever-larger prime numbers by examining a particular category of such numbers.
A Mersenne number is defined as a number of the form (2 p–1), where p is a prime number -- a number divisible only by one and itself. (A number that can be divided by numbers other than itself and one are called "composite" numbers, and each of these can be uniquely represented by the prime numbers that can be multiplied together to generate the composite number — referred to as its prime factors.)
Initially it looks as though the Mersenne numbers are all primes.
If, however, we are having a "Grand Internet" search, that must not be the case.
Where k is an input parameter, compute all the Mersenne composite numbers less than 2 k -- where k <= 63 (that is, it will fit in a 64-bit signed integer on the computer). In Java, the "long" data type is a signed 64 bit integer. Under gcc and g++ (C and C++ in the programming contest environment), the "long long" data type is a signed 64 bit integer.
A Mersenne number is defined as a number of the form (2 p–1), where p is a prime number -- a number divisible only by one and itself. (A number that can be divided by numbers other than itself and one are called "composite" numbers, and each of these can be uniquely represented by the prime numbers that can be multiplied together to generate the composite number — referred to as its prime factors.)
Initially it looks as though the Mersenne numbers are all primes.
Prime | Corresponding Mersenne Number |
---|---|
2 | 4–1 = 3 -- prime |
3 | 8–1 = 7 -- prime |
5 | 32–1 = 31 -- prime |
7 | 128–1 = 127 -- prime |
If, however, we are having a "Grand Internet" search, that must not be the case.
Where k is an input parameter, compute all the Mersenne composite numbers less than 2 k -- where k <= 63 (that is, it will fit in a 64-bit signed integer on the computer). In Java, the "long" data type is a signed 64 bit integer. Under gcc and g++ (C and C++ in the programming contest environment), the "long long" data type is a signed 64 bit integer.
Input
Input contains a single number, without leading or trailing blanks, giving the value of k. As promised, k <= 63.
Output
One line per Mersenne composite number giving first the prime factors (in increasing order) separate by asterisks, an equal sign, the Mersenne number itself, an equal sign, and then the explicit statement of the Mersenne number, as shown in the sample output. Use exactly this format. Note that all separating white space fields consist of one blank.
Sample Input
31
Sample Output
23 * 89 = 2047 = ( 2 ^ 11 ) - 1 47 * 178481 = 8388607 = ( 2 ^ 23 ) - 1 233 * 1103 * 2089 = 536870911 = ( 2 ^ 29 ) - 1
题意:梅森数即2^n-1(n是素数)的数,你要找的是梅森合数,即这个数是一个合数;给你一个n,找比它小的梅森合数;
思路:打表,大的数我只能用O(n)方法判断;//用G++交,因为pow函数C++有精度问题
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<queue> #define M 1020000 #define LL long long using namespace std; struct cprim { int powe; int num; long long n[5]; } p[15]; int main() { p[0].powe=11; p[0].num=2; p[0].n[0]=23; p[0].n[1]=89; p[1].powe=23; p[1].num=2; p[1].n[0]=47; p[1].n[1]=178481; p[2].powe=29; p[2].num=3; p[2].n[0]=233; p[2].n[1]=1103; p[2].n[2]=2089; p[3].powe=37; p[3].num=2; p[3].n[0]=223; p[3].n[1]=616318177; p[4].powe=41; p[4].num=2; p[4].n[0]=13367; p[4].n[1]=164511353; p[5].powe=43; p[5].num=3; p[5].n[0]=431; p[5].n[1]=9719; p[5].n[2]=2099863; p[6].powe=47; p[6].num=3; p[6].n[0]=2351; p[6].n[1]=4513; p[6].n[2]=13264529; p[7].powe=53; p[7].num=3; p[7].n[0]=6361; p[7].n[1]=69431; p[7].n[2]=20394401; p[8].powe=59; p[8].num=2; p[8].n[0]=179951; p[8].n[1]=3203431780337; p[9].powe=10001; int n,i,j; while(~scanf("%d",&n)) { for(i=0; p[i].powe<=n; i++) { printf("%lld",p[i].n[0]); for(j=1; j<p[i].num; j++) printf(" * %lld",p[i].n[j]); long long x=pow((long double)2,(long double)p[i].powe)-1; printf(" = %lld = ( ",x); printf("2 ^ %d ) - 1\n",p[i].powe); } } return 0; }