2013-07-12 07:53:41Morris

[UVA][梅森質數] 1180 - Perfect Numbers

The early Greeks have made great contributions to the field of mathematics. Among the great mathematicians of this times were Euclid and Pythagoras. The 23 volume Elements of Euclid is still regarded as on of the cornerstones in the study of mathematics.

Euclid made an important contributions to a problem posed by Pythagoras - that of finding all the perfect numbers. The number 6 is called a perfect number because 6 = 1 + 2 + 3, the sum of all its proper divisors, (i.e. divisors less than 6). Another example of a perfect number is 28, because 28 = 1 + 2 + 4 + 7 + 14, and 1, 2, 4, 7 and 14 are the divisors of 28 less than 28.

In Book IX of the `Elements' Euclid found all the even perfect numbers. (It has been proven later much later, in the 20-th century that all perfect numbers are even.) Euclid proved that an even number is perfect if it has the form

2p-1(2p - 1)
where both p and 2p - 1 are prime numbers.

Two thousand years later, Leonhard Euler proved the converse of Euclid's theorem. That is every even perfect number must be of Euclid's type. For example, for 6 and 28, we have

6 = 22-1(22 -1), and 28 = 23-1(23 - 1)

Perfect numbers are very rare. By 1975, only 24 perfect numbers have been discovered. The first four perfect numbers are


6, 28, 496, and 8128


which correspond to the following values of p


2, 3, 5, and 7.


You would be given several integer numbers p, which would not necessarily be prime numbers. You have to determine if 2p-1(2p - 1) is a perfect number. The largest perfect number in this problem will not exceed 233.

Input 

The input contains two lines of integers. The first line contains the number of integers appearing on the second line. The second line contains the values of p that should be tested.

Output 

For each integer value to be tested output ``Yes" or ``No" on a line of its own. The output will be ``Yes" if the integer value p generates a perfect number and ``No" otherwise.

Sample Input 

  
6 
2,3,4,5,6,7

Sample Output 

  
Yes 
Yes 
No 
Yes 
No 
Yes

看網路上公布的做法,感覺會 overflow 的說,對於 2^33。
至於梅森質數直接去 wiki 打表


#include <stdio.h>
#include <map>
using namespace std;
int main() {
//http://zh.wikipedia.org/wiki/%E6%A2%85%E6%A3%AE%E7%B4%A0%E6%95%B0
map<int, int> R;
int A[] = {2, 3, 5, 7, 13, 17, 31, 61, 89};
int n, x, i;
for(i = 0; i < 9; i++)
R[A[i]] = 1;
while(scanf("%d", &n) == 1) {
for(i = 0; i < n; i++) {
scanf("%d", &x);
int flag = R[x];
puts(flag ? "Yes" : "No");
getchar();
}
}
return 0;
}