本文主要是介绍Vivian's Problem 梅森素数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
所谓梅森数,是指形如2p-1的一类数,其中指数p是素数,常记为Mp 。如果梅森数是素数,就称为梅森素数。
关于梅森素数,有一个重要的定理:“一个数能够写成几个不重复的梅森素数的乘积” 等价于 “这个数的约数和是2的幂次”,但是不能重复,比如说3是梅森素数,9就不满足约数和为2的幂。
Vivian's Problem(Asia Guangzhou 2003)
The desire to explore the unknown has been a driving force in human history since the dawn of time. From the earliest documented accounts, ancient civilizations had explored the earth by sailing around. Early adventurers were motivated by religious beliefs, the desire conquest, the need to establish trade routes, and hunger for gold.
You never know what will happen before the exploration. Neither does Bruce Lee. Someday, Mr. Lee entered a desolate tropical rainforest. And after several days' exploring, he came in front of a cave with something blinking in it. A beautiful girl named Vivian came out just before he tried to go into the cave. And Vivian told Mr. Lee that he must answer some questions before he entered the cave. As the best friend of Mr. Lee, you should help him to work it out.
You will get k positive integers p1, p2 ... pi ... pk (1 ≤ i ≤ k) from Vivian. From these numbers, you can calculate N, N = PI{pi^ei} (0 ≤ ei ≤ 10, SUM{ei} ≥ 1, 1 ≤ i ≤ k); you may decide the integers ei's as you wish. From one N, you can calculate corresponding M, which equals to the sum of all divisors of N. Now, you should tell Vivian whether or not there is an M which is the power of 2 (1, 2, 4, 8, and 16 ... so on). If there's no N can make M equal to the power of 2, tell Vivian "NO". If M equals to some 2^x, then show her the exponent (x). And if there are several x, only show her the largest one.
输入
Input contains several testcases. For each testcase, the first line contains only one integer k (0 < k ≤ 100), representing the number of positive integers. Then there are k positive integers p1, p2 ... pi ... pk (1 < pi < 2^31, 1 ≤ i ≤ k) in the second line, representing the given numbers.
Input is terminated by end of file.
输出
For each testcase, you should output your result in a single line. If you can find N from the given numbers, output the largest exponent. Otherwise, output "NO". Extra spaces are not allowed.
题意:给出K个数,p1,p2,……pk,不一定是素数,给这些数添加指数,0-10之间,最终的乘积为n,他的所有因子和为m,问是否存在一个m为2的幂,如果有多个输出最大的指数,如果没有输出NO。
思路:梅森素数是非常少的,在题目给出的范围之内,只有8个梅森素数,可以打表列出,然后判断每一个给出的数中是不是唯一拥有若干个梅森素数。
#include <stdio.h>
#include <algorithm>
using namespace std;
int mers[10]={(1<<2)-1,(1<<3)-1,(1<<5)-1,(1<<7)-1,(1<<13)-1,(1<<17)-1,(1<<19)-1,(1<<31)-1};
int pwr[10]={2,3,5,7,13,17,19,31};
int sum[500],a[500];
bool used[500];
int change(int x)
{int ret=0,i;for(i=0;i<8;i++){if(x%mers[i]==0){x/=mers[i];if(x%mers[i]==0)return -1;ret|=1<<i;}}if(x!=1)return -1;return ret;
}
int main()
{int n,c,cnt,p,ans,k,i,j;for(i=0;i<256;i++){sum[i]=0;for(j=0;j<8;j++){if(i&(1<<j))sum[i]+=pwr[j];}}while(~scanf("%d",&n)) {memset(used,0,sizeof(used));used[0]=1;for(i=1;i<=n;i++){scanf("%d",&p);p=change(p);if(p==-1)continue;for(j=0;j<256;j++){if((j&p)==p&&used[p^j]==1) used[j]=1;}}ans=0;for(j=0;j<256;j++){if(used[j]&&ans<sum[j])ans=sum[j];}if(ans==0)printf("NO\n");elseprintf("%d\n",ans);}return 0;
}
这篇关于Vivian's Problem 梅森素数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!