[sicily online]1009. Mersenne Composite N

來源:互聯網
上載者:User
/*移位操作的限制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;}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.