Vivian's Problem 梅森素数

本文介绍了一个基于梅森素数的算法问题,即通过给定的整数集合构造一个数N,使得N的所有因子之和为2的幂,并讨论了相应的解决思路和实现代码。

所谓梅森数,是指形如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");
        else
        printf("%d\n",ans);
    }
    return 0;
}

 

 

 

 

 

### 梅森素数的定义与计算方法 梅森素数是指形如 \(2^p - 1\) 的正整数,其中指数 \(p\) 是一个质数[^2]。如果 \(2^p - 1\) 同时也是一个质数,则称其为梅森素数梅森素数的研究在数学和计算机科学领域中具有重要意义。 #### 梅森素数的计算方法 为了计算梅森素数,通常需要以下几个步骤: 1. **生成候选梅森数**:根据梅森素数的定义,首先生成所有形如 \(2^p - 1\) 的数字,其中 \(p\) 是小于给定范围的所有质数。 2. **判断是否为素数**:对生成的每个梅森数进行素性测试,以确定它是否是一个质数。常用的素性测试算法包括试除法、米勒-拉宾算法(Miller-Rabin Algorithm)以及卢卡斯-莱默算法(Lucas-Lehmer Test)[^4]。 #### 实现代码示例 以下是一个使用 Python 编写的简单程序,用于找出 \(n < 20\) 的所有梅森素数: ```python import math def is_prime(num): if num <= 1: return False for i in range(2, int(math.sqrt(num)) + 1): if num % i == 0: return False return True def find_mersenne_primes(limit): mersenne_primes = [] for p in range(2, limit): if is_prime(p): # 判断指数是否为质数 mersenne_number = 2**p - 1 if is_prime(mersenne_number): # 判断梅森数是否为质数 mersenne_primes.append(mersenne_number) return mersenne_primes # 计算 n < 20 的所有梅森素数 result = find_mersenne_primes(20) print("梅森素数列表:", result) ``` #### 应用场景 梅森素数在数学和计算机科学中有广泛的应用: 1. **密码学**:梅森素数由于其特殊的性质,常被用于生成大质数,从而应用于加密算法中。 2. **分布式计算**:寻找更大的梅森素数是分布式计算项目(如 GIMPS,Great Internet Mersenne Prime Search)的重要目标之一。 3. **理论研究**:梅森素数的研究推动了数学领域的许多重要发现,尤其是在数论和代数领域。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值