首页 > 代码库 > UVA - 1323 Vivian's Problem

UVA - 1323 Vivian's Problem

Description

Download as PDF

The desire to explore the unknown has been a driving force in human history since the dawn of time. From the earliestdocumented accounts, ancient civilizations had explored the earth by sailing around. Early adventurers were motivatedby 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 desolatetropical 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 mustanswer 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$ \le$i$ \le$k) from Vivian. From thesenumbers, you can calculate N,

N = $\displaystyle \prod_{i=1}^{k}$piei        (0$\displaystyle \le$ei$\displaystyle \le$10,$\displaystyle \sum_{i=1}^{k}$ei$\displaystyle \geq$ 1, 1$\displaystyle \le$i$\displaystyle \le$k);
you may decide the integers ei‘s as you wish. From oneN, you can calculate corresponding M, whichequals to the sum of all divisors of N. Now, you should tell Vivian whether or not there is anM which is thepower of 2 (1,2, 4, 8, and 16 ...so on). If there‘s noN can make M equal to the power of 2,tell Vivian ``NO". IfM equals to some 2x, then show her the exponent(x). And if there are several x,only show her the largest one.

Input

Input contains several testcases. For each testcase, the first line contains only one integerk (0 < k$ \le$100),representing the number of positive integers. Then there are k positive integersp1,p2...pi...pk (1 < pi < 231, 1$ \le$i$ \le$k) in the second line, representingthe given numbers.

Input is terminated by end of file.

Output

For each testcase, you should output your result in a single line. If you can findN from the given numbers,output the largest exponent. Otherwise, output `NO‘. Extra spaces are not allowed.

Sample Input

1
2
3
2 3 4

Sample Output

NO
2

题意:输入k个正整数p1,p2,p3,..,pk,找出k个非符整数ei使得,N = pi^ei(1<=i<=k),注意,由于x>0,ei不能全为0,如果无解输出NO,否则输出最大的x

思路:梅森素数问题:关于梅森素数,有一个重要的定理:“一个数能够写成几个不重复的梅森素数的乘积” 等价于 “这个数的约数和是2的幂次”,根据这个原理,我们去分解每个数,然后求解

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
typedef long long ll;
using namespace std;
const int maxn = 105;

int f[8] = {2,3,5,7,13,17,19,31} ;
int ans, cnt, status[maxn];
int dp[10];

void insert(int a) {
	status[cnt] = 0;
	for (int i = 0; i < 8 && a >= dp[i]; i++) 
		if (a % dp[i] == 0) {
			status[cnt] |= 1<<i;
			a /= dp[i];
			if (a % dp[i] == 0)
				return;
		}
	if (a == 1)	
		cnt++;
}

int  getans(int st) {
	int res = 0;
	for (int i = 0; i < 8; i++)
		if (st & (1<<i))
			res += f[i];
	return res;
}

void dfs(int cur, int st) {
	ans = max(ans, getans(st));
	for (int i = cur; i < cnt; i++)
		if ((st & status[i]) == 0)
			dfs(i+1, st | status[i]);
}

int main() {
	for (int i = 0; i < 8; i++)
		dp[i] = (1 << f[i]) - 1;
	int k, a;
	while (scanf("%d", &k) != EOF) {
		cnt = 0;
		for (int i = 0; i < k; i++) {
			scanf("%d", &a);
			insert(a);
		}
		if (cnt == 0) {
			printf("NO\n");
		} else {
			ans = 0;
			dfs(0, 0);
			printf("%d\n", ans);
		}
	}
	return 0;
}


UVA - 1323 Vivian's Problem