poj 2191 Mersenne Composite Numbers 筛法判素数

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. 
PrimeCorresponding Mersenne Number
24–1 = 3 -- prime
38–1 = 7 -- prime
532–1 = 31 -- prime
7128–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;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值