UVA - 1323 Vivian's Problem

Description

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 (1ik)
from Vivian. From thesenumbers, you can calculate N,

N = piei        (0ei10,ei
1, 1ik);

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 < k100),representing the number of positive integers. Then
there are k positive integersp1,
p2...pi...pk
(1 < pi < 231, 1ik)
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

时间: 2024-08-29 15:27:33

UVA - 1323 Vivian's Problem的相关文章

uva 1323 - Vivian&#39;s Problem(梅森素数)

题目链接:uva 1323 - Vivian's Problem 题目大意:给定N个数,然后为每个数添加一个幂ei,最后N项垒乘的结果为M,要是得M的所有因子的和可以写成2x,求x的最大值,如果没有条件满足,输出NO 解题思路:若一个数可以写成若干个不同的梅森素数的乘积,那么这个数的所以因子和可以写成2x. 232?1的范围内只有8个梅森素数,所以可以用状压处理. 梅森素数即为2^i-1形式的素数 /********************** * 梅森素数,(2^k) - 1 * * 一个数若

[2016-02-19][UVA][524][Prime Ring Problem]

UVA - 524 Prime Ring Problem Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  into each circle separately, and the

UVA 10837 - A Research Problem(欧拉函数)

UVA 10837 - A Research Problem 题目链接 题意:给定phi(n),求最小满足的最小的n 思路:phi(n)=pk11(p1?1)?pk22(p2?1)?pk33(p3?1)....(p为质数),因此对于给定phi(n),先把满足条件phi(n)%(p?1)=0的素数全找出来,在这些素数基础上进行暴力搜索,枚举哪些素数用与不用,求出最小值.这样做看似时间复杂度很高,但是实际上,由于每次多选一个素数之后对于值是呈指数上升的,所以实际组合出来的情况并不会太多,因此是可行的

uva 10401 Injured Queen Problem(DP)

uva 10401 Injured Queen Problem 题目大意:这是一个变形的N皇后问题,皇后不再是占据一行一列以及斜线,她占据的只是她周围的一圈以及她所在的一列.题目给出一个含有问号,数字和字母的字符串.第i个字符是问号代表皇后在第i列的任意一行,若第i个字符是数字或字母X(1-F)代表皇后在第i列的X行.求满足该字符串的摆放方式的方法一共有几种. 解题思路:从第一列开始往后递推.dp[i][j]表示的是结合j - 1列的摆放方式,将皇后放在(i, j)的位置会有几种情况. #inc

uva 10837 - A Research Problem(欧拉函数+暴力)

题目链接:uva 10837 - A Research Problem 题目大意:给定一个phin,要求一个最小的n,欧拉函数n等于phin 解题思路:欧拉函数性质有,p为素数的话有phip=p?1;如果p和q互质的话有phip?q=phip?phiq 然后根据这样的性质,n=pk11(p1?1)?pk22(p2?1)???pkii(pi?1),将所有的pi处理出来,暴力搜索维护最小值,虽然看上去复杂度非常高,但是因为对于垒乘来说,增长非常快,所以搜索范围大大被缩小了. #include <cs

uva 11490 - Just Another Problem(数学)

题目链接:uva 11490 - Just Another Problem 题目大意:有n个士兵,要排列成一个方阵,要求方阵尽量大,于是在方正的中间会空出两个正方形的区域,空出来的局域要求厚度相同,即正方形的四条边向相应方向均再有x行或者列. 解题思路:根据题意可以知道x(6x+7r)=n,x为厚度,r为正方形的边长.接着枚举x,x是n的因子. #include <cstdio> #include <cstring> #include <cmath> typedef l

UVA 11490 - Just Another Problem(数论)

11490 - Just Another Problem 题目链接 题意:有S个士兵,排成一个矩阵,矩阵中可以有两个洞,要求两个洞上下左右厚度一样,问能缺少士兵的情况数. 思路:推推公式,设厚度为a, 正方形为i, 那么(3 a + 2 i) (2 a + i) = S + 2 i i; 化简一下得到6 i i + 7 a i = S 由于S很大,所以去枚举厚度,这样只要枚举到sqrt(S)就够了,复杂度可以接受 代码: #include <stdio.h> #include <stri

UVA 1363 Joseph&#39;s Problem

https://vjudge.net/problem/UVA-1363 n 题意:求 Σ  k%i i=1 除法分块 如果 k/i==k/(i+1)=p 那么 k%(i+1)=k-(i+1)*p= k-i*p-p = k%i-p 所以 商相同时,余数为等差数列 我不知道为什么交到vjudge一直WA,网上搜的题解交上去也WA #include<cmath> #include<cstdio> using namespace std; int main() { int n,k,i,j,

uva 10026 Shoemaker&#39;s Problem(贪心+排序)

虽然是个水题,但是在一些细节上wa了几次,好像不支持'\b'退格符号,我用在了输出空格那,结果wa了...白白 wa了几次...题意是看的题解..今天只写了两道题,速度有点慢,得加快了,以后得先认真读懂题目,题目读懂了 就相当于做出来一半然后仔细动脑想想,有想法了再敲,不能盲目的做题.另外,热烈祝贺今天c++ primer看到 了100页 思路: 这道题是让给的数据是每件工作需要做的天数和每耽误一天所需要的费用,让求一个序列使得付费最小,如果有相同答 案把字典树最小的输出...输出的是序号,该件