Mersenne Composite Numbers
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1494 | Accepted: 698 |
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
题意很简单,就是有点绕呀;
这题开始我用的 LL temp=(LL)(1<<((LL)pri[i]))-1;结果发现最多右移31位,然后对于大于30的改成了暴力求幂,然后tle,无语,打表把。。
不用打表也可以过:加上那个限制条件,然他在1<<59里内找,for(int i=0;pri[i]<=k&&pri[i]<=59;i++)
View Code
1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4
5 typedef long long LL;
6 using namespace std;
7 LL prime[10000],pn[10000],num;
8
9 bool prime_factor(LL p)
10 {
11 num=0;
12 for(LL i=2;i*i<=p;i++)
13 {
14 if(p%i==0)
15 {
16 prime[num]=i;
17 LL temp=0;
18 while(p%i==0)
19 {
20 temp++;
21 p/=i;
22 }
23 pn[num++]=temp;
24 }
25 }
26 if(p>1) prime[num]=p,pn[num++]=1;
27 if(num<=1) return 0;
28 else return 1;
29 }
30 int pri[100],isprime[100];
31 int find_prime()
32 {
33 memset(isprime,1,sizeof(isprime));
34 int temp=0;
35 for(int i=2;i<100;i++)
36 {
37 if(isprime[i])
38 {
39 for(int j=2*i;j<100;j+=i)
40 {
41 isprime[j]=0;
42 }
43 pri[temp++]=i;
44 }
45 }
46 return temp;
47 }
48 int main()
49 {
50 int k;
51 find_prime();
52 while(cin>>k)
53 {
54 for(int i=0;pri[i]<=k;i++)
55 {
56 LL temp=(LL)(1<<((LL)pri[i]))-1;
57 //cout<<temp<<" ";
58 if(pri[i]>31)
59 {
60 int ti=pri[i];
61 temp=1;
62 while(ti--)
63 temp*=2;
64 temp-=1;
65 }
66 if(prime_factor(temp))
67 {
68 for(int j=0;j<num;j++)
69 {
70 for(int k=0;k<pn[j];k++)
71 {
72 cout<<prime[j];
73 if(!(j==num-1&&k==pn[j]-1)) cout<<" * ";
74 }
75 }
76 cout<<" = "<<temp<<" = ";
77 cout<<"( 2"<<" ^ "<<pri[i]<<" )"<<" - 1"<<endl;
78 }
79 }
80 }
81 return 0;
82 }
然后打表代码:
View Code
1 #include <iostream>
2 #include <cstdio>
3 #include <cstdlib>
4 #include <cstring>
5 using namespace std;
6
7 int main()
8 {
9 //freopen("t.txt", "r", stdin);
10 int f[10] =
11 { 11, 23, 29, 37, 41, 43, 47, 53, 59 };
12 char st[10][100] =
13 { "23 * 89 = 2047 = ( 2 ^ 11 ) - 1",
14 "47 * 178481 = 8388607 = ( 2 ^ 23 ) - 1",
15 "233 * 1103 * 2089 = 536870911 = ( 2 ^ 29 ) - 1",
16 "223 * 616318177 = 137438953471 = ( 2 ^ 37 ) - 1",
17 "13367 * 164511353 = 2199023255551 = ( 2 ^ 41 ) - 1",
18 "431 * 9719 * 2099863 = 8796093022207 = ( 2 ^ 43 ) - 1",
19 "2351 * 4513 * 13264529 = 140737488355327 = ( 2 ^ 47 ) - 1",
20 "6361 * 69431 * 20394401 = 9007199254740991 = ( 2 ^ 53 ) - 1",
21 "179951 * 3203431780337 = 576460752303423487 = ( 2 ^ 59 ) - 1" };
22 int n;
23 scanf("%d", &n);
24 for (int i = 0; i < 9; i++)
25 if (f[i] <= n)
26 printf("%s\n", st[i]);
27
28 return 0;
29 }