题意:给定k,求所有满足小于2^k的所有梅森数(2^p(p为素数)-1被素因子分解的数)。
思路:此题一看就想到打表、先暴力打表,然后直接写即可。
题目链接:http://poj.org/problem?id=2191
View Code
1 /**********************************暴力打表***************************** 2 #include <cstdio> 3 #include <cmath> 4 #include <cstdlib> 5 #include <cstring> 6 #include <string> 7 #include <algorithm> 8 #include <iostream> 9 using namespace std; 10 11 bool isprime(int n){ 12 for(int i=2;i<n;i++) 13 if(n%i==0) return false; 14 return true; 15 } 16 17 int main(){ 18 19 // freopen("data.in","r",stdin); 20 // freopen("data.out","w",stdout); 21 22 for(int i=31;i<=61;i++){ 23 if(isprime(i)){ 24 long long k=1; 25 k<<=i; 26 k--; 27 for(long long j=2;j<=sqrt(k*1.0);j++){ 28 if(k%j==0){ 29 printf("%lld *",j); 30 k/=j; 31 } 32 } 33 if(k!=1) printf(" %lld",k); 34 k=1; 35 k<<=i; 36 k--; 37 printf(" = %lld = ( 2 ^ %d ) - 1\n",k,i); 38 } 39 } 40 return 0; 41 } 42 */ 43 44 45 #include <cstdio> 46 #include <cmath> 47 #include <cstdlib> 48 #include <cstring> 49 #include <string> 50 #include <algorithm> 51 #include <iostream> 52 using namespace std; 53 54 int a[10]={11,23,29,37,41,43,47,53,59}; 55 char b[10][100]={ 56 "23 * 89 = 2047 = ( 2 ^ 11 ) - 1", 57 "47 * 178481 = 8388607 = ( 2 ^ 23 ) - 1", 58 "233 * 1103 * 2089 = 536870911 = ( 2 ^ 29 ) - 1", 59 "223 * 616318177 = 137438953471 = ( 2 ^ 37 ) - 1", 60 "13367 * 164511353 = 2199023255551 = ( 2 ^ 41 ) - 1", 61 "431 * 9719 * 2099863 = 8796093022207 = ( 2 ^ 43 ) - 1", 62 "2351 * 4513 * 13264529 = 140737488355327 = ( 2 ^ 47 ) - 1", 63 "6361 * 69431 * 20394401 = 9007199254740991 = ( 2 ^ 53 ) - 1", 64 "179951 * 3203431780337 = 576460752303423487 = ( 2 ^ 59 ) - 1" 65 }; 66 67 int main(){ 68 69 // freopen("data.in","r",stdin); 70 // freopen("data.out","w",stdout); 71 72 int n; 73 while(scanf("%d",&n)!=EOF){ 74 for(int i=0;i<10;i++){ 75 if(n>=a[i]) puts(b[i]); 76 else break; 77 } 78 } 79 return 0; 80 }