Vivian's Problem 梅森素数

2023-11-05 05:50
文章标签 problem 素数 梅森 vivian

本文主要是介绍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 梅森素数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/347841

相关文章

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

uva 10025 The ? 1 ? 2 ? ... ? n = k problem(数学)

题意是    ?  1  ?  2  ?  ...  ?  n = k 式子中给k,? 处可以填 + 也可以填 - ,问最小满足条件的n。 e.g k = 12  - 1 + 2 + 3 + 4 + 5 + 6 - 7 = 12 with n = 7。 先给证明,令 S(n) = 1 + 2 + 3 + 4 + 5 + .... + n 暴搜n,搜出当 S(n) >=

11991 - Easy Problem from Rujia Liu?

题意: 输入一串整型数列,再输入两个数k,v,输出第k个v的序号。不存在则输出0,如第一个样例 8 41 3 2 2 4 3 2 11 3 //第1个3,序号为2,输出22 4 //第2个4,不存在,输出03 2 //第3个2,序号为7,输出74 2 思路: struct num {

HDU 1016 Prime Ring Problem (深搜)

OJ题目 : click here ~~ 大概题意:n个数,形成一个环,使得相邻两个数的和为素数。以1开始,按字典序输出序列。 很简单的深搜。 AC_CODE int n;int visit[22];int num[22];int len;bool Is_prime(int x){for(int i = 2;i*i <= x;i++)if(x%i == 0) return

js算法判断是否为素数

/*判断一个数字是否是质数: 质数(prime number)又称素数,有无限个。除了1和它本身以外不再被其他的除数整除。*/ function isPrime(number){ //判断输入是否为number类型,是否为整数       if (typeof number!=='number'||!Number.isInteger(number))      {

LVM 'Can’t open /dev/sdb1 exclusively. Mounted filesystem?' Problem

在将几块盘做LVM时,遇到一个之前都没遇到过的问题: root@ubuntu:~# pvcreate /dev/sdc1Can't open /dev/sdc1 exclusively. Mounted filesystem? 首先第一反应就是查看这个分区是否已经在使用了,但是没有。 查看硬盘的一些信息: root@ubuntu:~# cat /proc/partitionsmajo

【时时三省】c语言例题----华为机试题< 查找组成一个偶数最接近的两个素数>

山不在高,有仙则名。水不在深,有龙则灵。                                                                         ----CSDN 时时三省 1,题目 HJ60 查找组成一个偶数最接近的两个素数 描述 任意一个偶数(大于2)都可以由2个素数组成,组成偶数的2个素数有很多种情况,本题目要求输出组成指定偶数的两个

题目:求100以内的素数,全部打印出来。

题目:求100以内的素数,全部打印出来。 public class ZhaoSuShu {public static void isPrime1(){int i,j,count = 0;//System.out.println("2");for(i = 1; i <= 100; i++){for(j = 2; j <= i; j++){if(i % j == 0){break;}}if(j ==

素数判定和分解质素数

1.素数判定   public static boolean isPrime(int n) {if (n <= 1) return false;if (n == 2) return true;if (n % 2 == 0) return false;int limit = (int)Math.sqrt(n) + 1;for (int i = 3; i <= limit; i += 2) {i

【HDU】3861 The King’s Problem 强连通缩点+有向图最小路径覆盖

传送门:【HDU】3861 The King’s Problem 题目分析:首先强连通缩点,因为形成一个环的王国肯定在一条路径中,这样才能保证拆的少。 然后缩点后就是DAG图了,由于题目要求的是最小路径覆盖,那么二分匹配即可。 代码如下: #include <cstdio>#include <cstring>#include <algorithm>#includ