码迷,mamicode.com
首页 > 其他好文 > 详细

UVA - 1323 Vivian's Problem

时间:2014-08-27 13:00:27      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   io   strong   for   

Description

bubuko.com,布布扣

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 (1bubuko.com,布布扣ibubuko.com,布布扣k) from Vivian. From thesenumbers, you can calculate N,

N = bubuko.com,布布扣piei        (0bubuko.com,布布扣eibubuko.com,布布扣10,bubuko.com,布布扣ei bubuko.com,布布扣 1, 1bubuko.com,布布扣ibubuko.com,布布扣k);
you may decide the integers ei‘s as you wish. From one N, you can calculate corresponding M, whichequals to the sum of all divisors of N. Now, you should tell Vivian whether or not there is an M which is thepower 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 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 integer k (0 < kbubuko.com,布布扣100),representing the number of positive integers. Then there are k positive integersp1, p2...pi...pk (1 < pi < 231, 1bubuko.com,布布扣ibubuko.com,布布扣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 find N 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

标签:des   style   blog   http   color   os   io   strong   for   

原文地址:http://blog.csdn.net/u011345136/article/details/38868083

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!