[sicily online]1009. Mersenne Composite N

来源:互联网 发布:谷歌优化指南 编辑:程序博客网 时间:2024/06/05 01:54
/*移位操作的限制ConstraintsTime Limit: 1 secs, Memory Limit: 32 MBDescriptionOne 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 (2p–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 Number24–1 = 3 -- prime38–1 = 7 -- prime532–1 = 31 -- prime7128–1 = 127 -- primeIf, 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 2k -- 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.InputInput is from file a. in. It contains a single number, without leading or trailing blanks, giving the value of k. As promised, k <= 63.OutputOne 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 Input31Sample Output23 * 89 = 2047 = ( 2 ^ 11 ) - 147 * 178481 = 8388607 = ( 2 ^ 23 ) - 1233 * 1103 * 2089 = 536870911 = ( 2 ^ 29 ) - 1*/#include<iostream>#include <iomanip>#include<stdio.h>#include<cmath>#include<iomanip>#include<list>#include <map>#include <vector>#include <string>#include <algorithm>#include <sstream>#include <stack>#include<queue>#include<string.h>using namespace std;bool cal(unsigned long long number,vector<unsigned long long>&factor){for(unsigned long long i=3;i<=sqrt((double)number);i+=2){if(number%i==0){factor.push_back(i);if(!cal(number/i,factor))factor.push_back(number/i);return true;}}return false;}int main(){int n;cin>>n;for(int i=6;i<n;i++){bool flag=false;for(int j=2;j<=sqrt((double)i);j++){if(i%j==0)flag=true;}if(flag)continue;unsigned long long number=pow(2.0,i)-1;//移位操作最多只能31位vector<unsigned long long> factor;cal(number,factor);sort(factor.begin(),factor.end());if(factor.size()<2)continue;for(vector<unsigned long long>::size_type j=0;j<factor.size()-1;j++)cout<<factor[j]<<" * ";cout<<factor[factor.size()-1]<<" = "<<number<<" = ( 2 ^ "<<i<<" ) - 1"<<endl;}}